Browse Source

Add topic hooks

Peter Justin 7 years ago
parent
commit
d370102e01

+ 10 - 3
docs/hooks.rst

@@ -48,7 +48,7 @@ The hooks below are listed in the order they are called.
 FlaskBB CLI Hooks
 FlaskBB CLI Hooks
 ~~~~~~~~~~~~~~~~~
 ~~~~~~~~~~~~~~~~~
 
 
-These are hooks are only invoked when using the ``flaskbb``
+These hooks are only invoked when using the ``flaskbb``
 CLI.
 CLI.
 
 
 .. autofunction:: flaskbb_cli
 .. autofunction:: flaskbb_cli
@@ -58,8 +58,10 @@ CLI.
 FlaskBB Event Hooks
 FlaskBB Event Hooks
 ~~~~~~~~~~~~~~~~~~~
 ~~~~~~~~~~~~~~~~~~~
 
 
-.. autofunction:: flaskbb_event_before_post
-.. autofunction:: flaskbb_event_after_post
+.. autofunction:: flaskbb_event_post_save_before
+.. autofunction:: flaskbb_event_post_save_after
+.. autofunction:: flaskbb_event_topic_save_before
+.. autofunction:: flaskbb_event_topic_save_after
 
 
 
 
 FlaskBB Form Hooks
 FlaskBB Form Hooks
@@ -68,6 +70,9 @@ FlaskBB Form Hooks
 .. autofunction:: flaskbb_form_new_post_save
 .. autofunction:: flaskbb_form_new_post_save
 .. autofunction:: flaskbb_form_new_post
 .. autofunction:: flaskbb_form_new_post
 
 
+.. autofunction:: flaskbb_form_new_topic
+.. autofunction:: flaskbb_form_new_topic_save
+
 
 
 Template Hooks
 Template Hooks
 --------------
 --------------
@@ -88,6 +93,8 @@ Template Hooks
 .. autofunction:: flaskbb_tpl_form_user_details_after
 .. autofunction:: flaskbb_tpl_form_user_details_after
 .. autofunction:: flaskbb_tpl_form_new_post_before
 .. autofunction:: flaskbb_tpl_form_new_post_before
 .. autofunction:: flaskbb_tpl_form_new_post_after
 .. autofunction:: flaskbb_tpl_form_new_post_after
+.. autofunction:: flaskbb_tpl_form_new_topic_before
+.. autofunction:: flaskbb_tpl_form_new_topic_after
 .. autofunction:: flaskbb_tpl_profile_settings_menu
 .. autofunction:: flaskbb_tpl_profile_settings_menu
 .. autofunction:: flaskbb_tpl_profile_sidebar_stats
 .. autofunction:: flaskbb_tpl_profile_sidebar_stats
 .. autofunction:: flaskbb_tpl_post_author_info_before
 .. autofunction:: flaskbb_tpl_post_author_info_before

+ 2 - 0
flaskbb/forum/forms.py

@@ -74,6 +74,8 @@ class NewTopicForm(ReplyForm):
 
 
         if self.track_topic.data:
         if self.track_topic.data:
             user.track_topic(topic)
             user.track_topic(topic)
+
+        current_app.pluggy.hook.flaskbb_form_new_topic_save(form=self)
         return topic.save(user=user, forum=forum, post=post)
         return topic.save(user=user, forum=forum, post=post)
 
 
 
 

+ 12 - 5
flaskbb/forum/models.py

@@ -201,14 +201,14 @@ class Post(HideableCRUDMixin, db.Model):
         :param user: The user who has created the post
         :param user: The user who has created the post
         :param topic: The topic in which the post was created
         :param topic: The topic in which the post was created
         """
         """
-        current_app.pluggy.hook.flaskbb_event_before_post(post=self)
+        current_app.pluggy.hook.flaskbb_event_post_save_before(post=self)
 
 
         # update/edit the post
         # update/edit the post
         if self.id:
         if self.id:
             db.session.add(self)
             db.session.add(self)
             db.session.commit()
             db.session.commit()
-            current_app.pluggy.hook.flaskbb_event_after_post(post=self,
-                                                             is_new=False)
+            current_app.pluggy.hook.flaskbb_event_post_save_after(post=self,
+                                                                  is_new=False)
             return self
             return self
 
 
         # Adding a new post
         # Adding a new post
@@ -238,8 +238,8 @@ class Post(HideableCRUDMixin, db.Model):
             # And commit it!
             # And commit it!
             db.session.add(topic)
             db.session.add(topic)
             db.session.commit()
             db.session.commit()
-            current_app.pluggy.hook.flaskbb_event_after_post(post=self,
-                                                             is_new=True)
+            current_app.pluggy.hook.flaskbb_event_post_save_after(post=self,
+                                                                  is_new=True)
             return self
             return self
 
 
     def delete(self):
     def delete(self):
@@ -633,11 +633,15 @@ class Topic(HideableCRUDMixin, db.Model):
         :param forum: The forum where the topic is stored
         :param forum: The forum where the topic is stored
         :param post: The post object which is connected to the topic
         :param post: The post object which is connected to the topic
         """
         """
+        current_app.pluggy.hook.flaskbb_event_topic_save_before(topic=self)
 
 
         # Updates the topic
         # Updates the topic
         if self.id:
         if self.id:
             db.session.add(self)
             db.session.add(self)
             db.session.commit()
             db.session.commit()
+            current_app.pluggy.hook.flaskbb_event_topic_save_after(
+                topic=self, is_new=False
+            )
             return self
             return self
 
 
         # Set the forum and user id
         # Set the forum and user id
@@ -661,6 +665,9 @@ class Topic(HideableCRUDMixin, db.Model):
         # Update the topic count
         # Update the topic count
         forum.topic_count += 1
         forum.topic_count += 1
         db.session.commit()
         db.session.commit()
+
+        current_app.pluggy.hook.flaskbb_event_topic_save_after(topic=self,
+                                                               is_new=True)
         return self
         return self
 
 
     def delete(self, users=None):
     def delete(self, users=None):

+ 23 - 8
flaskbb/forum/views.py

@@ -201,11 +201,12 @@ class ViewTopic(MethodView):
 
 
 class NewTopic(MethodView):
 class NewTopic(MethodView):
     decorators = [login_required]
     decorators = [login_required]
-    form = NewTopicForm
 
 
     def get(self, forum_id, slug=None):
     def get(self, forum_id, slug=None):
         forum_instance = Forum.query.filter_by(id=forum_id).first_or_404()
         forum_instance = Forum.query.filter_by(id=forum_id).first_or_404()
-        return render_template('forum/new_topic.html', forum=forum_instance, form=self.form())
+        return render_template(
+            'forum/new_topic.html', forum=forum_instance, form=self.form()
+        )
 
 
     @allows.requires(CanPostTopic)
     @allows.requires(CanPostTopic)
     def post(self, forum_id, slug=None):
     def post(self, forum_id, slug=None):
@@ -213,14 +214,21 @@ class NewTopic(MethodView):
         form = self.form()
         form = self.form()
         if 'preview' in request.form and form.validate():
         if 'preview' in request.form and form.validate():
             return render_template(
             return render_template(
-                'forum/new_topic.html', forum=forum_instance, form=form, preview=form.content.data
+                'forum/new_topic.html', forum=forum_instance, form=form,
+                preview=form.content.data
             )
             )
         elif 'submit' in request.form and form.validate():
         elif 'submit' in request.form and form.validate():
             topic = form.save(real(current_user), forum_instance)
             topic = form.save(real(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))
         else:
         else:
-            return render_template('forum/new_topic.html', forum=forum_instance, form=form)
+            return render_template(
+                'forum/new_topic.html', forum=forum_instance, form=form
+            )
+
+    def form(self):
+        current_app.pluggy.hook.flaskbb_form_new_topic(form=NewTopicForm)
+        return NewTopicForm()
 
 
 
 
 class ManageForum(MethodView):
 class ManageForum(MethodView):
@@ -362,7 +370,6 @@ class NewPost(MethodView):
 
 
     def post(self, topic_id, slug=None):
     def post(self, topic_id, slug=None):
         topic = Topic.query.filter_by(id=topic_id).first_or_404()
         topic = Topic.query.filter_by(id=topic_id).first_or_404()
-
         form = self.form()
         form = self.form()
 
 
         if form.validate_on_submit():
         if form.validate_on_submit():
@@ -409,12 +416,14 @@ class ReplyPost(MethodView):
 
 
 class EditPost(MethodView):
 class EditPost(MethodView):
     decorators = [allows.requires(CanEditPost), login_required]
     decorators = [allows.requires(CanEditPost), login_required]
-    form = ReplyForm
 
 
     def get(self, post_id):
     def get(self, post_id):
         post = Post.query.filter_by(id=post_id).first_or_404()
         post = Post.query.filter_by(id=post_id).first_or_404()
         form = self.form(obj=post)
         form = self.form(obj=post)
-        return render_template('forum/new_post.html', topic=post.topic, form=form, edit_mode=True)
+
+        return render_template(
+            'forum/new_post.html', topic=post.topic, form=form, edit_mode=True
+        )
 
 
     def post(self, post_id):
     def post(self, post_id):
         post = Post.query.filter_by(id=post_id).first_or_404()
         post = Post.query.filter_by(id=post_id).first_or_404()
@@ -436,7 +445,13 @@ class EditPost(MethodView):
                 post.save()
                 post.save()
                 return redirect(url_for('forum.view_post', post_id=post.id))
                 return redirect(url_for('forum.view_post', post_id=post.id))
 
 
-        return render_template('forum/new_post.html', topic=post.topic, form=form, edit_mode=True)
+        return render_template(
+            'forum/new_post.html', topic=post.topic, form=form, edit_mode=True
+        )
+
+    def form(self):
+        current_app.pluggy.hook.flaskbb_form_new_post(form=ReplyForm)
+        return ReplyForm()
 
 
 
 
 class ReportView(MethodView):
 class ReportView(MethodView):

+ 49 - 2
flaskbb/plugins/spec.py

@@ -78,7 +78,7 @@ def flaskbb_shell_context():
 
 
 # Event hooks
 # Event hooks
 @spec
 @spec
-def flaskbb_event_before_post(post):
+def flaskbb_event_post_save_before(post):
     """Hook for handling a post before it has been saved.
     """Hook for handling a post before it has been saved.
 
 
     :param flaskbb.forum.models.Post post: The post which triggered the event.
     :param flaskbb.forum.models.Post post: The post which triggered the event.
@@ -86,7 +86,7 @@ def flaskbb_event_before_post(post):
 
 
 
 
 @spec
 @spec
-def flaskbb_event_after_post(post, is_new):
+def flaskbb_event_post_save_after(post, is_new):
     """Hook for handling a post after it has been saved.
     """Hook for handling a post after it has been saved.
 
 
     :param flaskbb.forum.models.Post post: The post which triggered the event.
     :param flaskbb.forum.models.Post post: The post which triggered the event.
@@ -94,6 +94,25 @@ def flaskbb_event_after_post(post, is_new):
     """
     """
 
 
 
 
+@spec
+def flaskbb_event_topic_save_before(topic):
+    """Hook for handling a topic before it has been saved.
+
+    :param flaskbb.forum.models.Topic topic: The topic which triggered the
+                                             event.
+    """
+
+
+@spec
+def flaskbb_event_topic_save_after(topic, is_new):
+    """Hook for handling a topic after it has been saved.
+
+    :param flaskbb.forum.models.Topic topic: The topic which triggered the
+                                             event.
+    :param bool is_new: True if the topic is new, False if it is an edit.
+    """
+
+
 # Form hooks
 # Form hooks
 @spec
 @spec
 def flaskbb_form_new_post(form):
 def flaskbb_form_new_post(form):
@@ -124,6 +143,16 @@ def flaskbb_form_new_post_save(form):
     """
     """
 
 
 
 
+@spec
+def flaskbb_form_new_topic(form):
+    """ """
+
+
+@spec
+def flaskbb_form_new_topic_save(form):
+    """ """
+
+
 # Template Hooks
 # Template Hooks
 @spec
 @spec
 def flaskbb_tpl_navigation_before():
 def flaskbb_tpl_navigation_before():
@@ -358,3 +387,21 @@ def flaskbb_tpl_form_new_post_after(form):
 
 
     :param form: The form object.
     :param form: The form object.
     """
     """
+
+
+@spec
+def flaskbb_tpl_form_new_topic_before(form):
+    """Hook for inserting a new form field before the first field is
+    rendered (but before the CSRF token).
+
+    :param form: The form object.
+    """
+
+
+@spec
+def flaskbb_tpl_form_new_topic_after(form):
+    """Hook for inserting a new form field after the last field is
+    rendered (but before the submit button).
+
+    :param form: The form object.
+    """

+ 6 - 0
flaskbb/templates/forum/new_topic.html

@@ -30,9 +30,15 @@
                     <div class="form-group">
                     <div class="form-group">
                         <div class="col-md-12 col-sm-12 col-xs-12">
                         <div class="col-md-12 col-sm-12 col-xs-12">
                             <div class="editor-box">
                             <div class="editor-box">
+
+                                {{ run_hook("flaskbb_tpl_form_new_topic_before", form=form) }}
+
                                 <div class="editor">
                                 <div class="editor">
                                     {{ render_quickreply(form.content, div_class="new-message", rows=7, cols=75, placeholder="", **{'data-provide': 'markdown', 'data-autofocus': 'false', 'class': 'flaskbb-editor'}) }}
                                     {{ render_quickreply(form.content, div_class="new-message", rows=7, cols=75, placeholder="", **{'data-provide': 'markdown', 'data-autofocus': 'false', 'class': 'flaskbb-editor'}) }}
                                 </div>
                                 </div>
+
+                                {{ run_hook("flaskbb_tpl_form_new_topic_after", form=form) }}
+
                                 <div class="editor-submit">
                                 <div class="editor-submit">
                                     {{ render_submit_field(form.submit, input_class="btn btn-success pull-right") }}
                                     {{ render_submit_field(form.submit, input_class="btn btn-success pull-right") }}
                                 </div>
                                 </div>