Yang Zhang avatar Yang Zhang committed 3e518de

star book works!!

Comments (0)

Files changed (10)

     cache = sender
     cache.init_app(app, config={'CACHE_TYPE': 'filesystem', 'CACHE_DIR':'cache'})
 
+@quick_connect('check_started')
+def check_started(sender):
+    if('user' not in session):
+        return
 
+    bookidset = session['user'].get_stared_bookid()
+    for book in sender:
+        if(book.id in bookidset):
+            book.stared = True
 
 
 
 
 
 
+
+
+
+
+
+
+
+
+
+
+
+
+
-# -*- coding: utf-8 *-*
    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
    def get_nickname(self):
        c = g.db.cursor()
        c.execute("SELECT nickname FROM user WHERE email=:email",\
                                {'email':self.email})
        return c.fetchone()['nickname']
    @staticmethod
    def get_avatar_by_id(id):
        c = g.db.cursor()
        c.execute("SELECT avatar FROM user WHERE id=:id",\
                                {'id':id})
        return c.fetchone()['avatar']
    @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 is_email_exsit(self):
        c = g.db.cursor()
        c.execute("SELECT nickname FROM user WHERE email=:email",\
                                {'email':self.email})
        if(not c.fetchone()):
            return False
        return True
    def is_nickname_exsit(self):
        c = g.db.cursor()
        c.execute("SELECT nickname FROM user WHERE nickname=:nickname",\
                                {'nickname':self.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 update(self):
        c = g.db.cursor()
        self.create_date = datetime.datetime.now()
        c.execute("UPDATE user SET nickname =:nickname, email =:email,\
                   avatar =:avatar, reputation=:reputation WHERE id=:id",\
                                {"nickname":self.nickname,"email":self.email,"avatar":self.avatar,"reputation":self.reputation,"id":self.id})
        g.db.commit()
    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()
    def vote_for(self,postid):
        c = g.db.cursor()
        if(self.reputation is None):
            c.execute("SELECT reputation FROM user WHERE id=:id",{'id':self.id})
            self.reputation = c.fetchone()['reputation']
        import math
        score = 0
        if(self.reputation > 2):
            score = int(math.log(self.reputation , 2))
        c.execute("INSERT OR REPLACE INTO vote(userid, postid, popularity) VALUES (?,?,?)",(self.id,postid,score))
        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 create_post_activity(self, points, postid):
        info = "<span>You post a post and gain 2 point</span>"
        tempac = Activity(info,self.id)
        tempac.save()
    def __init__(self, title, link, des, post_date = None):
        self.id = None
        self.title = title
        self.link = link
        self.des = des
        self.post_date = post_date
        self.posterid = None
        self.poster = None
    @staticmethod
    def get_post_byid(id):
        c = g.db.cursor()
        c.execute("SELECT * FROM post WHERE id=:id",\
                                {"id":id})
        post = c.fetchone()
        if(post is None):
            return None
        temppost = Post(post['title'], post['link'],post['des'],post['post_date'])
        temppost.id = post['id']
        temppost.posterid = post['posterid']
        return temppost
        
    @staticmethod
    def is_exist(id):
        c = g.db.cursor()
        c.execute("SELECT id FROM post WHERE id=:id",\
                                {"id":id})
        return  c.fetchone() is not None
    def init_post_poster(self):
        c = g.db.cursor()
        c.execute("SELECT nickname, id,reputation FROM user where id=:id",\
                                {"id":self.posterid})
        tempresult = c.fetchone()
        user = User(None,tempresult['nickname'],None)
        user.reputation = tempresult['reputation']
        user.id = tempresult['id']
        self.poster = user
    @staticmethod
    def get_posts_by_postdate(limit = configure.POSTNUMBER_PER_PAGE, offset =0):
        c = g.db.cursor()
        c.execute("SELECT * FROM post WHERE posterid is not NULL ORDER BY post_date DESC LIMIT :limit OFFSET :offset ",\
                                {"limit":limit,"offset":offset*limit})
        result = c.fetchall()
        posts = []
        for post in result:
            temppost = Post(post['title'], post['link'],post['des'],post['post_date'])
            temppost.id = post['id']
            temppost.posterid = post['posterid']
            c.execute("SELECT nickname, id,reputation FROM user where id=:id",\
                                {"id":temppost.posterid})
            tempresult = c.fetchone()
            user = User(None,tempresult['nickname'],None)
            user.reputation = tempresult['reputation']
            user.id = tempresult['id']
            temppost.poster = user
            posts.append(temppost)
        return posts
    @staticmethod
    def get_hot_posts(limit = configure.POSTNUMBER_PER_PAGE, offset =0):
        c = g.db.cursor()
        c.execute("SELECT * FROM post WHERE julianday('now') - julianday(post_date) <= :save_post_days AND posterid is not NULL\
                   LIMIT :limit OFFSET :offset",\
                                {"limit":limit,"offset":offset*limit,'save_post_days':configure.POST_SAVE})
        result = c.fetchall()
        posts = []
        for post in result:
            temppost = Post(post['title'], post['link'],post['des'],post['post_date'])
            temppost.id = post['id']
            temppost.posterid = post['posterid']
            c.execute("SELECT nickname, id,reputation FROM user where id=:id",\
                                {"id":temppost.posterid})
            tempresult = c.fetchone()
            user = User(None,tempresult['nickname'],None)
            user.reputation = tempresult['reputation']
            user.id = tempresult['id']
            temppost.poster = user
            posts.append(temppost)
        return posts
    @staticmethod
    def get_deadpool_posts(limit = configure.POSTNUMBER_PER_PAGE, offset =0):
        c = g.db.cursor()
        c.execute("SELECT * FROM post WHERE julianday('now') - julianday(post_date) > :save_post_days AND posterid is not NULL\
                    ORDER BY post_date DESC LIMIT :limit OFFSET :offset",\
                                {"limit":limit,"offset":offset*limit,'save_post_days':configure.POST_SAVE})
        result = c.fetchall()
        posts = []
        for post in result:
            temppost = Post(post['title'], post['link'],post['des'],post['post_date'])
            temppost.id = post['id']
            temppost.posterid = post['posterid']
            c.execute("SELECT nickname, id,reputation FROM user where id=:id",\
                                {"id":temppost.posterid})
            tempresult = c.fetchone()
            user = User(None,tempresult['nickname'],None)
            user.reputation = tempresult['reputation']
            user.id = tempresult['id']
            temppost.poster = user
            posts.append(temppost)
        return posts
    @staticmethod
    def delete_byid(id):
        c = g.db.cursor()
        c.execute("DELETE FROM post WHERE id=:id",\
                                {'id':id})
        g.db.commit()
    def save(self):
        c = g.db.cursor()
        self.link = self.link.strip()
        if(not self.link.startswith('http')):
            self.link = 'http://' + self.link
        c.execute("INSERT INTO post(title,link,des,post_date,posterid) VALUES (?,?,?,?,?)",\
                                (self.title,self.link,self.des,datetime.datetime.now(),session['user'].id))
        self.id = c.lastrowid
        g.db.commit()
        
    def special_save(self):
        c = g.db.cursor()
        self.link = self.link.strip()
        c.execute("INSERT INTO post(title,link,post_date) VALUES (?,?,?)",\
                                (self.title,self.link,datetime.datetime.now()))
        self.id = c.lastrowid
        g.db.commit()
    @property
    def domain(self):
        import urlparse
        rv = urlparse.urlparse(self.link).netloc
        if rv.startswith("www."):
            rv = rv[4:]
        return rv
    @property
    def score(self):
        c = g.db.cursor()
        c.execute("SELECT popularity FROM vote WHERE postid=:id",{'id':self.id})
        scores = [x['popularity'] for x in c.fetchall()]
        return sum(scores)
    @property
    def comments_number(self):
        c = g.db.cursor()
        c.execute("SELECT COUNT(*) AS number FROM comment WHERE postid=:id",{'id':self.id})
        result = c.fetchone()
        number = result['number'] if result else 0
        return number
    def __init__(self, userid, posterid, popularity = 0):
        self.userid = userid
        self.postid = postid
        self.popularity = popularity
    def __init__(self, postid, content, userid, parentid = None):
        self.id = None
        self.postid = postid
        self.content = content
        self.userid = userid
        self.poster =None
        self.parentid = parentid
        self.create_date =None
        self.children = []
    def save(self):
        c = g.db.cursor()
        self.create_date = datetime.datetime.now()
        c.execute("INSERT INTO comment(postid,content,userid,parentid,create_date) VALUES (?,?,?,?,?)",\
                                (self.postid,self.content,self.userid,self.parentid,self.create_date))
        self.id = c.lastrowid
        g.db.commit()
    @staticmethod
    def get_comments_by_postid(postid):
        c = g.db.cursor()
        c.execute("SELECT * FROM comment WHERE postid=:id AND parentid is NULL ORDER BY create_date",{'id':postid})
        result = c.fetchall()
        comments = []
        for tempvalue in result:
            tempcomment = Comment(tempvalue['postid'],tempvalue['content'],tempvalue['userid'],tempvalue['parentid'])
            tempcomment.id = tempvalue['id']
            tempcomment.create_date = tempvalue['create_date']
            tempcomment.init_comment_poster()
            tempcomment.init_children()
            comments.append(tempcomment)
        return comments
    def init_comment_poster(self):
        self.poster = User.get_user_by_id(self.userid)
    def init_children(self):
        c = g.db.cursor()
        c.execute("SELECT * FROM comment WHERE parentid=:id ORDER BY create_date",{'id':self.id})
        result = c.fetchall()
        for tempvalue in result:
            if(tempvalue['parentid'] == tempvalue['postid']):
                break
            tempcomment = Comment(tempvalue['postid'],tempvalue['content'],tempvalue['userid'],tempvalue['parentid'])
            tempcomment.id = tempvalue['id']
            tempcomment.create_date = tempvalue['create_date']
            tempcomment.init_comment_poster()
            tempcomment.init_children()
            self.children.append(tempcomment)
    def save_to_parent(self):
        pass
    def __init__(self, info, userid):
        self.info = info
        self.userid = userid
        self.create_date = None
    def save(self):
        c = g.db.cursor()
        self.create_date = datetime.datetime.now()
        c.execute("INSERT INTO activity(info,create_date,userid) VALUES (?,?,?)",\
                                (self.info, self.create_date,self.userid))
        self.id = c.lastrowid
        g.db.commit()
    @staticmethod
    def get_activities_byuserid(id, limit = configure.ActivityNUMBER_PER_PAGE, offset = 0):
        c = g.db.cursor()
        c.execute("SELECT * FROM activity ORDER BY create_date DESC LIMIT :limit OFFSET :offset",\
                                {"limit":limit,"offset":offset*limit})
        result = c.fetchall()
        activities = []
        for activity in result:
            tempac = Activity(activity['info'], activity['userid'])
            tempac.create_date = activity['create_date']
            tempac.id = activity['id']
            activities.append(tempac)
        return activities
        
    
        
        
        
        
+# -*- coding: utf-8 *-*
    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
    def get_nickname(self):
        c = g.db.cursor()
        c.execute("SELECT nickname FROM user WHERE email=:email",\
                                {'email':self.email})
        return c.fetchone()['nickname']
    @staticmethod
    def get_avatar_by_id(id):
        c = g.db.cursor()
        c.execute("SELECT avatar FROM user WHERE id=:id",\
                                {'id':id})
        return c.fetchone()['avatar']
    @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_book(self, bookid):
        c = g.db.cursor()
        c.execute("SELECT userid FROM star_book WHERE userid=:userid AND bookid=:bookid",\
                                {'userid':self.id, 'bookid':bookid})
        if(c.fetchone()):
            c.execute("DELETE FROM star_book WHERE userid=:userid AND bookid=:bookid",\
                                {'userid':self.id, 'bookid':bookid})
        else:
            c.execute("INSERT INTO star_book(userid, bookid) VALUES (:userid, :bookid)",\
                                {'userid':self.id, 'bookid':bookid})
        g.db.commit()
    
    def get_stared_bookid(self):
        c = g.db.cursor()
        c.execute("SELECT bookid FROM star_book WHERE userid=:userid",\
                                {'userid':self.id})
        bookidset = set([x['bookid'] for x in c.fetchall()])
        return bookidset
                                
                                
                                
    def is_email_exsit(self):
        c = g.db.cursor()
        c.execute("SELECT nickname FROM user WHERE email=:email",\
                                {'email':self.email})
        if(not c.fetchone()):
            return False
        return True
    def is_nickname_exsit(self):
        c = g.db.cursor()
        c.execute("SELECT nickname FROM user WHERE nickname=:nickname",\
                                {'nickname':self.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 update(self):
        c = g.db.cursor()
        self.create_date = datetime.datetime.now()
        c.execute("UPDATE user SET nickname =:nickname, email =:email,\
                   avatar =:avatar, reputation=:reputation WHERE id=:id",\
                                {"nickname":self.nickname,"email":self.email,"avatar":self.avatar,"reputation":self.reputation,"id":self.id})
        g.db.commit()
    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()
    def vote_for(self,postid):
        c = g.db.cursor()
        if(self.reputation is None):
            c.execute("SELECT reputation FROM user WHERE id=:id",{'id':self.id})
            self.reputation = c.fetchone()['reputation']
        import math
        score = 1
        if(self.reputation > 2):
            score = int(math.log(self.reputation , 2))
        c.execute("INSERT OR REPLACE INTO vote(userid, postid, popularity) VALUES (?,?,?)",(self.id,postid,score))
        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 create_post_activity(self, points, postid):
        info = "<span>You post a post and gain 2 point</span>"
        tempac = Activity(info,self.id)
        tempac.save()
    def __init__(self, title, link, des, post_date = None):
        self.id = None
        self.title = title
        self.link = link
        self.des = des
        self.post_date = post_date
        self.posterid = None
        self.poster = None
    @staticmethod
    def get_post_byid(id):
        c = g.db.cursor()
        c.execute("SELECT * FROM post WHERE id=:id",\
                                {"id":id})
        post = c.fetchone()
        if(post is None):
            return None
        temppost = Post(post['title'], post['link'],post['des'],post['post_date'])
        temppost.id = post['id']
        temppost.posterid = post['posterid']
        return temppost
        
    @staticmethod
    def is_exist(id):
        c = g.db.cursor()
        c.execute("SELECT id FROM post WHERE id=:id",\
                                {"id":id})
        return  c.fetchone() is not None
    def init_post_poster(self):
        c = g.db.cursor()
        c.execute("SELECT nickname, id,reputation FROM user where id=:id",\
                                {"id":self.posterid})
        tempresult = c.fetchone()
        user = User(None,tempresult['nickname'],None)
        user.reputation = tempresult['reputation']
        user.id = tempresult['id']
        self.poster = user
    @staticmethod
    def get_posts_by_postdate(limit = configure.POSTNUMBER_PER_PAGE, offset =0):
        c = g.db.cursor()
        c.execute("SELECT * FROM post WHERE posterid is not NULL ORDER BY post_date DESC LIMIT :limit OFFSET :offset ",\
                                {"limit":limit,"offset":offset*limit})
        result = c.fetchall()
        posts = []
        for post in result:
            temppost = Post(post['title'], post['link'],post['des'],post['post_date'])
            temppost.id = post['id']
            temppost.posterid = post['posterid']
            c.execute("SELECT nickname, id,reputation FROM user where id=:id",\
                                {"id":temppost.posterid})
            tempresult = c.fetchone()
            user = User(None,tempresult['nickname'],None)
            user.reputation = tempresult['reputation']
            user.id = tempresult['id']
            temppost.poster = user
            posts.append(temppost)
        return posts
    @staticmethod
    def get_hot_posts(limit = configure.POSTNUMBER_PER_PAGE, offset =0):
        c = g.db.cursor()
        c.execute("SELECT * FROM post WHERE julianday('now') - julianday(post_date) <= :save_post_days AND posterid is not NULL\
                   LIMIT :limit OFFSET :offset",\
                                {"limit":limit,"offset":offset*limit,'save_post_days':configure.POST_SAVE})
        result = c.fetchall()
        posts = []
        for post in result:
            temppost = Post(post['title'], post['link'],post['des'],post['post_date'])
            temppost.id = post['id']
            temppost.posterid = post['posterid']
            c.execute("SELECT nickname, id,reputation FROM user where id=:id",\
                                {"id":temppost.posterid})
            tempresult = c.fetchone()
            user = User(None,tempresult['nickname'],None)
            user.reputation = tempresult['reputation']
            user.id = tempresult['id']
            temppost.poster = user
            posts.append(temppost)
        return posts
    @staticmethod
    def get_deadpool_posts(limit = configure.POSTNUMBER_PER_PAGE, offset =0):
        c = g.db.cursor()
        c.execute("SELECT * FROM post WHERE julianday('now') - julianday(post_date) > :save_post_days AND posterid is not NULL\
                    ORDER BY post_date DESC LIMIT :limit OFFSET :offset",\
                                {"limit":limit,"offset":offset*limit,'save_post_days':configure.POST_SAVE})
        result = c.fetchall()
        posts = []
        for post in result:
            temppost = Post(post['title'], post['link'],post['des'],post['post_date'])
            temppost.id = post['id']
            temppost.posterid = post['posterid']
            c.execute("SELECT nickname, id,reputation FROM user where id=:id",\
                                {"id":temppost.posterid})
            tempresult = c.fetchone()
            user = User(None,tempresult['nickname'],None)
            user.reputation = tempresult['reputation']
            user.id = tempresult['id']
            temppost.poster = user
            posts.append(temppost)
        return posts
    @staticmethod
    def delete_byid(id):
        c = g.db.cursor()
        c.execute("DELETE FROM post WHERE id=:id",\
                                {'id':id})
        g.db.commit()
    def save(self):
        c = g.db.cursor()
        self.link = self.link.strip()
        if(not self.link.startswith('http')):
            self.link = 'http://' + self.link
        c.execute("INSERT INTO post(title,link,des,post_date,posterid) VALUES (?,?,?,?,?)",\
                                (self.title,self.link,self.des,datetime.datetime.now(),session['user'].id))
        self.id = c.lastrowid
        g.db.commit()
        
    def special_save(self):
        c = g.db.cursor()
        self.link = self.link.strip()
        c.execute("INSERT INTO post(title,link,post_date) VALUES (?,?,?)",\
                                (self.title,self.link,datetime.datetime.now()))
        self.id = c.lastrowid
        g.db.commit()
    @property
    def domain(self):
        import urlparse
        rv = urlparse.urlparse(self.link).netloc
        if rv.startswith("www."):
            rv = rv[4:]
        return rv
    @property
    def score(self):
        c = g.db.cursor()
        c.execute("SELECT popularity FROM vote WHERE postid=:id",{'id':self.id})
        scores = [x['popularity'] for x in c.fetchall()]
        return sum(scores)
    @property
    def comments_number(self):
        c = g.db.cursor()
        c.execute("SELECT COUNT(*) AS number FROM comment WHERE postid=:id",{'id':self.id})
        result = c.fetchone()
        number = result['number'] if result else 0
        return number
    def __init__(self, userid, posterid, popularity = 0):
        self.userid = userid
        self.postid = postid
        self.popularity = popularity
    def __init__(self, postid, content, userid, parentid = None):
        self.id = None
        self.postid = postid
        self.content = content
        self.userid = userid
        self.poster =None
        self.parentid = parentid
        self.create_date =None
        self.children = []
    def save(self):
        c = g.db.cursor()
        self.create_date = datetime.datetime.now()
        c.execute("INSERT INTO comment(postid,content,userid,parentid,create_date) VALUES (?,?,?,?,?)",\
                                (self.postid,self.content,self.userid,self.parentid,self.create_date))
        self.id = c.lastrowid
        g.db.commit()
    @staticmethod
    def get_comments_by_postid(postid):
        c = g.db.cursor()
        c.execute("SELECT * FROM comment WHERE postid=:id AND parentid is NULL ORDER BY create_date",{'id':postid})
        result = c.fetchall()
        comments = []
        for tempvalue in result:
            tempcomment = Comment(tempvalue['postid'],tempvalue['content'],tempvalue['userid'],tempvalue['parentid'])
            tempcomment.id = tempvalue['id']
            tempcomment.create_date = tempvalue['create_date']
            tempcomment.init_comment_poster()
            tempcomment.init_children()
            comments.append(tempcomment)
        return comments
    def init_comment_poster(self):
        self.poster = User.get_user_by_id(self.userid)
    def init_children(self):
        c = g.db.cursor()
        c.execute("SELECT * FROM comment WHERE parentid=:id ORDER BY create_date",{'id':self.id})
        result = c.fetchall()
        for tempvalue in result:
            if(tempvalue['parentid'] == tempvalue['postid']):
                break
            tempcomment = Comment(tempvalue['postid'],tempvalue['content'],tempvalue['userid'],tempvalue['parentid'])
            tempcomment.id = tempvalue['id']
            tempcomment.create_date = tempvalue['create_date']
            tempcomment.init_comment_poster()
            tempcomment.init_children()
            self.children.append(tempcomment)
    def save_to_parent(self):
        pass
    def __init__(self, info, userid):
        self.info = info
        self.userid = userid
        self.create_date = None
    def save(self):
        c = g.db.cursor()
        self.create_date = datetime.datetime.now()
        c.execute("INSERT INTO activity(info,create_date,userid) VALUES (?,?,?)",\
                                (self.info, self.create_date,self.userid))
        self.id = c.lastrowid
        g.db.commit()
    @staticmethod
    def get_activities_byuserid(id, limit = configure.ActivityNUMBER_PER_PAGE, offset = 0):
        c = g.db.cursor()
        c.execute("SELECT * FROM activity ORDER BY create_date DESC LIMIT :limit OFFSET :offset",\
                                {"limit":limit,"offset":offset*limit})
        result = c.fetchall()
        activities = []
        for activity in result:
            tempac = Activity(activity['info'], activity['userid'])
            tempac.create_date = activity['create_date']
            tempac.id = activity['id']
            activities.append(tempac)
        return activities
        
    
        
        
        
        
 
 
 
+create table star_book(
+    userid              INTEGER,
+    bookid              INTEGER,
+    FOREIGN KEY(userid) REFERENCES user(id),
+    PRIMARY KEY(userid, bookid)
+);
 
 
 
 
 
 
-
 @pybookapp.route('/')
 def pybook():
     books = get_books(15, 0)
+    signal('check_started').send(books)
     return render_template('booklist.html',books = books, currentpage = 1, pages = [x for x in xrange(1,10)], generate_time = time.time() - g.time)
 
 @pybookapp.route('/<int:page>')
     return send_file(strIO,
                      attachment_filename=title,
                      as_attachment=True)
+                     
+
 @pybookapp.route('/report')
 def report():
     id = request.form['error']

pybook/pybook.sql

     UNIQUE(ISBN),
     UNIQUE(orginal_url)
 );
-ALTER TABLE pybook ADD postid INTEGER;
+ALTER TABLE pybook ADD postid INTEGER;
+
+

pybook/pymodel.py

         self.download_url = None
         self.book_content = None
         self.postid = None
+        self.started = False
 
     def save(self):
         c = g.pybookdb.cursor()
         result = c.fetchone()
         return result is not None
 
-
-
     def update_postid(self):
         c = g.pybookdb.cursor()
         c.execute("UPDATE pybook SET postid=:postid WHERE id=:id",{'id':self.id,'postid':self.postid})

pybook/templates/booklist.html

                         <td style='width:88px;'>书名:</td>
                         <td style='width:488px;'>
                             <span class='span5'><a href='showbook?id={{book.id}}'>{{book.title}}</a></span>
-                            <span class='span1'><a class="btn btn-small" href=""><i class="icon-star"></i></a></span>
+                            <span class='span1'><a class="btn btn-small {%if book.stared%}btn-warning{%endif%}" href="javascript:void(0)" onclick='star_pybook({{book.id}}, this)'><i class="icon-star"></i></a></span>
                         </td>
                     </tr>
                     <tr>

static/js/main.js

     alert(string);
 
 }
+
+function vote_for_post(postid) {
+    require(['dojo/dom', "dojo/request","dojo/cookie"], function (dom, request, cookie) {
 
+        request.put("/useraction/voteforpost", {
+            data : {
+                postid : postid
+            }
+        }).then(function (text) {
+            console.log(text);
+            if (String(text) == "true") {
+                //notify('yes');
+            }else if(String(text) == "login"){
+                notify('you need to login first');
+            }
+        });
+        
+    });
+    
+}
 
+function star_pybook(bookid, node) {
+    require(['dojo/dom', "dojo/request","dojo/cookie","dojo/dom-class"], function (dom, request, cookie ,domClass) {
 
+        request.put("/useraction/star_pybook", {
+            data : {
+                bookid : bookid
+            }
+        }).then(function (text) {
+            console.log(text);
+            if (String(text) == "true") {
+                domClass.toggle(node, "btn-warning");
+            }else if(String(text) == "login"){
+                notify('you need to login first');
+            }
+        });
+        
+    });
+    
+}
 
 
 
 
 
+
+
+
+
+
+
+
+

templates/news.html

 <ul id="posts" class='unstyled'>
     {%for post in posts%}
         <li>
-        <span class ='span0'><i class="icon-thumbs-up"></i></span>
+        <span class ='span0'><a href='javascript:void(0);' onclick='vote_for_post({{post.id}})'><i class="icon-thumbs-up"></i></a></span>
         {%if not post.des %}
 
         <div class = 'span11'>

usercontroller.py

 @userapp.before_request
 def require_login():
     if 'user' not in session:
+        if request.method == 'PUT':
+            return 'login'
         temp = Message("You need to login first","alert-error")
         temp.showed = False
         session['message'] = temp
         comment = Comment(postid, content, session['user'].id, parentid = parentid)
         comment.save()
         return 'true'
+
+@userapp.route('/voteforpost',methods =['PUT'])
+def voteforpost():
+    postid = request.form['postid']
+    session['user'].vote_for(postid)
+    return 'true'
+
+@userapp.route('/star_pybook',methods =['PUT'])
+def star_pybook():
+    bookid = request.form['bookid']
+    session['user'].toggle_star_book(bookid)
+    return 'true'
+    
 def reset_password():
     pass
 def reset_nickname():
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.