Browse Source

Update hooks

Peter Justin 7 years ago
parent
commit
c918f04e9b

+ 28 - 10
docs/hooks.rst

@@ -52,6 +52,21 @@ These are hooks are only invoked when using the ``flaskbb``
 CLI.
 CLI.
 
 
 .. autofunction:: flaskbb_cli
 .. autofunction:: flaskbb_cli
+.. autofunction:: flaskbb_shell_context
+
+
+FlaskBB Event Hooks
+~~~~~~~~~~~~~~~~~~~
+
+.. autofunction:: flaskbb_event_before_post
+.. autofunction:: flaskbb_event_after_post
+
+
+FlaskBB Form Hooks
+~~~~~~~~~~~~~~~~~~
+
+.. autofunction:: flaskbb_form_new_post_save
+.. autofunction:: flaskbb_form_new_post
 
 
 
 
 Template Hooks
 Template Hooks
@@ -63,15 +78,18 @@ Template Hooks
     hidden CSRF token field and before an submit field.
     hidden CSRF token field and before an submit field.
 
 
 
 
-.. autofunction:: flaskbb_tpl_before_navigation
-.. autofunction:: flaskbb_tpl_after_navigation
-.. autofunction:: flaskbb_tpl_before_user_nav_loggedin
-.. autofunction:: flaskbb_tpl_after_user_nav_loggedin
-.. autofunction:: flaskbb_tpl_before_registration_form
-.. autofunction:: flaskbb_tpl_after_registration_form
-.. autofunction:: flaskbb_tpl_before_user_details_form
-.. autofunction:: flaskbb_tpl_after_user_details_form
+.. autofunction:: flaskbb_tpl_navigation_before
+.. autofunction:: flaskbb_tpl_navigation_after
+.. autofunction:: flaskbb_tpl_user_nav_loggedin_before
+.. autofunction:: flaskbb_tpl_user_nav_loggedin_after
+.. autofunction:: flaskbb_tpl_form_registration_before
+.. autofunction:: flaskbb_tpl_form_registration_after
+.. autofunction:: flaskbb_tpl_form_user_details_before
+.. autofunction:: flaskbb_tpl_form_user_details_after
+.. autofunction:: flaskbb_tpl_form_new_post_before
+.. autofunction:: flaskbb_tpl_form_new_post_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_before_post_author_info
-.. autofunction:: flaskbb_tpl_after_post_author_info
+.. autofunction:: flaskbb_tpl_post_author_info_before
+.. autofunction:: flaskbb_tpl_post_author_info_after
+.. autofunction:: flaskbb_tpl_admin_settings_menu

+ 10 - 4
flaskbb/forum/models.py

@@ -201,6 +201,8 @@ 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)
+
         # update/edit the post
         # update/edit the post
         if self.id:
         if self.id:
             db.session.add(self)
             db.session.add(self)
@@ -346,7 +348,9 @@ class Post(HideableCRUDMixin, db.Model):
             topic_post_clauses = clauses + [
             topic_post_clauses = clauses + [
                 Post.topic_id == self.topic.id,
                 Post.topic_id == self.topic.id,
             ]
             ]
-            self.topic.post_count = Post.query.filter(*topic_post_clauses).count()
+            self.topic.post_count = Post.query.filter(
+                *topic_post_clauses
+            ).count()
 
 
         forum_post_clauses = clauses + [
         forum_post_clauses = clauses + [
             Post.topic_id == Topic.id,
             Post.topic_id == Topic.id,
@@ -354,7 +358,9 @@ class Post(HideableCRUDMixin, db.Model):
             Topic.hidden != True,
             Topic.hidden != True,
         ]
         ]
 
 
-        self.topic.forum.post_count = Post.query.filter(*forum_post_clauses).count()
+        self.topic.forum.post_count = Post.query.filter(
+            *forum_post_clauses
+        ).count()
 
 
     def _restore_post_to_topic(self):
     def _restore_post_to_topic(self):
         last_unhidden_post = Post.query.filter(
         last_unhidden_post = Post.query.filter(
@@ -368,8 +374,8 @@ class Post(HideableCRUDMixin, db.Model):
             self.topic.last_post = self
             self.topic.last_post = self
             self.second_last_post = last_unhidden_post
             self.second_last_post = last_unhidden_post
 
 
-            # if we're the newest in the topic again, we might be the newest in the forum again
-            # only set if our parent topic isn't hidden
+            # if we're the newest in the topic again, we might be the newest
+            # in the forum again only set if our parent topic isn't hidden
             if (
             if (
                 not self.topic.hidden and
                 not self.topic.hidden and
                 (
                 (

+ 22 - 16
flaskbb/forum/views.py

@@ -57,8 +57,8 @@ class ForumIndex(MethodView):
         if not current_app.config['REDIS_ENABLED']:
         if not current_app.config['REDIS_ENABLED']:
             online_users = User.query.filter(User.lastseen >= time_diff()).count()
             online_users = User.query.filter(User.lastseen >= time_diff()).count()
 
 
-            # Because we do not have server side sessions, we cannot check if there
-            # are online guests
+            # Because we do not have server side sessions,
+            # we cannot check if there are online guests
             online_guests = None
             online_guests = None
         else:
         else:
             online_users = len(get_online_users())
             online_users = len(get_online_users())
@@ -115,9 +115,12 @@ class ViewPost(MethodView):
     def get(self, post_id):
     def get(self, post_id):
         '''Redirects to a post in a topic.'''
         '''Redirects to a post in a topic.'''
         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.id <= post_id).order_by(Post.id.asc()).count()
-        page = int(math.ceil(post_in_topic / float(flaskbb_config['POSTS_PER_PAGE'])))
+        post_in_topic = Post.query.filter(
+            Post.topic_id == post.topic_id,
+            Post.id <= post_id).order_by(Post.id.asc()).count()
+        page = int(math.ceil(
+            post_in_topic / float(flaskbb_config['POSTS_PER_PAGE'])
+        ))
 
 
         return redirect(
         return redirect(
             url_for(
             url_for(
@@ -143,30 +146,33 @@ class ViewTopic(MethodView):
         topic.views += 1
         topic.views += 1
         topic.save()
         topic.save()
 
 
-
         # Update the topicsread status if the user hasn't read it
         # Update the topicsread status if the user hasn't read it
         forumsread = None
         forumsread = None
         if current_user.is_authenticated:
         if current_user.is_authenticated:
-            forumsread = ForumsRead.query.\
-                filter_by(user_id=current_user.id,
-                          forum_id=topic.forum_id).first()
+            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)
         topic.update_read(real(current_user), topic.forum, forumsread)
 
 
         # fetch the posts in the topic
         # fetch the posts in the topic
-        posts = Post.query.\
-            outerjoin(User, Post.user_id == User.id).\
-            filter(Post.topic_id == topic.id).\
-            add_entity(User).\
-            order_by(Post.id.asc()).\
-            paginate(page, flaskbb_config['POSTS_PER_PAGE'], False)
+        posts = Post.query.outerjoin(
+            User, Post.user_id == User.id
+        ).filter(
+            Post.topic_id == topic.id
+        ).add_entity(
+            User
+        ).order_by(
+            Post.id.asc()
+        ).paginate(page, flaskbb_config['POSTS_PER_PAGE'], False)
 
 
         # Abort if there are no posts on this page
         # Abort if there are no posts on this page
         if len(posts.items) == 0:
         if len(posts.items) == 0:
             abort(404)
             abort(404)
 
 
         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()
         )
         )
 
 
     @allows.requires(CanPostReply)
     @allows.requires(CanPostReply)

+ 37 - 25
flaskbb/plugins/spec.py

@@ -15,7 +15,6 @@ spec = HookspecMarker('flaskbb')
 
 
 
 
 # Setup Hooks
 # Setup Hooks
-
 @spec
 @spec
 def flaskbb_extensions(app):
 def flaskbb_extensions(app):
     """Hook for initializing any plugin loaded extensions."""
     """Hook for initializing any plugin loaded extensions."""
@@ -71,13 +70,30 @@ def flaskbb_cli(cli):
 
 
 @spec
 @spec
 def flaskbb_shell_context():
 def flaskbb_shell_context():
-    """
-    Hook for registering shell context handlers
+    """Hook for registering shell context handlers
     Expected to return a single callable function that returns a dictionary or
     Expected to return a single callable function that returns a dictionary or
     iterable of key value pairs.
     iterable of key value pairs.
     """
     """
 
 
 
 
+# Event hooks
+@spec
+def flaskbb_event_before_post(post):
+    """Hook for handling a post before it has been saved.
+
+    :param flaskbb.forum.models.Post post: The post which triggered the event.
+    """
+
+
+@spec
+def flaskbb_event_after_post(post, is_new):
+    """Hook for handling a post after it has been saved.
+
+    :param flaskbb.forum.models.Post post: The post which triggered the event.
+    :param bool is_new: True if the post 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):
@@ -109,9 +125,8 @@ def flaskbb_form_new_post_save(form):
 
 
 
 
 # Template Hooks
 # Template Hooks
-
 @spec
 @spec
-def flaskbb_tpl_before_navigation():
+def flaskbb_tpl_navigation_before():
     """Hook for registering additional navigation items.
     """Hook for registering additional navigation items.
 
 
     in :file:`templates/layout.html`.
     in :file:`templates/layout.html`.
@@ -119,7 +134,7 @@ def flaskbb_tpl_before_navigation():
 
 
 
 
 @spec
 @spec
-def flaskbb_tpl_after_navigation():
+def flaskbb_tpl_navigation_after():
     """Hook for registering additional navigation items.
     """Hook for registering additional navigation items.
 
 
     in :file:`templates/layout.html`.
     in :file:`templates/layout.html`.
@@ -127,7 +142,7 @@ def flaskbb_tpl_after_navigation():
 
 
 
 
 @spec
 @spec
-def flaskbb_tpl_before_user_nav_loggedin():
+def flaskbb_tpl_user_nav_loggedin_before():
     """Hook for registering additional user navigational items
     """Hook for registering additional user navigational items
     which are only shown when a user is logged in.
     which are only shown when a user is logged in.
 
 
@@ -136,7 +151,7 @@ def flaskbb_tpl_before_user_nav_loggedin():
 
 
 
 
 @spec
 @spec
-def flaskbb_tpl_after_user_nav_loggedin():
+def flaskbb_tpl_user_nav_loggedin_after():
     """Hook for registering additional user navigational items
     """Hook for registering additional user navigational items
     which are only shown when a user is logged in.
     which are only shown when a user is logged in.
 
 
@@ -145,38 +160,46 @@ def flaskbb_tpl_after_user_nav_loggedin():
 
 
 
 
 @spec
 @spec
-def flaskbb_tpl_before_registration_form():
+def flaskbb_tpl_form_registration_before(form):
     """This hook is emitted in the Registration form **before** the first
     """This hook is emitted in the Registration form **before** the first
     input field but after the hidden CSRF token field.
     input field but after the hidden CSRF token field.
 
 
     in :file:`templates/auth/register.html`.
     in :file:`templates/auth/register.html`.
+
+    :param form: The form object.
     """
     """
 
 
 
 
 @spec
 @spec
-def flaskbb_tpl_after_registration_form():
+def flaskbb_tpl_form_registration_after(form):
     """This hook is emitted in the Registration form **after** the last
     """This hook is emitted in the Registration form **after** the last
     input field but before the submit field.
     input field but before the submit field.
 
 
     in :file:`templates/auth/register.html`.
     in :file:`templates/auth/register.html`.
+
+    :param form: The form object.
     """
     """
 
 
 
 
 @spec
 @spec
-def flaskbb_tpl_before_user_details_form():
+def flaskbb_tpl_form_user_details_before(form):
     """This hook is emitted in the Change User Details form **before** an
     """This hook is emitted in the Change User Details form **before** an
     input field is rendered.
     input field is rendered.
 
 
     in :file:`templates/user/change_user_details.html`.
     in :file:`templates/user/change_user_details.html`.
+
+    :param form: The form object.
     """
     """
 
 
 
 
 @spec
 @spec
-def flaskbb_tpl_after_user_details_form():
+def flaskbb_tpl_form_user_details_after(form):
     """This hook is emitted in the Change User Details form **after** the last
     """This hook is emitted in the Change User Details form **after** the last
     input field has been rendered but before the submit field.
     input field has been rendered but before the submit field.
 
 
     in :file:`templates/user/change_user_details.html`.
     in :file:`templates/user/change_user_details.html`.
+
+    :param form: The form object.
     """
     """
 
 
 
 
@@ -237,17 +260,6 @@ def flaskbb_tpl_admin_settings_menu(user):
     """
     """
 
 
 
 
-# Event hooks
-
-@spec
-def flaskbb_event_after_post(post, is_new):
-    """Hook for handling a post after it has been saved.
-
-    :param flaskbb.forum.models.Post post: The post which triggered the event
-    :param bool is_new: True if the post is new, False if it is an edit
-    """
-
-
 @spec
 @spec
 def flaskbb_tpl_profile_sidebar_stats(user):
 def flaskbb_tpl_profile_sidebar_stats(user):
     """This hook is emitted on the users profile page below the standard
     """This hook is emitted on the users profile page below the standard
@@ -261,7 +273,7 @@ def flaskbb_tpl_profile_sidebar_stats(user):
 
 
 
 
 @spec
 @spec
-def flaskbb_tpl_before_post_author_info(user, post):
+def flaskbb_tpl_post_author_info_before(user, post):
     """This hook is emitted before the information about the
     """This hook is emitted before the information about the
     author of a post is displayed (but after the username).
     author of a post is displayed (but after the username).
 
 
@@ -273,7 +285,7 @@ def flaskbb_tpl_before_post_author_info(user, post):
 
 
 
 
 @spec
 @spec
-def flaskbb_tpl_after_post_author_info(user, post):
+def flaskbb_tpl_post_author_info_after(user, post):
     """This hook is emitted after the information about the
     """This hook is emitted after the information about the
     author of a post is displayed (but after the username).
     author of a post is displayed (but after the username).
 
 

+ 2 - 2
flaskbb/templates/auth/register.html

@@ -13,7 +13,7 @@
     <div class="panel-body">
     <div class="panel-body">
         <form class="form-horizontal" role="form" method="POST">
         <form class="form-horizontal" role="form" method="POST">
             {{ form.hidden_tag() }}
             {{ form.hidden_tag() }}
-            {{ run_hook('flaskbb_tpl_before_registration_form') }}
+            {{ run_hook('flaskbb_tpl_form_registration_before', form=form) }}
             {{ horizontal_field(form.username)}}
             {{ horizontal_field(form.username)}}
             {{ horizontal_field(form.email)}}
             {{ horizontal_field(form.email)}}
             {{ horizontal_field(form.password)}}
             {{ horizontal_field(form.password)}}
@@ -25,8 +25,8 @@
 
 
             {{ horizontal_field(form.language) }}
             {{ horizontal_field(form.language) }}
             {{ horizontal_field(form.accept_tos)}}
             {{ horizontal_field(form.accept_tos)}}
+            {{ run_hook('flaskbb_tpl_form_registration_after', form=form) }}
             {{ horizontal_field(form.submit)}}
             {{ horizontal_field(form.submit)}}
-            {{ run_hook('flaskbb_tpl_after_registration_form') }}
         </form>
         </form>
     </div>
     </div>
 </div>
 </div>

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

@@ -43,7 +43,7 @@
                     {% endif %}
                     {% endif %}
                     <div class="author-title"><h5>{{ user.primary_group.name }}</h5></div>
                     <div class="author-title"><h5>{{ user.primary_group.name }}</h5></div>
 
 
-                    {{ run_hook("flaskbb_tpl_before_post_author_info", user=user, post=post) }}
+                    {{ run_hook("flaskbb_tpl_post_author_info_before", user=user, post=post) }}
 
 
                     {% if user.avatar %}
                     {% if user.avatar %}
                         <div class="author-avatar"><img src="{{ user.avatar }}" alt="avatar"></div>
                         <div class="author-avatar"><img src="{{ user.avatar }}" alt="avatar"></div>
@@ -52,7 +52,7 @@
                     <div class="author-registered">{% trans %}Joined{% endtrans %}: {{ user.date_joined|format_date('%b %d %Y') }}</div>
                     <div class="author-registered">{% trans %}Joined{% endtrans %}: {{ user.date_joined|format_date('%b %d %Y') }}</div>
                     <div class="author-posts">{% trans %}Posts{% endtrans %}: {{ user.post_count }}</div>
                     <div class="author-posts">{% trans %}Posts{% endtrans %}: {{ user.post_count }}</div>
 
 
-                    {{ run_hook("flaskbb_tpl_after_post_author_info", user=user, post=post) }}
+                    {{ run_hook("flaskbb_tpl_post_author_info_after", user=user, post=post) }}
 
 
                     {% else %}
                     {% else %}
                     <!-- user deleted or guest -->
                     <!-- user deleted or guest -->

+ 4 - 4
flaskbb/templates/layout.html

@@ -73,18 +73,18 @@
                         <ul class="nav navbar-nav forum-nav">
                         <ul class="nav navbar-nav forum-nav">
                             {%- from theme("macros.html") import is_active, topnav with context -%}
                             {%- from theme("macros.html") import is_active, topnav with context -%}
 
 
-                            {{ run_hook("flaskbb_tpl_before_navigation") }}
+                            {{ run_hook("flaskbb_tpl_navigation_before") }}
                             {{ topnav(endpoint='forum.index', name=_('Forum'), icon='fa fa-comment', active=active_forum_nav) }}
                             {{ topnav(endpoint='forum.index', name=_('Forum'), icon='fa fa-comment', active=active_forum_nav) }}
                             {{ topnav(endpoint='forum.memberlist', name=_('Memberlist'), icon='fa fa-user') }}
                             {{ topnav(endpoint='forum.memberlist', name=_('Memberlist'), icon='fa fa-user') }}
                             {{ topnav(endpoint='forum.search', name=_('Search'), icon='fa fa-search') }}
                             {{ topnav(endpoint='forum.search', name=_('Search'), icon='fa fa-search') }}
-                            {{ run_hook("flaskbb_tpl_after_navigation") }}
+                            {{ run_hook("flaskbb_tpl_navigation_after") }}
                         </ul>
                         </ul>
 
 
                         <!-- navbar right -->
                         <!-- navbar right -->
                         <ul class="nav navbar-nav navbar-right">
                         <ul class="nav navbar-nav navbar-right">
                             {% if current_user and current_user.is_authenticated %}
                             {% if current_user and current_user.is_authenticated %}
 
 
-                            {{ run_hook("flaskbb_tpl_before_user_nav_loggedin") }}
+                            {{ run_hook("flaskbb_tpl_user_nav_loggedin_before") }}
 
 
                             <!-- User Menu -->
                             <!-- User Menu -->
                             <li>
                             <li>
@@ -108,7 +108,7 @@
                                 </div>
                                 </div>
                             </li>
                             </li>
 
 
-                            {{ run_hook("flaskbb_tpl_after_user_nav_loggedin") }}
+                            {{ run_hook("flaskbb_tpl_user_nav_loggedin_after") }}
 
 
                             {% else %}
                             {% else %}
                             <!-- Not logged in - Login/Register -->
                             <!-- Not logged in - Login/Register -->

+ 0 - 1
flaskbb/templates/management/plugins.html

@@ -76,7 +76,6 @@
                         <a class="btn btn-info" href="{{ url_for('management.settings', plugin=plugin.name) }}">Settings</a>
                         <a class="btn btn-info" href="{{ url_for('management.settings', plugin=plugin.name) }}">Settings</a>
                         {% endif %}
                         {% endif %}
                     </div>
                     </div>
-                    </div>
                 </div>
                 </div>
                 {% endfor %}
                 {% endfor %}
             </div>
             </div>

+ 2 - 2
flaskbb/templates/user/change_user_details.html

@@ -10,7 +10,7 @@
     <div class="panel-body page-body">
     <div class="panel-body page-body">
         <form class="form-horizontal" role="form" method="POST">
         <form class="form-horizontal" role="form" method="POST">
             {{ form.hidden_tag() }}
             {{ form.hidden_tag() }}
-            {{ run_hook('flaskbb_tpl_before_user_details_form') }}
+            {{ run_hook('flaskbb_tpl_form_user_details_before', form=form) }}
             {{ horizontal_select_field(form.birthday, select_class="form-control", surrounded_div="col-sm-4") }}
             {{ horizontal_select_field(form.birthday, select_class="form-control", surrounded_div="col-sm-4") }}
             {{ horizontal_field(form.gender) }}
             {{ horizontal_field(form.gender) }}
             {{ horizontal_field(form.location) }}
             {{ horizontal_field(form.location) }}
@@ -18,7 +18,7 @@
             {{ horizontal_field(form.avatar) }}
             {{ horizontal_field(form.avatar) }}
             {{ horizontal_field(form.signature, div_class="col-sm-8 editor", rows="5", placeholder="", **{'data-provide': 'markdown', 'class': 'flaskbb-editor'}) }}
             {{ horizontal_field(form.signature, div_class="col-sm-8 editor", rows="5", placeholder="", **{'data-provide': 'markdown', 'class': 'flaskbb-editor'}) }}
             {{ horizontal_field(form.notes, div_class="col-sm-8 editor", rows="12", placeholder="", **{'data-provide': 'markdown', 'class': 'flaskbb-editor'}) }}
             {{ horizontal_field(form.notes, div_class="col-sm-8 editor", rows="12", placeholder="", **{'data-provide': 'markdown', 'class': 'flaskbb-editor'}) }}
-            {{ run_hook('flaskbb_tpl_after_user_details_form') }}
+            {{ run_hook('flaskbb_tpl_form_user_details_after', form=form) }}
             {{ horizontal_field(form.submit) }}
             {{ horizontal_field(form.submit) }}
 
 
             {% include theme('editor_help.html') %}
             {% include theme('editor_help.html') %}