Browse Source

Added lock/unlock topic feature

sh4nks 11 years ago
parent
commit
04d6a4f21f

+ 4 - 1
flaskbb/app.py

@@ -30,7 +30,8 @@ from flaskbb.extensions import (db, login_manager, mail, cache, redis,
 from flaskbb.utils.helpers import (format_date, time_since, crop_title,
 from flaskbb.utils.helpers import (format_date, time_since, crop_title,
                                    can_post_reply, can_post_topic,
                                    can_post_reply, can_post_topic,
                                    can_delete_topic, can_delete_post, is_online,
                                    can_delete_topic, can_delete_post, is_online,
-                                   can_edit_post, render_markup, mark_online,
+                                   can_edit_post, can_lock_topic,
+                                   can_move_topic, render_markup, mark_online,
                                    is_unread)
                                    is_unread)
 
 
 
 
@@ -130,6 +131,8 @@ def configure_template_filters(app):
     app.jinja_env.filters['edit_post'] = can_edit_post
     app.jinja_env.filters['edit_post'] = can_edit_post
     app.jinja_env.filters['delete_post'] = can_delete_post
     app.jinja_env.filters['delete_post'] = can_delete_post
     app.jinja_env.filters['delete_topic'] = can_delete_topic
     app.jinja_env.filters['delete_topic'] = can_delete_topic
+    app.jinja_env.filters['move_topic'] = can_move_topic
+    app.jinja_env.filters['lock_topic'] = can_lock_topic
     app.jinja_env.filters['post_reply'] = can_post_reply
     app.jinja_env.filters['post_reply'] = can_post_reply
     app.jinja_env.filters['post_topic'] = can_post_topic
     app.jinja_env.filters['post_topic'] = can_post_topic
 
 

+ 37 - 1
flaskbb/forum/views.py

@@ -19,7 +19,8 @@ from flask.ext.login import login_required, current_user
 from flaskbb.extensions import db
 from flaskbb.extensions import db
 from flaskbb.utils.helpers import (can_post_reply, can_delete_topic,
 from flaskbb.utils.helpers import (can_post_reply, can_delete_topic,
                                    can_edit_post, can_post_topic,
                                    can_edit_post, can_post_topic,
-                                   can_delete_post, get_online_users, time_diff)
+                                   can_delete_post, can_lock_topic,
+                                   get_online_users, time_diff)
 from flaskbb.forum.models import Forum, Topic, Post, ForumsRead, TopicsRead
 from flaskbb.forum.models import Forum, Topic, Post, ForumsRead, TopicsRead
 from flaskbb.forum.forms import QuickreplyForm, ReplyForm, NewTopicForm
 from flaskbb.forum.forms import QuickreplyForm, ReplyForm, NewTopicForm
 from flaskbb.forum.helpers import get_forums
 from flaskbb.forum.helpers import get_forums
@@ -194,6 +195,41 @@ def delete_topic(topic_id):
     return redirect(url_for("forum.view_forum", forum_id=topic.forum_id))
     return redirect(url_for("forum.view_forum", forum_id=topic.forum_id))
 
 
 
 
+@forum.route("/topic/<int:topic_id>/lock")
+@login_required
+def lock_topic(topic_id):
+    topic = Topic.query.filter_by(id=topic_id).first_or_404()
+
+    if not can_lock_topic(user=current_user, forum=topic.forum):
+        flash("Yo do not have the permissions to lock this topic", "danger")
+        return redirect(url_for("forum.view_topic", topic_id=topic.id))
+
+    topic.locked = True
+    topic.save()
+    return redirect(url_for("forum.view_topic", topic_id=topic.id))
+
+
+@forum.route("/topic/<int:topic_id>/unlock")
+@login_required
+def unlock_topic(topic_id):
+    topic = Topic.query.filter_by(id=topic_id).first_or_404()
+
+    # Unlock is basically the same as lock
+    if not can_lock_topic(user=current_user, forum=topic.forum):
+        flash("Yo do not have the permissions to unlock this topic", "danger")
+        return redirect(url_for("forum.view_topic", topic_id=topic.id))
+
+    topic.locked = False
+    topic.save()
+    return redirect(url_for("forum.view_topic", topic_id=topic.id))
+
+
+@forum.route("/topic/<int:topic_id>/move")
+@login_required
+def move_topic(topic_id):
+    pass
+
+
 @forum.route("/topic/<int:topic_id>/post/new", methods=["POST", "GET"])
 @forum.route("/topic/<int:topic_id>/post/new", methods=["POST", "GET"])
 @login_required
 @login_required
 def new_post(topic_id):
 def new_post(topic_id):

+ 22 - 13
flaskbb/templates/forum/topic.html

@@ -20,22 +20,31 @@
 </div> <!-- end span pagination -->
 </div> <!-- end span pagination -->
 
 
 <div class="pull-right" style="padding-bottom: 10px">
 <div class="pull-right" style="padding-bottom: 10px">
+    <div class="btn btn-group">
     {% if current_user|delete_topic(topic.first_post.user_id, topic.forum) %}
     {% if current_user|delete_topic(topic.first_post.user_id, topic.forum) %}
-    <a href="{{ url_for('forum.delete_topic', topic_id=topic.id) }}" class="btn btn-danger"><span class="fa fa-trash-o"></span> Delete Topic</a>
+        <a href="{{ url_for('forum.delete_topic', topic_id=topic.id) }}" class="btn btn-danger"><span class="fa fa-trash-o"></span> Delete Topic</a>
     {% endif %}
     {% endif %}
+    {% if current_user|lock_topic(topic.forum) %}
+        {% if not topic.locked %}
+            <a href="{{ url_for('forum.lock_topic', topic_id=topic.id) }}" class="btn btn-warning"><span class="fa fa-lock"></span> Lock Topic</a>
+        {% else %}
+            <a href="{{ url_for('forum.unlock_topic', topic_id=topic.id) }}" class="btn btn-warning"><span class="fa fa-unlock"></span> Unlock Topic</a>
+        {% endif %}
+    {% endif %}
+    </div>
 
 
     {% if current_user.is_authenticated() %}
     {% if current_user.is_authenticated() %}
-        <div class="btn btn-group">
-            {% if current_user.is_tracking_topic(topic) %}
-            <a href="{{ url_for('forum.untrack_topic', topic_id=topic.id) }}" class="btn btn-default"><span class="fa fa-star"></span> Untrack Topic</a>
-            {% else %}
-            <a href="{{ url_for('forum.track_topic', topic_id=topic.id) }}" class="btn btn-default"><span class="fa fa-star"></span> Track Topic</a>
-            {% endif %}
-
-            {% if current_user|post_reply(topic.forum) and not (topic.locked or topic.forum.locked) %}
-            <a href="{{ url_for('forum.new_post', topic_id=topic.id) }}" class="btn btn-primary"><span class="fa fa-pencil"></span> Reply</a>
-            {% endif %}
-        </div>
+    <div class="btn btn-group">
+        {% if current_user.is_tracking_topic(topic) %}
+        <a href="{{ url_for('forum.untrack_topic', topic_id=topic.id) }}" class="btn btn-default"><span class="fa fa-star"></span> Untrack Topic</a>
+        {% else %}
+        <a href="{{ url_for('forum.track_topic', topic_id=topic.id) }}" class="btn btn-default"><span class="fa fa-star"></span> Track Topic</a>
+        {% endif %}
+
+        {% if current_user|post_reply(topic.forum) and not (topic.locked or topic.forum.locked) %}
+        <a href="{{ url_for('forum.new_post', topic_id=topic.id) }}" class="btn btn-primary"><span class="fa fa-pencil"></span> Reply</a>
+        {% endif %}
+    </div>
     {% endif %}
     {% endif %}
 </div>
 </div>
 
 
@@ -45,7 +54,7 @@
         <tr>
         <tr>
             <td >
             <td >
                 <span class="pull-right">
                 <span class="pull-right">
-                    <strong>#{%- if posts.page == 1 -%} {{ loop.index }} {%- else -%} {{ loop.index + (posts.page - 1) * per_page }} {%- endif -%}</strong>
+                    <strong>#{%- if posts.page == 1 -%} {{ loop.index }} {%- else -%} {{ loop.index + (posts.page - 1) * config["POSTS_PER_PAGE"] }} {%- endif -%}</strong>
                 </span>
                 </span>
                 <span class="pull-left">
                 <span class="pull-left">
                     <a href="
                     <a href="

+ 2 - 0
flaskbb/user/models.py

@@ -43,6 +43,8 @@ class Group(db.Model):
     editpost = db.Column(db.Boolean, default=True)
     editpost = db.Column(db.Boolean, default=True)
     deletepost = db.Column(db.Boolean, default=False)
     deletepost = db.Column(db.Boolean, default=False)
     deletetopic = db.Column(db.Boolean, default=False)
     deletetopic = db.Column(db.Boolean, default=False)
+    locktopic = db.Column(db.Boolean, default=True)
+    movetopic = db.Column(db.Boolean, default=True)
     posttopic = db.Column(db.Boolean, default=True)
     posttopic = db.Column(db.Boolean, default=True)
     postreply = db.Column(db.Boolean, default=True)
     postreply = db.Column(db.Boolean, default=True)
 
 

+ 14 - 0
flaskbb/utils/helpers.py

@@ -130,6 +130,20 @@ def can_delete_topic(user, post_user_id, forum):
                       post_user_id=post_user_id)
                       post_user_id=post_user_id)
 
 
 
 
+def can_lock_topic(user, forum):
+    """
+    Check if the user is allowed to lock a topic in the forum
+    """
+    return check_perm(user=user, perm='locktopic', forum=forum)
+
+
+def can_move_topic(user, forum):
+    """
+    Check if the user is allowed to lock a topic in the forum
+    """
+    return check_perm(user=user, perm='movetopic', forum=forum)
+
+
 def can_post_reply(user, forum):
 def can_post_reply(user, forum):
     """
     """
     Check if the user is allowed to post in the forum
     Check if the user is allowed to post in the forum

+ 12 - 1
flaskbb/utils/populate.py

@@ -10,7 +10,6 @@
 """
 """
 from collections import OrderedDict
 from collections import OrderedDict
 
 
-from flaskbb.extensions import db
 from flaskbb.user.models import User, Group
 from flaskbb.user.models import User, Group
 from flaskbb.forum.models import Post, Topic, Forum
 from flaskbb.forum.models import Post, Topic, Forum
 
 
@@ -28,6 +27,8 @@ GROUPS = OrderedDict((
         'deletetopic': True,
         'deletetopic': True,
         'posttopic': True,
         'posttopic': True,
         'postreply': True,
         'postreply': True,
+        'locktopic': True,
+        'movetopic': True,
         'viewtopic': True,
         'viewtopic': True,
         'viewprofile': True
         'viewprofile': True
     }),
     }),
@@ -43,6 +44,8 @@ GROUPS = OrderedDict((
         'deletetopic': True,
         'deletetopic': True,
         'posttopic': True,
         'posttopic': True,
         'postreply': True,
         'postreply': True,
+        'locktopic': True,
+        'movetopic': True,
         'viewtopic': True,
         'viewtopic': True,
         'viewprofiles': True
         'viewprofiles': True
     }),
     }),
@@ -58,6 +61,8 @@ GROUPS = OrderedDict((
         'deletetopic': True,
         'deletetopic': True,
         'posttopic': True,
         'posttopic': True,
         'postreply': True,
         'postreply': True,
+        'locktopic': True,
+        'movetopic': True,
         'viewtopic': True,
         'viewtopic': True,
         'viewprofile': True
         'viewprofile': True
     }),
     }),
@@ -73,6 +78,8 @@ GROUPS = OrderedDict((
         'deletetopic': False,
         'deletetopic': False,
         'posttopic': True,
         'posttopic': True,
         'postreply': True,
         'postreply': True,
+        'locktopic': False,
+        'movetopic': False,
         'viewtopic': True,
         'viewtopic': True,
         'viewprofile': True
         'viewprofile': True
     }),
     }),
@@ -88,6 +95,8 @@ GROUPS = OrderedDict((
         'deletetopic': False,
         'deletetopic': False,
         'posttopic': False,
         'posttopic': False,
         'postreply': False,
         'postreply': False,
+        'locktopic': False,
+        'movetopic': False,
         'viewtopic': False,
         'viewtopic': False,
         'viewprofile': False
         'viewprofile': False
     }),
     }),
@@ -103,6 +112,8 @@ GROUPS = OrderedDict((
         'deletetopic': False,
         'deletetopic': False,
         'posttopic': False,
         'posttopic': False,
         'postreply': False,
         'postreply': False,
+        'locktopic': False,
+        'movetopic': False,
         'viewtopic': False,
         'viewtopic': False,
         'viewprofile': False
         'viewprofile': False
     })
     })