Yang Zhang avatar Yang Zhang committed a57e719

user can see others' commented topic

Comments (0)

Files changed (5)

   END;
 
 
-
-
-
-
-
-
-
-
-# -*- 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 get_post_byid(topicid):
        c = g.db.cursor()
        c.execute("SELECT * FROM topic_view WHERE id=:topicid", {'topicid':topicid})
        result = c.fetchone()
        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)
        tempost.poster = tempposter
        return tempost
    @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:
            tempcomment = Comment(result['content'], result['topicid'])
            tempcomment.create_date = result['create_date']
            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_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

templates/user.html

             {%if comment_list|length!=0%}
                 {%for comment in comment_list%}
                     {%if loop.index!=1%}<br/>{%endif%}
+                    {%set poster = comment.topic.poster%}
+                    
                 <div class='span11 offset1'>
-                        <strong>在谁谁创建的******主题中回复到:</strong>
+                    <strong>在<a href='/user/{{poster.id}}'>{{poster.nickname}}</a>创建的<a href='/topic/{{comment.topic.id}}#{{comment.topic.comments_number}}'>{{comment.topic.title}}</a>中回复到:</strong>
                 </div>
                 <div class='comment bottom-divider'>
                      <div class='span1'>
                 </div>
                 {%endfor%}
                 {%if comment_list|length ==5%}
-                    <a class='muted span12'href='/' style='text-align:center'>查看更多</a>
+                    <a class='muted span12'href='{{user.id}}/comments' style='text-align:center'>查看更多</a>
                 {%endif%}
             {%else%}
                 <label style='text-align:center'>该用户暂未评论任何主题</label>

templates/user_commented_topic.html

+{%extends 'base.html'%}
+
+{%block content%}
+<div class='container'>
+<div class='container-box span8 offset2'>
+    {%block sub_header%}
+    <h3><a href='/user/{{user.id}}'>{{user.nickname}}</a>回复的主题</h3>
+    {%endblock%}
+
+            {%for comment in comment_list%}
+                {%if loop.index!=1%}<br/>{%endif%}
+                {%set poster = comment.topic.poster%}
+                
+            <div class='span11 offset1'>
+                <strong>在<a href='/user/{{poster.id}}'>{{poster.nickname}}</a>创建的<a href='/topic/{{comment.topic.id}}#{{comment.topic.comments_number}}'>{{comment.topic.title}}</a>中回复到:</strong>
+            </div>
+            <div class='comment bottom-divider'>
+                 <div class='span1'>
+                    <a href="#" class="thumbnail avatar">
+                      {%if user.email%}
+                        <img src="{{ user.email | gravatar }}" alt="">
+                      {%else%}
+                        <img src="http://placehold.it/200x200" alt=""/>
+                      {%endif%}
+                    </a>
+                </div>
+                
+                <div class='row-fluid offset1' style='margin-top: 5px;'>
+                    <div class='span10' style='margin-left: 0px;word-wrap: break-word;'>
+                        {{comment.content}}
+                    </div>
+                    <div class='muted span10' style='margin-left: 0;'>
+                        <small>
+                            <a  href='/user/{{user.id}}'>{{user.nickname}}</a>
+                            发表于{{comment.create_date|time_since}}
+                        </small>
+                    </div>
+                </div>
+                
+            </div>
+            {%endfor%}
+
+    <div class="row-fluid">
+        {%if comment_list|length==0%}
+            <label style='text-align: center;'>该栏目暂时没有主题...</label>
+
+        {%else%}
+        {% set total_page = (counter-1)//15+1 %}
+        <ul class="pager span11">
+
+            {%if current_page ==1%}
+            <li class="previous disabled">
+            <a href="javascript:void(0)">&larr; 上一页</a>
+            </li>
+            {%else%}
+            <li class="previous">
+            <a href="{{page_url}}/{{current_page-1}}">&larr; 上一页</a>
+            </li>
+            {%endif%}
+
+          {%if current_page ==total_page%}
+              <li class="next disabled">
+                <a href="javascript:void(0)">下一页 &rarr;</a>
+              </li>
+          {%else%}
+            <li class="next">
+                <a href="{{page_url}}/{{current_page+1}}">下一页 &rarr;</a>
+            </li>
+          {%endif%}
+          <li>
+            <a href="javascript:void(0)">{{current_page}} / {{total_page}}</a>
+          </li>
+        </ul>
+
+
+        {%endif%}
+    </div>
+
+</div></div>
+
+
+{%endblock%}
+
+
+
+
     return render_template('user_started_topics.html',
                         user = user,
                         topic_list = stared_topic, 
-                        counter=user.created_topic_counter, 
+                        counter=user.star_counter, 
                         page_url = page_url,
                         current_page = page)
 
 @user.route('/<int:userid>/comments')
 @user.route('/<int:userid>/comments/<int:page>/')
 def show_user_comments(userid,page=1):
-    pass
+    user = User.get_user_by_id(userid)
+    page_url = url_for("user.show_user_comments", userid= userid)
+    comment_list = Comment.get_comment_byuserid(user.id, limit = 15, offset = page-1)
+    for comment in comment_list:
+        comment.content = make_markdown(comment.content)
+    return render_template('user_commented_topic.html',
+                        user = user,
+                        comment_list = comment_list, 
+                        counter=user.comment_counter, 
+                        page_url = page_url,
+                        current_page = page)
 
 
 
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.