1. Yang Zhang
  2. pyforum

Commits

Yang Zhang  committed 426b73a

fix hot/deadpool page number problem

  • Participants
  • Parent commits 825c267
  • Branches default

Comments (0)

Files changed (3)

File models/topic.py

View file
-# -*- coding: utf-8 *-*
    def __init__(self, title, content):
        self.id = None
        self.title = title
        self.content = content
        self.create_date = None
        self.posterid = session['user'].id if 'user' in session else None
        self.comments_number = 0
        self.last_comment_posterid = None
        self.last_comment_date = None
        self.poster = session.get('user',None)
        self._score = 0
        self.last_commenter = None
        self.is_toggoled = False
    def post(self):
        c = g.db.cursor()
        self.create_date = datetime.datetime.now()
        self.reputation = 0
        c.execute("INSERT INTO topic(title,content,create_date,posterid) VALUES (?,?,?,?)",\
                                (self.title,self.content,self.create_date,self.posterid))
        self.id = c.lastrowid
        g.db.commit()
    @staticmethod
    def _process_topic(result):
        if(result is None):
            return None
        tempost = Topic(result['title'], None)
        tempost.id = result['id']
        tempost.create_date = result['create_date']
        tempost.posterid = result['posterid']
        tempost.content = result['content']
        tempost.last_comment_posterid = result['last_comment_posterid']
        tempost.last_comment_date = result['last_comment_date']
        tempost.comments_number = result['comments_number']
        tempposter = User(result['poster_email'],result['poster_name'],None)
        tempposter.id = tempost.posterid
        tempost.poster = tempposter
        return tempost
    @staticmethod
    def get_post_byid(topicid):
        c = g.db.cursor()
        c.execute("SELECT * FROM topic_view WHERE id=:topicid", {'topicid':topicid})
        result = c.fetchone()
        return Topic._process_topic(result)
    @staticmethod
    def get_topic_created_byuserid(userid, limit = 5, offset = 0):
        #here userid has to be int, so it is safe
        return Topic._get_posts(condition="WHERE posterid="+str(userid),
                                limit=limit,
                                offset=offset)
    @staticmethod
    def get_topic_stared_byuserid(userid, limit = 5, offset = 0):
        #here userid has to be int, so it is safe
        return Topic._get_posts(condition="WHERE id in \
                                    (SELECT topicid FROM star_topic WHERE userid="+str(userid)+")",
                                limit=limit,
                                offset=offset)
    @staticmethod
    def _process_topic_list(results):
        if(results is None):
            return None
        topic_list = []
        for result in results:
            tempost = Topic(result['title'], None)
            tempost.id = result['id']
            tempost.create_date = result['create_date']
            tempost.posterid = result['posterid']
            tempost.last_comment_posterid = result['last_comment_posterid']
            tempost.comments_number = result['comments_number']
            tempost.last_comment_date = result['last_comment_date']
            tempcommenter = User(None,result['commenter_name'],None)
            tempcommenter.id = result['commenter_id']
            tempost.last_commenter = tempcommenter
            tempposter = User(result['poster_email'],result['poster_name'],None)
            tempost.poster = tempposter
            topic_list.append(tempost)
        return  topic_list
    @staticmethod
    def _get_posts(orderby =(('create_date',True),('last_comment_date',True)),condition = "", offset = 0, limit = 15):
        orderstr = ''.join([i[0]+' '+'DESC,' if i[1] else 'ASC'  for i in orderby])[:-1]
        c = g.db.cursor()
        c.execute("SELECT * FROM topic_list_view "+condition+
                    " ORDER BY "+orderstr+
                         " LIMIT :limit OFFSET :offset",\
                                {'limit':limit, 'offset':limit*offset})
        results = c.fetchall()
        return Topic._process_topic_list(results)
    @staticmethod
    def get_hot_posts(offset = 0, limit = 15):
        return Topic._get_posts(orderby=(('score',True),),\
                condition=" WHERE julianday('now') - julianday(create_date) <= 7 ",\
                offset = offset, limit= limit)
    @staticmethod
    def get_deadpool_posts(offset = 0, limit = 15):
        return Topic._get_posts(orderby=(('score',True),),\
                condition=" WHERE julianday('now') - julianday(create_date) > 7 ",\
                offset = offset, limit= limit)
    @staticmethod
    def get_last_posts(offset = 0, limit = 15):
        topiclist = Topic._get_posts(offset = offset, limit= limit)
        topiclist.sort(key=lambda t:t.update_date, reverse = True)
        return topiclist
    def _is_toggoled_by(self,userid):
        c = g.db.cursor()
        c.execute("SELECT 1 FROM star_topic WHERE userid=:userid AND topicid=:topicid",{'userid':userid,'topicid':self.id})
        if(c.fetchone() is None):
            return False
        return True
    @staticmethod
    def check_list_toggled_byuser(userid, topic_list):
        for topic in topic_list:
            if(topic._is_toggoled_by(userid)):
                topic.is_toggoled = True
    def add_comment(self):
        pass
    'only for admin'
    def revise(self):
        pass
    'only for admin'
    def delete(self):
        pass
    @property
    def update_date(self):
        return max(self.create_date,self.last_comment_date) \
                if self.last_comment_date is not None else self.create_date
    def __init__(self,content,topicid):
        self.id = None
        self.content = content
        self.userid = session['user'].id if 'user' in session else None
        self.topicid =topicid
        self.create_date =None
        self.commenter = session.get('user', None)
    def save(self):
        c = g.db.cursor()
        self.create_date = datetime.datetime.now()
        c.execute("INSERT INTO comment(content,topicid,userid,create_date) VALUES (?,?,?,?)",\
                                (self.content,self.topicid,self.userid,self.create_date))
        self.id = c.lastrowid
        g.db.commit()
    @staticmethod
    def get_comment_bytopicid(topicid, offset =0, limit = 50):
        c = g.db.cursor()
        c.execute("SELECT * FROM comment_view WHERE topicid=:topicid \
        ORDER BY create_date ASC LIMIT :limit OFFSET :offset ", {'topicid':topicid,'limit':limit,'offset':limit*offset})
        results = c.fetchall()
        if(results is None):
            return None
        comment_list = []
        for result in results:
            tempcomment = Comment(result['content'], topicid)
            tempcomment.create_date = result['create_date']
            tempuser = User(result['email'],result['nickname'],None)
            tempcomment.commenter = tempuser
            comment_list.append(tempcomment)
        return comment_list
    @staticmethod
    def get_comment_byuserid(userid, offset =0, limit = 5):
        c = g.db.cursor()
        c.execute("SELECT topicid,content,create_date FROM comment WHERE userid=:userid \
                    ORDER BY create_date ASC LIMIT :limit OFFSET :offset ", \
                    {'userid':userid,'limit':limit,'offset':limit*offset})
        results = c.fetchall()
        if(results is None):
            return None
        comment_list = []
        for result in results:
            topicid = result['topicid']
            tempcomment = Comment(result['content'],topicid)
            tempcomment.create_date = result['create_date']
            tempcomment.topic = Topic.get_post_byid(topicid)
            comment_list.append(tempcomment)
        return comment_list
+# -*- coding: utf-8 *-*
    def __init__(self, title, content):
        self.id = None
        self.title = title
        self.content = content
        self.create_date = None
        self.posterid = session['user'].id if 'user' in session else None
        self.comments_number = 0
        self.last_comment_posterid = None
        self.last_comment_date = None
        self.poster = session.get('user',None)
        self._score = 0
        self.last_commenter = None
        self.is_toggoled = False
    def post(self):
        c = g.db.cursor()
        self.create_date = datetime.datetime.now()
        self.reputation = 0
        c.execute("INSERT INTO topic(title,content,create_date,posterid) VALUES (?,?,?,?)",\
                                (self.title,self.content,self.create_date,self.posterid))
        self.id = c.lastrowid
        g.db.commit()
    @staticmethod
    def _process_topic(result):
        if(result is None):
            return None
        tempost = Topic(result['title'], None)
        tempost.id = result['id']
        tempost.create_date = result['create_date']
        tempost.posterid = result['posterid']
        tempost.content = result['content']
        tempost.last_comment_posterid = result['last_comment_posterid']
        tempost.last_comment_date = result['last_comment_date']
        tempost.comments_number = result['comments_number']
        tempposter = User(result['poster_email'],result['poster_name'],None)
        tempposter.id = tempost.posterid
        tempost.poster = tempposter
        return tempost
    @staticmethod
    def get_post_byid(topicid):
        c = g.db.cursor()
        c.execute("SELECT * FROM topic_view WHERE id=:topicid", {'topicid':topicid})
        result = c.fetchone()
        return Topic._process_topic(result)
    @staticmethod
    def get_topic_created_byuserid(userid, limit = 5, offset = 0):
        #here userid has to be int, so it is safe
        return Topic._get_posts(condition="WHERE posterid="+str(userid),
                                limit=limit,
                                offset=offset)
    @staticmethod
    def get_topic_stared_byuserid(userid, limit = 5, offset = 0):
        #here userid has to be int, so it is safe
        return Topic._get_posts(condition="WHERE id in \
                                    (SELECT topicid FROM star_topic WHERE userid="+str(userid)+")",
                                limit=limit,
                                offset=offset)
    @staticmethod
    def _process_topic_list(results):
        if(results is None):
            return None
        topic_list = []
        for result in results:
            tempost = Topic(result['title'], None)
            tempost.id = result['id']
            tempost.create_date = result['create_date']
            tempost.posterid = result['posterid']
            tempost.last_comment_posterid = result['last_comment_posterid']
            tempost.comments_number = result['comments_number']
            tempost.last_comment_date = result['last_comment_date']
            tempcommenter = User(None,result['commenter_name'],None)
            tempcommenter.id = result['commenter_id']
            tempost.last_commenter = tempcommenter
            tempposter = User(result['poster_email'],result['poster_name'],None)
            tempost.poster = tempposter
            topic_list.append(tempost)
        return  topic_list
    @staticmethod
    def _get_posts(orderby =(('create_date',True),('last_comment_date',True)),condition = "", offset = 0, limit = 15):
        orderstr = ''.join([i[0]+' '+'DESC,' if i[1] else 'ASC'  for i in orderby])[:-1]
        c = g.db.cursor()
        c.execute("SELECT * FROM topic_list_view "+condition+
                    " ORDER BY "+orderstr+
                         " LIMIT :limit OFFSET :offset",\
                                {'limit':limit, 'offset':limit*offset})
        results = c.fetchall()
        return Topic._process_topic_list(results)
    @staticmethod
    def get_hot_counter():
        c = g.db.cursor()
        c.execute("SELECT COUNT(id) AS counter FROM topic_list_view \
                    WHERE julianday('now') - julianday(create_date) <= 7")
        result = c.fetchone()
        return result['counter']
    @staticmethod
    def get_deadpool_counter():
        c = g.db.cursor()
        c.execute("SELECT COUNT(id) AS counter FROM topic_list_view \
                    WHERE julianday('now') - julianday(create_date) > 7")
        result = c.fetchone()
        return result['counter']
        
    @staticmethod
    def get_hot_posts(offset = 0, limit = 15):
        return Topic._get_posts(orderby=(('score',True),),\
                condition=" WHERE julianday('now') - julianday(create_date) <= 7 ",\
                offset = offset, limit= limit)
    @staticmethod
    def get_deadpool_posts(offset = 0, limit = 15):
        return Topic._get_posts(orderby=(('score',True),),\
                condition=" WHERE julianday('now') - julianday(create_date) > 7 ",\
                offset = offset, limit= limit)
    @staticmethod
    def get_last_posts(offset = 0, limit = 15):
        topiclist = Topic._get_posts(offset = offset, limit= limit)
        topiclist.sort(key=lambda t:t.update_date, reverse = True)
        return topiclist
    def _is_toggoled_by(self,userid):
        c = g.db.cursor()
        c.execute("SELECT 1 FROM star_topic WHERE userid=:userid AND topicid=:topicid",{'userid':userid,'topicid':self.id})
        if(c.fetchone() is None):
            return False
        return True
    @staticmethod
    def check_list_toggled_byuser(userid, topic_list):
        for topic in topic_list:
            if(topic._is_toggoled_by(userid)):
                topic.is_toggoled = True
    def add_comment(self):
        pass
    'only for admin'
    def revise(self):
        pass
    'only for admin'
    def delete(self):
        pass
    @property
    def update_date(self):
        return max(self.create_date,self.last_comment_date) \
                if self.last_comment_date is not None else self.create_date
    def __init__(self,content,topicid):
        self.id = None
        self.content = content
        self.userid = session['user'].id if 'user' in session else None
        self.topicid =topicid
        self.create_date =None
        self.commenter = session.get('user', None)
    def save(self):
        c = g.db.cursor()
        self.create_date = datetime.datetime.now()
        c.execute("INSERT INTO comment(content,topicid,userid,create_date) VALUES (?,?,?,?)",\
                                (self.content,self.topicid,self.userid,self.create_date))
        self.id = c.lastrowid
        g.db.commit()
    @staticmethod
    def get_comment_bytopicid(topicid, offset =0, limit = 50):
        c = g.db.cursor()
        c.execute("SELECT * FROM comment_view WHERE topicid=:topicid \
        ORDER BY create_date ASC LIMIT :limit OFFSET :offset ", {'topicid':topicid,'limit':limit,'offset':limit*offset})
        results = c.fetchall()
        if(results is None):
            return None
        comment_list = []
        for result in results:
            tempcomment = Comment(result['content'], topicid)
            tempcomment.create_date = result['create_date']
            tempuser = User(result['email'],result['nickname'],None)
            tempcomment.commenter = tempuser
            comment_list.append(tempcomment)
        return comment_list
    @staticmethod
    def get_comment_byuserid(userid, offset =0, limit = 5):
        c = g.db.cursor()
        c.execute("SELECT topicid,content,create_date FROM comment WHERE userid=:userid \
                    ORDER BY create_date ASC LIMIT :limit OFFSET :offset ", \
                    {'userid':userid,'limit':limit,'offset':limit*offset})
        results = c.fetchall()
        if(results is None):
            return None
        comment_list = []
        for result in results:
            topicid = result['topicid']
            tempcomment = Comment(result['content'],topicid)
            tempcomment.create_date = result['create_date']
            tempcomment.topic = Topic.get_post_byid(topicid)
            comment_list.append(tempcomment)
        return comment_list

File templates/index.html

View file
             <label style='text-align: center;'>该栏目暂时没有主题...</label>
             
         {%else%}
-        {% set total_page = (counter.topic_number-1)//15+1 %}
+        {% set total_page = total_page or (counter.topic_number-1)//15+1 %}
         <ul class="pager span11">
           
             {%if current_page ==1%}

File views/frontend.py

View file
 def hot(page=1):
     page_url = url_for("frontend.hot")
     topic_list = Topic.get_hot_posts(offset = page-1)
+    total_counter = Topic.get_hot_counter()
     if('user' in session):
         Topic.check_list_toggled_byuser(session['user'].id,topic_list)
     user = ini_user()
                            label = 'hot',
                            user=user,
                            current_page = page,
+                           total_page = (int(total_counter)-1)//15+1,
                            page_url = page_url)
 
 @frontend.route("/latest")
 def deadpool(page=1):
     page_url = url_for("frontend.deadpool")
     topic_list = Topic.get_deadpool_posts(offset = page-1)
+    total_counter = Topic.get_deadpool_counter()
     if('user' in session):
         Topic.check_list_toggled_byuser(session['user'].id,topic_list)
     user = ini_user()
                            topic_list = topic_list,
                            label = 'deadpool',
                            user=user,
+                           total_page = (int(total_counter)-1)//15+1,
                            current_page = page,
                            page_url = page_url)