Просмотр исходного кода

Merge branch 'master' into cascades

* master:
  Remove n+1 queries in view topic
  Attach actual user objects not their IDs when resurrecting PM thread
  Pin SQLAlchemy to 1.1.15 for now
  cast page to int in ViewPost
Peter Justin 7 лет назад
Родитель
Сommit
2d6fdd9a7b
4 измененных файлов с 21 добавлено и 19 удалено
  1. 11 10
      flaskbb/forum/views.py
  2. 5 4
      flaskbb/message/views.py
  3. 4 4
      flaskbb/templates/forum/topic.html
  4. 1 1
      requirements.txt

+ 11 - 10
flaskbb/forum/views.py

@@ -117,7 +117,7 @@ class ViewPost(MethodView):
         post = Post.query.filter_by(id=post_id).first_or_404()
         post = Post.query.filter_by(id=post_id).first_or_404()
         post_in_topic = Post.query.filter(Post.topic_id == post.topic_id,
         post_in_topic = Post.query.filter(Post.topic_id == post.topic_id,
                                           Post.id <= post_id).order_by(Post.id.asc()).count()
                                           Post.id <= post_id).order_by(Post.id.asc()).count()
-        page = math.ceil(post_in_topic / float(flaskbb_config['POSTS_PER_PAGE']))
+        page = int(math.ceil(post_in_topic / float(flaskbb_config['POSTS_PER_PAGE'])))
 
 
         return redirect(
         return redirect(
             url_for(
             url_for(
@@ -143,6 +143,16 @@ class ViewTopic(MethodView):
         topic.views += 1
         topic.views += 1
         topic.save()
         topic.save()
 
 
+
+        # Update the topicsread status if the user hasn't read it
+        forumsread = None
+        if current_user.is_authenticated:
+            forumsread = ForumsRead.query.\
+                filter_by(user_id=current_user.id,
+                          forum_id=topic.forum_id).first()
+
+        topic.update_read(real(current_user), topic.forum, forumsread)
+
         # fetch the posts in the topic
         # fetch the posts in the topic
         posts = Post.query.\
         posts = Post.query.\
             outerjoin(User, Post.user_id == User.id).\
             outerjoin(User, Post.user_id == User.id).\
@@ -155,15 +165,6 @@ class ViewTopic(MethodView):
         if len(posts.items) == 0:
         if len(posts.items) == 0:
             abort(404)
             abort(404)
 
 
-        # Update the topicsread status if the user hasn't read it
-        forumsread = None
-        if current_user.is_authenticated:
-            forumsread = ForumsRead.query.\
-                filter_by(user_id=real(current_user).id,
-                          forum_id=topic.forum.id).first()
-
-        topic.update_read(real(current_user), topic.forum, forumsread)
-
         return render_template(
         return render_template(
             'forum/topic.html', topic=topic, posts=posts, last_seen=time_diff(), form=self.form()
             'forum/topic.html', topic=topic, posts=posts, last_seen=time_diff(), form=self.form()
         )
         )

+ 5 - 4
flaskbb/message/views.py

@@ -21,11 +21,10 @@ from flaskbb.extensions import db
 from flaskbb.message.forms import ConversationForm, MessageForm
 from flaskbb.message.forms import ConversationForm, MessageForm
 from flaskbb.message.models import Conversation, Message
 from flaskbb.message.models import Conversation, Message
 from flaskbb.user.models import User
 from flaskbb.user.models import User
-from flaskbb.utils.helpers import (format_quote, register_view,
+from flaskbb.utils.helpers import (format_quote, real, register_view,
                                    render_template, time_utcnow)
                                    render_template, time_utcnow)
 from flaskbb.utils.settings import flaskbb_config
 from flaskbb.utils.settings import flaskbb_config
 
 
-
 logger = logging.getLogger(__name__)
 logger = logging.getLogger(__name__)
 
 
 message = Blueprint("message", __name__)
 message = Blueprint("message", __name__)
@@ -108,8 +107,10 @@ class ViewConversation(MethodView):
             # then we have to change the id's a bit.
             # then we have to change the id's a bit.
             if current_user.id == conversation.to_user_id:
             if current_user.id == conversation.to_user_id:
                 to_user_id = conversation.from_user_id
                 to_user_id = conversation.from_user_id
+                to_user = conversation.from_user
             else:
             else:
                 to_user_id = conversation.to_user_id
                 to_user_id = conversation.to_user_id
+                to_user = conversation.to_user
 
 
             form.save(conversation=conversation, user_id=current_user.id)
             form.save(conversation=conversation, user_id=current_user.id)
 
 
@@ -125,8 +126,8 @@ class ViewConversation(MethodView):
             if conversation is None:
             if conversation is None:
                 conversation = Conversation(
                 conversation = Conversation(
                     subject=old_conv.subject,
                     subject=old_conv.subject,
-                    from_user_id=current_user.id,
-                    to_user=to_user_id,
+                    from_user=real(current_user),
+                    to_user=to_user,
                     user_id=to_user_id,
                     user_id=to_user_id,
                     shared_id=old_conv.shared_id
                     shared_id=old_conv.shared_id
                 )
                 )

+ 4 - 4
flaskbb/templates/forum/topic.html

@@ -123,7 +123,7 @@
                             <!-- Edit Post -->
                             <!-- Edit Post -->
                             <a href="{{ url_for('forum.edit_post', post_id=post.id) }}" class="btn btn-icon icon-edit" data-toggle="tooltip" data-placement="top" title="Edit this post"></a>
                             <a href="{{ url_for('forum.edit_post', post_id=post.id) }}" class="btn btn-icon icon-edit" data-toggle="tooltip" data-placement="top" title="Edit this post"></a>
                             {% endif %}
                             {% endif %}
-                            {% if topic.first_post == post %}
+                            {% if topic.first_post_id == post.id %}
                                 {% if current_user|delete_topic(topic) %}
                                 {% if current_user|delete_topic(topic) %}
                                 <form class="inline-form" method="post" action="{{ url_for('forum.delete_topic', topic_id=topic.id, slug=topic.slug) }}">
                                 <form class="inline-form" method="post" action="{{ url_for('forum.delete_topic', topic_id=topic.id, slug=topic.slug) }}">
                                     <input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
                                     <input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
@@ -147,14 +147,14 @@
                             {% endif %}
                             {% endif %}
 
 
                             {% if current_user.permissions.get('makehidden') %}
                             {% if current_user.permissions.get('makehidden') %}
-                                {% if post.first_post %}
+                                {% if topic.first_post_id == post.id %}
                                     {% if topic.hidden %}
                                     {% if topic.hidden %}
-                                    <form class="inline-form" method="post" action="{{ url_for('forum.unhide_topic', topic_id=post.topic.id) }}">
+                                    <form class="inline-form" method="post" action="{{ url_for('forum.unhide_topic', topic_id=topic.id) }}">
                                         <input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
                                         <input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
                                         <button class="btn btn-icon fa fa-user" name="unhide" data-toggle="tooltip" data-placement="top" title="Unhide this topic"></button>
                                         <button class="btn btn-icon fa fa-user" name="unhide" data-toggle="tooltip" data-placement="top" title="Unhide this topic"></button>
                                     </form>
                                     </form>
                                     {% else %}
                                     {% else %}
-                                    <form class="inline-form" method="post" action="{{ url_for('forum.hide_topic', topic_id=post.topic.id) }}">
+                                    <form class="inline-form" method="post" action="{{ url_for('forum.hide_topic', topic_id=topic.id) }}">
                                         <input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
                                         <input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
                                         <button class="btn btn-icon fa fa-user-secret" name="hide" data-toggle="tooltip" data-placement="top" title="Hide this topic"></button>
                                         <button class="btn btn-icon fa fa-user-secret" name="hide" data-toggle="tooltip" data-placement="top" title="Hide this topic"></button>
                                     </form>
                                     </form>

+ 1 - 1
requirements.txt

@@ -44,7 +44,7 @@ requests==2.18.4
 simplejson==3.13.2
 simplejson==3.13.2
 six==1.11.0
 six==1.11.0
 speaklater==1.3
 speaklater==1.3
-SQLAlchemy==1.2.2
+SQLAlchemy==1.1.15
 SQLAlchemy-Utils==0.32.21
 SQLAlchemy-Utils==0.32.21
 Unidecode==1.0.22
 Unidecode==1.0.22
 urllib3==1.22
 urllib3==1.22