Commits

Yang Zhang committed ff29c9e

add admin and snippet sketch, change the algorithm to order news

  • Participants
  • Parent commits 3e518de

Comments (0)

Files changed (11)

File admin/__init__.py

Empty file added.

File admin/admin.py

Empty file added.

File application.py

 from server_session import SqliteSessionInterface
 from configure import configure,Configure,Message
 from pybook import pybookapp,cache
+from snippet import snippetapp
 import datetime
 from usercontroller import userapp
 import base64
 app.secret_key = '\x1e*\xa7\xe0\xab\x10\xab\xfcxU|4\xect\xbe\xb3\x8d?B\x99e\xd1\xf0\xe5'
 app.register_blueprint(userapp, url_prefix='/useraction')
 app.register_blueprint(pybookapp, url_prefix='/pybook')
+app.register_blueprint(snippetapp, url_prefix='/snippet')
 #app.register_blueprint(sphapp, url_prefix='/serverpush')
 #app.register_blueprint(adminapp, url_prefix='/admin')
 #mail = Mail(app)
 def hotposts():
     page = request.args.get('page',1)
     posts = Post.get_hot_posts(offset = int(page)-1)
-    posts.sort(key=lambda tpost:tpost.score, reverse=True)
+    posts.sort(key=lambda tpost:tpost.score + tpost.comments_number, reverse=True)
     return render_template('news.html',posts = posts, nextpage = int(page)+1,currenturl = request.url,news_type='news_hot')
 
 @app.route('/deadpool')
 def deadpool():
     page = request.args.get('page',1)
     posts = Post.get_deadpool_posts(offset = int(page)-1)
-    posts.sort(key=lambda tpost:tpost.score, reverse=True)
+    posts.sort(key=lambda tpost:tpost.score+ tpost.comments_number, reverse=True)
     return render_template('news.html',posts = posts, nextpage = int(page)+1,currenturl = request.url,news_type='news_deadpool')
 
 @app.route('/latest')

File news/model.py

-# -*- 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
        
    
        
        
        
        
+# -*- 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
        
        self._score = None
        self._comments_number = 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):
        if self._score is not None:
            return self._score
        c = g.db.cursor()
        c.execute("SELECT popularity FROM vote WHERE postid=:id",{'id':self.id})
        scores = [x['popularity'] for x in c.fetchall()]
        self._score = sum(scores)
        return self._score
    @property
    def comments_number(self):
        if self._comments_number is not None:
            return self._comments_number
        c = g.db.cursor()
        c.execute("SELECT COUNT(*) AS number FROM comment WHERE postid=:id",{'id':self.id})
        result = c.fetchone()
        self._comments_number = result['number'] if result else 0
        return self._comments_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
        
    
        
        
        
        

File pybook/templates/booklist.html

                 <div class='bookinfo'>
                 <table>
                     <tr>
-                        <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 {%if book.stared%}btn-warning{%endif%}" href="javascript:void(0)" onclick='star_pybook({{book.id}}, this)'><i class="icon-star"></i></a></span>
+                        <td style='width:88px;'>Title:</td>
+                        <td style='width:508px;'>
+                            <a href='showbook?id={{book.id}}'>{{book.title}}</a>
+                            <a class="btn btn-small {%if book.stared%}btn-warning{%endif%} pull-right" title='star'href="javascript:void(0)" onclick='star_pybook({{book.id}}, this)'><i class="icon-star"></i></a>
                         </td>
                     </tr>
                     <tr>
-                        <td>作者:</td>
-                        <td><span class='span5'>{{book.author}}</span></td>
+                        <td>Author:</td>
+                        <td>{{book.author}}
+                            <a href='download?id={{book.id}}' class='btn btn-small pull-right' title='download'><i class='icon-download-alt'></i></a>
+                        </td>
                     </tr>
-                    {%if book.subtitle%}
+                    
                     <tr>
-                        <td>副标题:</td>
-                        <td>{{book.subtitle}}</td>
+                        <td>Subtitle:</td>
+                        <td>{%if book.subtitle%}{{book.subtitle}}{%endif%}
+                            <a href='showbook?id={{book.id}}' class='btn btn-small pull-right' title='comment'><i class='icon-comment'></i></a>
+                        </td>
                     </tr>
-                    {%endif%}
+                    
                     <tr>
-                        <td>出版日期:</td>
-                        <td>{{book.date}}</td>
+                        <td>Date:</td>
+                        <td>{{book.date}} 
+                            <a href='report?id={{book.id}}' class='btn btn-small pull-right' title='error report'><i class=' icon-warning-sign'></i></a>
+                        </td>
                     </tr>
                     <tr>
-                        <td>出版商:</td>
-                        <td>{{book.publisher}}</td>
+                        <td>Publisher:</td>
+                        <td>{{book.publisher}}
+                            
+                        </td>
                     </tr>
                     <tr>
-                        <td>页数:</td>
+                        <td>Pages:</td>
                         <td>{{book.number_of_page}}</td>
                     </tr>
                     <tr>
                     </tr>
                 </table>
                 </div>
+        {#
         <td style='border:none'>
             <p><a href='download?id={{book.id}}' class='btn btn-primary btn-large'><i class='icon-download-alt icon-white'></i> Download</a></p>
             <a href='report?id={{book.id}}' class='btn btn-danger btn-small reporterror'>Report Error</a>
         </td>
+        #}
 
         </td></tr>
     {%endfor%}
 
 {% block extra_info %}
 <span class='span4 offset3' style='margin-top:10px'>
-<a href='' class ='text-warning'>下载文件出错解决办法</a>
+<a href='' class ='text-warning'>Download Error Solutions</a>
 </span>
 
 {% endblock %}

File snippet/__init__.py

+from .snippet import snippetapp

File snippet/model.py

+
    def __init__(self,id,content):
        self.id = id
        self.content = content
        self.visible_level = 'public'
    def save(self):
        pass
    @staticmethod
    def get_content_byid(id):
        pass
    

File snippet/snippet.py

+from flask import Blueprint, render_template,session,request,redirect
    return render_template('snippet.html')

File snippet/templates/snippet.html

+{% extends "base.html" %}
    <h1> Have Not Implemented Yet</h1>
  </div>
     $("#nav_snippet").addClass("active");

File templates/static_header.html

 {%else%}
     <div class="btn-group">
         <a class="dropdown-toggle" data-toggle="dropdown">
-        <span class="badge badge-important pull-left" style=' margin-top:18px;padding: 5px 8px;' >0</span>
+        <span class="badge badge-inverse !badge-important pull-left" style=' margin-top:18px;padding: 5px 8px;' >0</span>
         </a>
         <ul class="dropdown-menu" style="margin: 50px 0px 0 -90px;width: 200px;">
             <li><a>Message1</a></li>
     </div>
 
      <div class="btn-group span2" style="margin: 15px;">
-      <a class="btn btn-primary" href="#">
-            <i class="icon-user icon-white"></i> {{user.nickname}}
+      <a class="btn" href="#">
+            <i class="icon-user"></i> {{user.nickname}}
       </a>
-      <a class="btn btn-primary dropdown-toggle" data-toggle="dropdown" href="#"><span class="caret"></span></a>
+      <a class="btn dropdown-toggle" data-toggle="dropdown" href="#"><span class="caret"></span></a>
       <ul class="dropdown-menu">
         <li><a href="/useraction/newpost"><i class="icon-pencil"></i> Submit news</a></li>
+        <li><a href="/"><i class="icon-wrench"></i> Setting</a></li>
         <li class="divider"></li>
         <li><a href="/logout"><i class="icon-off"></i> Logout</a></li>
       </ul>

File templates/static_navigation.html

               <li class="divider-vertical"></li>
               <li id="nav_book"><a href="/pybook/"><i class="icon-book"></i> Book</a></li>
               <li class="divider-vertical"></li>
-              <li id="nav_book"><a href="/snippet/"><i class="icon-lock"></i> Code Snippet</a></li>
+              <li id="nav_snippet"><a href="/snippet/"><i class="icon-lock"></i> Code Snippet</a></li>
               <li class="divider-vertical"></li>
             </ul>
             <form method="GET" action="https://www.google.com/search?" class="navbar-search pull-right">
               <i src="" class="icon-search"></i>
               <input type="text" class="search-query" placeholder="search" name="q">
-              <input value="site:py4u.com/pybook/" name ="q" type=hidden />
+              <input value="site:py4u.com" name ="q" type=hidden />
             </form>
         </div>
 </div></div>