Browse Source

Forum/Views: many code fixes

- Rename all variable name conflicts;
- PEP8;
- Other minor cleanups
Henry Kupty 10 years ago
parent
commit
206e3d22f6
1 changed files with 68 additions and 40 deletions
  1. 68 40
      flaskbb/forum/views.py

+ 68 - 40
flaskbb/forum/views.py

@@ -76,16 +76,21 @@ def view_category(category_id, slug=None):
 def view_forum(forum_id, slug=None):
 def view_forum(forum_id, slug=None):
     page = request.args.get('page', 1, type=int)
     page = request.args.get('page', 1, type=int)
 
 
-    forum, forumsread = Forum.get_forum(forum_id=forum_id, user=current_user)
+    forum_instance, forumsread = Forum.get_forum(forum_id=forum_id,
+                                                 user=current_user)
 
 
-    if forum.external:
-        return redirect(forum.external)
+    if forum_instance.external:
+        return redirect(forum_instance.external)
 
 
-    topics = Forum.get_topics(forum_id=forum.id, user=current_user, page=page,
-                              per_page=flaskbb_config["TOPICS_PER_PAGE"])
+    topics = Forum.get_topics(
+        forum_id=forum_instance.id, user=current_user, page=page,
+        per_page=flaskbb_config["TOPICS_PER_PAGE"]
+    )
 
 
-    return render_template("forum/forum.html", forum=forum, topics=topics,
-                           forumsread=forumsread,)
+    return render_template(
+        "forum/forum.html", forum=forum_instance,
+        topics=topics, forumsread=forumsread,
+    )
 
 
 
 
 @forum.route("/topic/<int:topic_id>", methods=["POST", "GET"])
 @forum.route("/topic/<int:topic_id>", methods=["POST", "GET"])
@@ -145,28 +150,33 @@ def view_post(post_id):
 @forum.route("/<int:forum_id>-<slug>/topic/new", methods=["POST", "GET"])
 @forum.route("/<int:forum_id>-<slug>/topic/new", methods=["POST", "GET"])
 @login_required
 @login_required
 def new_topic(forum_id, slug=None):
 def new_topic(forum_id, slug=None):
-    forum = Forum.query.filter_by(id=forum_id).first_or_404()
+    forum_instance = Forum.query.filter_by(id=forum_id).first_or_404()
 
 
-    if forum.locked:
+    if forum_instance.locked:
         flash("This forum is locked; you cannot submit new topics or posts.",
         flash("This forum is locked; you cannot submit new topics or posts.",
               "danger")
               "danger")
-        return redirect(forum.url)
+        return redirect(forum_instance.url)
 
 
-    if not can_post_topic(user=current_user, forum=forum):
+    if not can_post_topic(user=current_user, forum=forum_instance):
         flash("You do not have the permissions to create a new topic.",
         flash("You do not have the permissions to create a new topic.",
               "danger")
               "danger")
-        return redirect(forum.url)
+        return redirect(forum_instance.url)
 
 
     form = NewTopicForm()
     form = NewTopicForm()
     if form.validate_on_submit():
     if form.validate_on_submit():
         if request.form['button'] == 'preview':
         if request.form['button'] == 'preview':
-            return render_template("forum/new_topic.html", forum=forum, form=form, preview=form.content.data)
+            return render_template(
+                "forum/new_topic.html", forum=forum_instance,
+                form=form, preview=form.content.data
+            )
         else:
         else:
-            topic = form.save(current_user, forum)
+            topic = form.save(current_user, forum_instance)
 
 
             # redirect to the new topic
             # redirect to the new topic
             return redirect(url_for('forum.view_topic', topic_id=topic.id))
             return redirect(url_for('forum.view_topic', topic_id=topic.id))
-    return render_template("forum/new_topic.html", forum=forum, form=form)
+    return render_template(
+        "forum/new_topic.html", forum=forum_instance, form=form
+    )
 
 
 
 
 @forum.route("/topic/<int:topic_id>/delete")
 @forum.route("/topic/<int:topic_id>/delete")
@@ -223,23 +233,28 @@ def unlock_topic(topic_id, slug=None):
 
 
 
 
 @forum.route("/topic/<int:topic_id>/move/<int:forum_id>")
 @forum.route("/topic/<int:topic_id>/move/<int:forum_id>")
-@forum.route("/topic/<int:topic_id>-<topic_slug>/move/<int:forum_id>-<forum_slug>")
+@forum.route(
+    "/topic/<int:topic_id>-<topic_slug>/move/<int:forum_id>-<forum_slug>"
+)
 @login_required
 @login_required
 def move_topic(topic_id, forum_id, topic_slug=None, forum_slug=None):
 def move_topic(topic_id, forum_id, topic_slug=None, forum_slug=None):
-    forum = Forum.query.filter_by(id=forum_id).first_or_404()
+    forum_instance = Forum.query.filter_by(id=forum_id).first_or_404()
     topic = Topic.query.filter_by(id=topic_id).first_or_404()
     topic = Topic.query.filter_by(id=topic_id).first_or_404()
 
 
     # TODO: Bulk move
     # TODO: Bulk move
 
 
     if not can_moderate(user=current_user, forum=topic.forum):
     if not can_moderate(user=current_user, forum=topic.forum):
         flash("Yo do not have the permissions to move this topic", "danger")
         flash("Yo do not have the permissions to move this topic", "danger")
-        return redirect(forum.url)
+        return redirect(forum_instance.url)
 
 
-    if not topic.move(forum):
-        flash("Could not move the topic to forum %s" % forum.title, "danger")
+    if not topic.move(forum_instance):
+        flash(
+            "Could not move the topic to forum %s" % forum_instance.title,
+            "danger"
+        )
         return redirect(topic.url)
         return redirect(topic.url)
 
 
-    flash("Topic was moved to forum %s" % forum.title, "success")
+    flash("Topic was moved to forum %s" % forum_instance.title, "success")
     return redirect(topic.url)
     return redirect(topic.url)
 
 
 
 
@@ -247,21 +262,22 @@ def move_topic(topic_id, forum_id, topic_slug=None, forum_slug=None):
 @forum.route("/topic/<int:old_id>-<old_slug>/merge/<int:new_id>-<new_slug>")
 @forum.route("/topic/<int:old_id>-<old_slug>/merge/<int:new_id>-<new_slug>")
 @login_required
 @login_required
 def merge_topic(old_id, new_id, old_slug=None, new_slug=None):
 def merge_topic(old_id, new_id, old_slug=None, new_slug=None):
-    old_topic = Topic.query.filter_by(id=old_id).first_or_404()
-    new_topic = Topic.query.filter_by(id=new_id).first_or_404()
+    _old_topic = Topic.query.filter_by(id=old_id).first_or_404()
+    _new_topic = Topic.query.filter_by(id=new_id).first_or_404()
 
 
     # TODO: Bulk merge
     # TODO: Bulk merge
 
 
-    if not can_moderate(user=current_user, forum=topic.forum):
+    # Looks to me that the user should have permissions on both forums, right?
+    if not can_moderate(user=current_user, forum=_old_topic.forum):
         flash("Yo do not have the permissions to merge this topic", "danger")
         flash("Yo do not have the permissions to merge this topic", "danger")
-        return redirect(old_topic.url)
+        return redirect(_old_topic.url)
 
 
-    if not old_topic.merge(new_topic):
+    if not _old_topic.merge(_new_topic):
         flash("Could not merge the topic.", "danger")
         flash("Could not merge the topic.", "danger")
-        return redirect(old_topic.url)
+        return redirect(_old_topic.url)
 
 
     flash("Topic succesfully merged.", "success")
     flash("Topic succesfully merged.", "success")
-    return redirect(new_topic.url)
+    return redirect(_new_topic.url)
 
 
 
 
 @forum.route("/topic/<int:topic_id>/post/new", methods=["POST", "GET"])
 @forum.route("/topic/<int:topic_id>/post/new", methods=["POST", "GET"])
@@ -286,7 +302,10 @@ def new_post(topic_id, slug=None):
     form = ReplyForm()
     form = ReplyForm()
     if form.validate_on_submit():
     if form.validate_on_submit():
         if request.form['button'] == 'preview':
         if request.form['button'] == 'preview':
-            return render_template("forum/new_post.html", topic=topic, form=form, preview=form.content.data)
+            return render_template(
+                "forum/new_post.html", topic=topic,
+                form=form, preview=form.content.data
+            )
         else:
         else:
             post = form.save(current_user, topic)
             post = form.save(current_user, topic)
             return view_post(post.id)
             return view_post(post.id)
@@ -294,7 +313,9 @@ def new_post(topic_id, slug=None):
     return render_template("forum/new_post.html", topic=topic, form=form)
     return render_template("forum/new_post.html", topic=topic, form=form)
 
 
 
 
-@forum.route("/topic/<int:topic_id>/post/<int:post_id>/reply", methods=["POST", "GET"])
+@forum.route(
+    "/topic/<int:topic_id>/post/<int:post_id>/reply", methods=["POST", "GET"]
+)
 @login_required
 @login_required
 def reply_post(topic_id, post_id):
 def reply_post(topic_id, post_id):
     topic = Topic.query.filter_by(id=topic_id).first_or_404()
     topic = Topic.query.filter_by(id=topic_id).first_or_404()
@@ -316,7 +337,10 @@ def reply_post(topic_id, post_id):
     form = ReplyForm()
     form = ReplyForm()
     if form.validate_on_submit():
     if form.validate_on_submit():
         if request.form['button'] == 'preview':
         if request.form['button'] == 'preview':
-            return render_template("forum/new_post.html", topic=topic, form=form, preview=form.content.data)
+            return render_template(
+                "forum/new_post.html", topic=topic,
+                form=form, preview=form.content.data
+            )
         else:
         else:
             form.save(current_user, topic)
             form.save(current_user, topic)
             return redirect(post.topic.url)
             return redirect(post.topic.url)
@@ -348,7 +372,10 @@ def edit_post(post_id):
     form = ReplyForm()
     form = ReplyForm()
     if form.validate_on_submit():
     if form.validate_on_submit():
         if request.form['button'] == 'preview':
         if request.form['button'] == 'preview':
-            return render_template("forum/new_post.html", topic=post.topic, form=form, preview=form.content.data)
+            return render_template(
+                "forum/new_post.html", topic=post.topic,
+                form=form, preview=form.content.data
+            )
         else:
         else:
             form.populate_obj(post)
             form.populate_obj(post)
             post.date_modified = datetime.datetime.utcnow()
             post.date_modified = datetime.datetime.utcnow()
@@ -401,16 +428,17 @@ def report_post(post_id):
 def markread(forum_id=None, slug=None):
 def markread(forum_id=None, slug=None):
     # Mark a single forum as read
     # Mark a single forum as read
     if forum_id:
     if forum_id:
-        forum = Forum.query.filter_by(id=forum_id).first_or_404()
-        forumsread = ForumsRead.query.filter_by(user_id=current_user.id,
-                                                forum_id=forum.id).first()
+        forum_instance = Forum.query.filter_by(id=forum_id).first_or_404()
+        forumsread = ForumsRead.query.filter_by(
+            user_id=current_user.id, forum_id=forum_instance.id
+        ).first()
         TopicsRead.query.filter_by(user_id=current_user.id,
         TopicsRead.query.filter_by(user_id=current_user.id,
-                                   forum_id=forum.id).delete()
+                                   forum_id=forum_instance.id).delete()
 
 
         if not forumsread:
         if not forumsread:
             forumsread = ForumsRead()
             forumsread = ForumsRead()
             forumsread.user_id = current_user.id
             forumsread.user_id = current_user.id
-            forumsread.forum_id = forum.id
+            forumsread.forum_id = forum_instance.id
 
 
         forumsread.last_read = datetime.datetime.utcnow()
         forumsread.last_read = datetime.datetime.utcnow()
         forumsread.cleared = datetime.datetime.utcnow()
         forumsread.cleared = datetime.datetime.utcnow()
@@ -418,7 +446,7 @@ def markread(forum_id=None, slug=None):
         db.session.add(forumsread)
         db.session.add(forumsread)
         db.session.commit()
         db.session.commit()
 
 
-        return redirect(forum.url)
+        return redirect(forum_instance.url)
 
 
     # Mark all forums as read
     # Mark all forums as read
     ForumsRead.query.filter_by(user_id=current_user.id).delete()
     ForumsRead.query.filter_by(user_id=current_user.id).delete()
@@ -426,10 +454,10 @@ def markread(forum_id=None, slug=None):
 
 
     forums = Forum.query.all()
     forums = Forum.query.all()
     forumsread_list = []
     forumsread_list = []
-    for forum in forums:
+    for forum_instance in forums:
         forumsread = ForumsRead()
         forumsread = ForumsRead()
         forumsread.user_id = current_user.id
         forumsread.user_id = current_user.id
-        forumsread.forum_id = forum.id
+        forumsread.forum_id = forum_instance.id
         forumsread.last_read = datetime.datetime.utcnow()
         forumsread.last_read = datetime.datetime.utcnow()
         forumsread.cleared = datetime.datetime.utcnow()
         forumsread.cleared = datetime.datetime.utcnow()
         forumsread_list.append(forumsread)
         forumsread_list.append(forumsread)