Yang Zhang avatar Yang Zhang committed bb79374

add models

Comments (0)

Files changed (4)

 
 --topic
 CREATE TABLE topic(
-    id                 INTEGER PRIMARY KEY AUTOINCREMENT,
-    title              VARCHAR,
-    content            TEXT,
-    create_date        TIMESTAMP DEFAULT (datetime('now','localtime')),
-    posterid           INTEGER,
+    id                          INTEGER PRIMARY KEY AUTOINCREMENT,
+    title                       VARCHAR,
+    content                     TEXT,
+    create_date                 TIMESTAMP DEFAULT (datetime('now','localtime')),
+    posterid                    INTEGER,
+    comments_number             INTEGER DEFAULT 0,
+    last_comment_posterid       INTEGER,
+    last_comment_date           TIMESTAMP,
     FOREIGN KEY(posterid) REFERENCES user(id)
 );
 
 -- user can star his favorate topic,
 -- when star a topic, whenever topic changed, it will notify the user
 -- unless he refuse recieve it.
-CREATE TABLE star(
+CREATE TABLE star_topic(
     userid              INTEGER,
     topicid             INTEGER,
     recieve             BOOLEAN,
     PRIMARY KEY(userid, topicid)
 );
 
-CREATE TABLE message(
+CREATE TABLE notify_message(
     id                 INTEGER PRIMARY KEY AUTOINCREMENT,
     userid             INTEGER,
     commentid          INTEGER,

models/__init__.py

+from .topic import Topic,Comment
+# -*- coding: utf-8 *-*
    def __init__(self, title, content, create_date):
        self.id = None
        self.title = title
        self.content = content
        self.create_date = create_date
        self.posterid = session['user'].id
        self.comments_number = 0
        self.last_comment_posterid = None
        self.last_comment_date = None
        self.poster = session['user']
        self._score = None
    def post(self):
        pass
    def add_comment(self):
        pass
    'only for admin'
    def revise(self):
        pass
    'only for admin'
    def delete(self):
        pass
    def __init__(self,content,topicid):
        self.id = None
        self.content = content
        self.userid = session['user'].id
        self.topicid =topicid
        self.create_date =None
        self.commenter = session['user']
+# -*- coding: utf-8 *-*
+import string,random
+from flask import g,session
+import sqlite3
+import hashlib
+import datetime
+from py4u.configure import configure
+
+
+class Admin:
+    def __init__(self, email, password):
+        self.email = email
+        self.password = password
+
+    def is_valid(self):
+        c = g.db.cursor()
+        c.execute("SELECT * FROM user WHERE email=:email AND password=:password",\
+                                {'email':self.email,'password':self.password})
+        tempuser = c.fetchone()
+        if(not tempuser):
+            return False
+            
+        return True
+    def reset_password(self):
+        c = g.db.cursor()
+        c.execute("UPDATE user SET password=:password WHERE email=:email ",\
+                                {'email':self.email,'password':self.password})
+        g.db.commit()
+
+class User:
+    def __init__(self,email,nickname,password):
+        self.id = None
+        self.email = email
+        self.nickname = nickname
+        self.avatar = None
+        self.password = not password or hashlib.sha224(password).hexdigest()
+        self.create_date = None
+        self.reputation = None
+
+
+    @staticmethod
+    def get_user_by_id(id):
+        c = g.db.cursor()
+        c.execute("SELECT * FROM user WHERE id=:id",\
+                                {'id':id})
+        result = c.fetchone()
+        tempuser = User(result['email'],result['nickname'],None)
+        tempuser.avatar = result['avatar']
+        tempuser.create_date = result['create_date']
+        tempuser.reputation = result['reputation']
+        return tempuser
+
+
+    @staticmethod
+    def change_avatar_by_id(id,avatar):
+        c = g.db.cursor()
+        c.execute("UPDATE user SET avatar = :avatar WHERE id=:id",\
+                                {'id':id,'avatar':avatar})
+        g.db.commit()
+
+    def is_valid(self):
+        c = g.db.cursor()
+        c.execute("SELECT * FROM user WHERE email=:email AND password=:password",\
+                                {'email':self.email,'password':self.password})
+        tempuser = c.fetchone()
+        if(not tempuser):
+            return False
+        self.id = tempuser['id']
+        self.email = tempuser['email']
+        self.nickname = tempuser['nickname']
+        self.avatar = tempuser['avatar']
+        self.create_date = tempuser['create_date']
+        self.reputation = tempuser['reputation']
+        return True
+
+    def toggle_star_topic(self, topicid):
+        c = g.db.cursor()
+        c.execute("SELECT userid FROM star_topic WHERE userid=:userid AND topicid=:topicid",\
+                                {'userid':self.id, 'topicid':topicid})
+        if(c.fetchone()):
+            c.execute("DELETE FROM star_topic WHERE userid=:userid AND topicid=:topicid",\
+                                {'userid':self.id, 'topicid':topicid})
+        else:
+            c.execute("INSERT INTO star_topic(userid, topicid) VALUES (:userid, :topicid)",\
+                                {'userid':self.id, 'topicid':topicid})
+        g.db.commit()
+
+    @staticmethod
+    def is_email_exsit(email):
+        c = g.db.cursor()
+        c.execute("SELECT nickname FROM user WHERE email=:email",\
+                                {'email':email})
+        if(not c.fetchone()):
+            return False
+        return True
+
+    @staticmethod
+    def is_nickname_exsit(nickname):
+        c = g.db.cursor()
+        c.execute("SELECT nickname FROM user WHERE nickname=:nickname",\
+                                {'nickname':nickname})
+        if(not c.fetchone()):
+            return False
+        return True
+
+    def is_exist(self):
+        c = g.db.cursor()
+        c.execute("SELECT nickname FROM user WHERE email=:email or nickname=:nickname",\
+                                {'email':self.email,'nickname':self.nickname})
+        if(not c.fetchone()):
+            return False
+        return True
+
+
+    def save(self):
+        c = g.db.cursor()
+        self.create_date = datetime.datetime.now()
+        self.reputation = 0
+        c.execute("INSERT INTO user(nickname,email,password,create_date,reputation) VALUES (?,?,?,?,?)",\
+                                (self.nickname,self.email,self.password,self.create_date,self.reputation))
+        self.id = c.lastrowid
+        g.db.commit()
+
+    @staticmethod
+    def password_generate(size=8, chars=string.ascii_letters + string.digits):
+        return ''.join(random.choice(chars) for x in range(size))
+
+    @staticmethod
+    def reset_password_for(email):
+        c = g.db.cursor()
+        c.execute("SELECT nickname FROM user WHERE email=:email",\
+                                {'email':email})
+        if(not c.fetchone()):
+            return None
+        password = User.password_generate()
+        md5pw = hashlib.sha224(password).hexdigest()
+        c.execute("UPDATE user SET password =:password WHERE email=:email",\
+                                {'email':email,'password':md5pw})
+        g.db.commit()
+        return password
+
+        
+    def read_message(self, notify_message_id):
+        pass
+        
+    def get_unread_message(self):
+        pass
+        
+class NotifyMessage:
+    def __init__(self):
+        pass
+        
+    
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.