Browse Source

增加帖子置顶功能

honmaple 8 years ago
parent
commit
3177155794

+ 12 - 6
maple/board/views.py

@@ -6,7 +6,7 @@
 # Author: jianglin
 # Email: xiyang0807@gmail.com
 # Created: 2016-06-03 14:32:06 (CST)
-# Last Update:星期五 2016-6-17 14:0:47 (CST)
+# Last Update:星期六 2016-6-25 17:52:50 (CST)
 #          By:
 # Description:
 # **************************************************************************
@@ -39,12 +39,18 @@ def board(child_b):
     page = is_num(request.args.get('page'))
     if child_b is None:
         boards = Board.query.filter_by(parent_board=g.parent_b).all()
-        topics = Topic.query.join(Topic.board).filter(
-            Board.parent_board == g.parent_b).paginate(page, 20, True)
-        data = {'boards': boards, 'topics': topics}
+        topic_base = Topic.query.join(Topic.board).filter(Board.parent_board ==
+                                                          g.parent_b)
+        topics = topic_base.filter(Topic.is_top == False).paginate(page, 20,
+                                                                   True)
+        top_topics = topic_base.filter(Topic.is_top == True).limit(5).all()
+        data = {'boards': boards, 'topics': topics, 'top_topics': top_topics}
         return render_template('forums/board_list.html', **data)
     else:
         board = Board.query.filter_by(board=child_b).first_or_404()
-        topics = board.topics.paginate(page, 20, True)
-        data = {'board': board, 'topics': topics}
+        topic_base = board.topics
+        topics = topic_base.filter(Topic.is_top == False).paginate(page, 20,
+                                                                   True)
+        top_topics = topic_base.filter(Topic.is_top == True).limit(5).all()
+        data = {'board': board, 'topics': topics, 'top_topics': top_topics}
         return render_template('forums/board.html', **data)

+ 5 - 5
maple/forums/views.py

@@ -6,7 +6,7 @@
 # Author: jianglin
 # Email: xiyang0807@gmail.com
 # Created: 2016-05-20 13:18:19 (CST)
-# Last Update:星期日 2016-6-19 16:6:42 (CST)
+# Last Update:星期六 2016-6-25 18:4:14 (CST)
 #          By:
 # Description:
 # **************************************************************************
@@ -23,10 +23,11 @@ site = Blueprint('forums', __name__)
 
 @site.route('/', methods=['GET'])
 def index():
-    topics = Topic.query.filter_by(is_good=True).paginate(1, 10)
+    topics = Topic.query.filter_by(is_good=True, is_top=False).paginate(1, 10)
+    top_topics = Topic.query.filter_by(is_top=True).limit(5).all()
     if not topics.items:
         topics = Topic.query.paginate(1, 10)
-    data = {'topics': topics}
+    data = {'topics': topics, 'top_topics': top_topics}
     return render_template('forums/index.html', **data)
 
 
@@ -48,8 +49,7 @@ def forums():
 def notice(page):
     notices = Notice.query.join(Notice.rece_user).filter(
         User.username == current_user.username).paginate(
-            page,
-            app.config['PER_PAGE'],
+            page, app.config['PER_PAGE'],
             error_out=True)
     return render_template('forums/notice.html', notices=notices)
 

+ 32 - 42
maple/main/orderby.py

@@ -6,7 +6,7 @@
 # Author: jianglin
 # Email: xiyang0807@gmail.com
 # Created: 2016-06-05 10:15:58 (CST)
-# Last Update:星期二 2016-6-14 23:20:14 (CST)
+# Last Update:星期六 2016-6-25 18:30:13 (CST)
 #          By:
 # Description:
 # **************************************************************************
@@ -53,79 +53,69 @@ def form_sort(t1, t2, t3):
 
     # 发表时间
     if t2 == 0:
+        topic_base = Topic.query.join(Topic.board).filter(
+            Topic.publish > time, Topic.is_top == False)
         if t3 == 0:
             if type == 'parent_b':
-                topics = Topic.query.join(Topic.board).filter(
-                    Topic.publish > time, Board.parent_board ==
-                    uid).order_by(Topic.publish.desc()).paginate(page, 20,
-                                                                 True)
+                topics = topic_base.filter(Board.parent_board == uid).paginate(
+                    page, 20, True)
             elif type == 'child_b':
-                topics = Topic.query.join(Topic.board).filter(
-                    Topic.publish > time,
-                    Board.id == uid).order_by(Topic.publish.desc()).paginate(
-                        page, 20, True)
+                topics = topic_base.filter(Board.id == uid).paginate(page, 20,
+                                                                     True)
             elif type == 'tags':
                 topics = Topic.query.join(Topic.tags).filter(
-                    Topic.publish > time, Tags.tagname ==
-                    uid).order_by(Topic.publish.desc()).paginate(page, 20,
-                                                                 True)
+                    Topic.publish > time, Topic.is_top == False, Tags.tagname
+                    == uid).order_by(Topic.publish.desc()).paginate(page, 20,
+                                                                    True)
             else:
                 topics = None
             return topics
         else:
             if type == 'parent_b':
-                topics = Topic.query.join(Topic.board).filter(
-                    Topic.publish > time, Board.parent_board ==
-                    uid).order_by(Topic.publish.asc()).paginate(page, 20, True)
+                topics = topic_base.filter(Board.parent_board == uid).order_by(
+                    Topic.publish.asc()).paginate(page, 20, True)
             elif type == 'child_b':
-                topics = Topic.query.join(Topic.board).filter(
-                    Topic.publish > time,
-                    Board.id == uid).order_by(Topic.publish.asc()).paginate(
-                        page, 20, True)
+                topics = topic_base.filter(Board.id == uid).order_by(
+                    Topic.publish.asc()).paginate(page, 20, True)
             elif type == 'tags':
                 topics = Topic.query.join(Topic.tags).filter(
-                    Topic.publish > time, Tags.tagname ==
+                    Topic.publish > time, Topic.is_top == False,
+                    Tags.tagname ==
                     uid).order_by(Topic.publish.asc()).paginate(page, 20, True)
             else:
                 topics = None
             return topics
     # 作者
     else:
+        topic_base = Topic.query.join(Topic.board).filter(
+            Topic.publish > time, Topic.is_top == False)
         if t3 == 0:
             if type == 'parent_b':
-                topics = Topic.query.join(Topic.board).filter(
-                    Topic.publish > time, Board.parent_board ==
-                    uid).order_by(Topic.author_id.desc()).paginate(page, 20,
-                                                                   True)
+                topics = topic_base.filter(Board.parent_board == uid).order_by(
+                    Topic.author_id.desc()).paginate(page, 20, True)
             elif type == 'child_b':
-                topics = Topic.query.join(Topic.board).filter(
-                    Topic.publish > time,
-                    Board.id == uid).order_by(Topic.author_id.desc()).paginate(
-                        page, 20, True)
+                topics = topic_base.filter(Board.id == uid).order_by(
+                    Topic.author_id.desc()).paginate(page, 20, True)
             elif type == 'tags':
                 topics = Topic.query.join(Topic.tags).filter(
-                    Topic.publish > time, Tags.tagname ==
-                    uid).order_by(Topic.author_id.desc()).paginate(page, 20,
-                                                                   True)
+                    Topic.publish > time, Topic.is_top == False, Tags.tagname
+                    == uid).order_by(Topic.author_id.desc()).paginate(page, 20,
+                                                                      True)
             else:
                 topics = None
             return topics
         else:
             if type == 'parent_b':
-                topics = Topic.query.join(Topic.board).filter(
-                    Topic.publish > time, Board.parent_board ==
-                    uid).order_by(Topic.author_id.asc()).paginate(page, 20,
-                                                                  True)
+                topics = topic_base.filter(Board.parent_board == uid).order_by(
+                    Topic.author_id.asc()).paginate(page, 20, True)
             elif type == 'child_b':
-                topics = Topic.query.join(Topic.board).filter(
-                    Topic.publish > time,
-                    Board.id == uid).order_by(Topic.author_id.asc()).paginate(
-                        page, 20, True)
+                topics = topic_base.filter(Board.id == uid).order_by(
+                    Topic.author_id.asc()).paginate(page, 20, True)
             elif type == 'tags':
                 topics = Topic.query.join(Topic.tags).filter(
-                    Topic.publish > time, Tags.tagname ==
-                    uid).order_by(Topic.author_id.asc()).paginate(page, 20,
-                                                                  True)
+                    Topic.publish > time, Topic.is_top == False, Tags.tagname
+                    == uid).order_by(Topic.author_id.asc()).paginate(page, 20,
+                                                                     True)
             else:
                 topics = None
             return topics

+ 10 - 10
maple/tag/views.py

@@ -6,7 +6,7 @@
 # Author: jianglin
 # Email: xiyang0807@gmail.com
 # Created: 2016-05-20 13:18:19 (CST)
-# Last Update:星期日 2016-6-19 16:51:32 (CST)
+# Last Update:星期六 2016-6-25 17:59:5 (CST)
 #          By:
 # Description:
 # **************************************************************************
@@ -19,7 +19,6 @@ from urllib.parse import urljoin
 from werkzeug.utils import escape
 from werkzeug.contrib.atom import AtomFeed
 
-
 site = Blueprint('tag', __name__)
 
 
@@ -32,18 +31,19 @@ def tag(tag):
         return render_template('forums/tag_list.html', **data)
     else:
         page = is_num(request.args.get('page'))
-        topics = Topic.query.join(Topic.tags).filter(
-            Tags.tagname == tag).paginate(page,
-                                          app.config['PER_PAGE'],
-                                          error_out=True)
+        topic_base = Topic.query.join(Topic.tags).filter(Tags.tagname == tag)
+        topics = topic_base.filter(Topic.is_top == False).paginate(
+            page, app.config['PER_PAGE'],
+            error_out=True)
+        top_topics = topic_base.filter(Topic.is_top == True).limit(5).all()
         tag = Tags.query.filter_by(tagname=tag).first_or_404()
-        data = {'tag': tag, 'topics': topics}
+        data = {'tag': tag, 'topics': topics, 'top_topics': top_topics}
         return render_template('forums/tag.html', **data)
 
 
 @site.route('/<tag>/feed')
 def rss(tag):
-    feed = AtomFeed('%s·HonMaple社区'%tag,
+    feed = AtomFeed('%s·HonMaple社区' % tag,
                     feed_url=request.url,
                     url=request.url_root,
                     subtitle='I like solitude, yearning for freedom')
@@ -52,8 +52,8 @@ def rss(tag):
     for topic in topics:
         feed.add(
             topic.title,
-            escape(Filters.safe_markdown(topic.content)
-            if topic.is_markdown else topic.content),
+            escape(Filters.safe_markdown(topic.content) if topic.is_markdown
+                   else topic.content),
             content_type='html',
             author=topic.author.username,
             url=urljoin(request.url_root,

+ 1 - 0
maple/templates/base/sort.html

@@ -1 +1,2 @@
+{% set topics = topics.items %}
 {% include 'topic/topic_list_base.html' %}

+ 1 - 1
maple/templates/forums/board.html

@@ -15,7 +15,7 @@
     <div class="col-md-9">
         <div class="panel panel-default">
             {% from 'topic/topic_list.html' import view as board_view %}
-            {{ board_view(topics)}}
+            {{ board_view(topics,top_topics)}}
             {{ p_footer(topics,'board.board',dict(child_b=board.board))}}
         </div>
     </div>

+ 1 - 1
maple/templates/forums/board_list.html

@@ -23,7 +23,7 @@
     <div class="col-md-9">
         <div class="panel panel-default">
             {% from 'topic/topic_list.html' import view as board_view %}
-            {{ board_view(topics)}}
+            {{ board_view(topics,top_topics)}}
             {{ p_footer(topics,'board.board')}}
         </div>
     </div>

+ 1 - 1
maple/templates/forums/index.html

@@ -45,7 +45,7 @@
     <div class="col-md-9">
         <div class="panel panel-default">
             {% from 'topic/topic_list.html' import view_no_form as board_view %}
-            {{ board_view(topics)}}
+            {{ board_view(topics,top_topics)}}
             <div class="panel-footer clearfix">
                 <a class="pull-right" href="{{ url_for('topic.good')}}">
                     查看更多精华帖

+ 1 - 1
maple/templates/forums/tag.html

@@ -29,7 +29,7 @@
                 </span>
             </div>
             {% from 'topic/topic_list.html' import view as board_view %}
-            {{ board_view(topics)}}
+            {{ board_view(topics,top_topics)}}
             {{ p_footer(topics, 'tag.tag',kw=dict(tag=tag.tagname))}}
         </div>
     </div>

+ 1 - 1
maple/templates/topic/topic.html

@@ -15,7 +15,7 @@
     <div class="col-md-9">
         <div class="panel panel-default">
             {% from 'topic/topic_list.html' import view as board_view %}
-            {{ board_view(topics)}}
+            {{ board_view(topics,top_topics)}}
             {{ p_footer(topics,'topic.topic')}}
         </div>
     </div>

+ 22 - 2
maple/templates/topic/topic_list.html

@@ -1,4 +1,4 @@
-{% macro view(topics) -%}
+{% macro view(topics,top_topics) -%}
 <style>
  select {
      background-color: #fff;
@@ -10,12 +10,26 @@
 <div class="panel-heading" style="font-size:12px;">
     {{ form() }}
 </div>
+{% set ts = topics.items %}
+<div class="top-topiclist">
+    {% set topics = top_topics %}
+    {% include 'topic/topic_list_base.html' %}
+</div>
 <div class="topiclist">
+    {% set topics = ts %}
+    {% if not topics and not top_topics %}
+    <div class="panel-body">
+        <span class="text-center" style="display:block;width:100%;color:#999">
+            没有帖子
+        </span>
+    </div>
+    {% else %}
     {% include 'topic/topic_list_base.html' %}
+    {% endif %}
 </div>
 {%- endmacro %}
 
-{% macro view_no_form(topics) -%}
+{% macro view_no_form(topics,top_topics) -%}
 <div class="panel-heading" style="font-size:12px;">
     <div class="row">
         <div class="col-sm-6">
@@ -32,7 +46,13 @@
         </div>
     </div>
 </div>
+{% set ts = topics.items %}
+<div class="top-topiclist">
+    {% set topics = top_topics %}
+    {% include 'topic/topic_list_base.html' %}
+</div>
 <div class="topiclist">
+    {% set topics = ts %}
     {% include 'topic/topic_list_base.html' %}
 </div>
 {%- endmacro %}

+ 7 - 10
maple/templates/topic/topic_list_base.html

@@ -1,6 +1,5 @@
 {% import 'base/link.html' as link_base  %}
-{% if topics.items %}
-{% for topic in topics.items %}
+{% for topic in topics %}
 {% set author = topic.author %}
 <div class="panel-body" style="padding:6px;border-bottom:1px solid #eee">
     <div class="row">
@@ -11,7 +10,12 @@
                 </a>
             </div>
             <div class="media-body">
-                <div class="media-heading"> <a href="{{url_for('topic.topic',uid=topic.uid)}}" style="color:#555">{{ topic.title}}</a></div>
+                <div class="media-heading">
+                    {% if topic.is_top %}
+                    <span><i class="icon-pushpin text-danger">&nbsp</i></span>
+                    {% endif %}
+                    <a href="{{url_for('topic.topic',uid=topic.uid)}}" style="color:#555">{{ topic.title}}</a>
+                </div>
                 <span class="visible-xs-inline">
                     <small style="font-size:10px;">由{{ link_base.user(topic.author.username) }}</small>
                     <small style="color:#999;font-size:10px;">
@@ -55,10 +59,3 @@
     </div>
 </div>
 {% endfor %}
-{% else %}
-<div class="panel-body">
-    <span class="text-center" style="display:block;width:100%;color:#999">
-        没有帖子
-    </span>
-</div>
-{% endif %}

+ 2 - 1
maple/topic/models.py

@@ -6,7 +6,7 @@
 # Author: jianglin
 # Email: xiyang0807@gmail.com
 # Created: 2016-05-20 13:32:12 (CST)
-# Last Update:星期六 2016-6-25 11:30:55 (CST)
+# Last Update:星期六 2016-6-25 16:40:1 (CST)
 #          By:
 # Description:
 # **************************************************************************
@@ -64,6 +64,7 @@ class Topic(db.Model):
 
     is_good = db.Column(db.Boolean, default=False)
     is_top = db.Column(db.Boolean, default=False)
+    # is_top = db.Column(db.Integer, default = 0)
     is_markdown = db.Column(db.Boolean, default=False)
     is_draft = db.Column(db.Boolean, default=False)
     collect_id = db.Column(db.Integer,

+ 11 - 8
maple/topic/views.py

@@ -6,7 +6,7 @@
 # Author: jianglin
 # Email: xiyang0807@gmail.com
 # Created: 2016-05-20 13:47:04 (CST)
-# Last Update:星期六 2016-6-25 13:15:29 (CST)
+# Last Update:星期六 2016-6-25 17:55:55 (CST)
 #          By:
 # Description:
 # **************************************************************************
@@ -100,20 +100,23 @@ class TopicAPI(MethodView):
         data = {'topic': topic, 'replies': replies, 'form': form}
         return render_template('topic/content.html', **data)
 
-    def template_without_uid(self, topics):
-        return render_template('topic/topic.html', topics=topics)
+    def template_without_uid(self, topics, top_topics):
+        data = {'topics': topics, 'top_topics': top_topics}
+        return render_template('topic/topic.html', **data)
 
     def get(self, uid):
         page = is_num(request.args.get('page'))
         if uid is None:
-            topics = Topic.query.paginate(page,
-                                          app.config['PER_PAGE'],
-                                          error_out=True)
-            return self.template_without_uid(topics)
+            topics = Topic.query.filter_by(is_top=False).paginate(
+                page, app.config['PER_PAGE'],
+                error_out=True)
+            top_topics = Topic.query.filter_by(is_top=True).limit(5).all()
+            return self.template_without_uid(topics, top_topics)
         else:
             form = ReplyForm()
             topic = Topic.query.filter_by(uid=str(uid)).first_or_404()
-            replies = topic.replies.paginate(page, 5, True)
+            replies = topic.replies.paginate(page, app.config['PER_PAGE'],
+                                             True)
             RedisData.set_read_count(topic.id)
             return self.template_with_uid(form, topic, replies)