Browse Source

Add hook for registering admin panels

Peter Justin 7 years ago
parent
commit
bec0ba8906

+ 14 - 0
flaskbb/management/__init__.py

@@ -1,3 +1,17 @@
+# -*- coding: utf-8 -*-
+"""
+    flaskbb.management
+    ~~~~~~~~~~~~~~~~~~
+
+    This module contains models, forms and views relevant
+    for managing FlaskBB
+
+    :copyright: (c) 2014 by the FlaskBB Team.
+    :license: BSD, see LICENSE for more details.
+"""
 import logging
 
+# force plugins to be loaded
+from . import plugins
+
 logger = logging.getLogger(__name__)

+ 32 - 0
flaskbb/management/plugins.py

@@ -0,0 +1,32 @@
+from itertools import chain
+from pluggy import HookimplMarker
+from flask_allows import Permission
+
+
+impl = HookimplMarker('flaskbb')
+
+
+@impl(hookwrapper=True, tryfirst=True)
+def flaskbb_tpl_admin_settings_menu(user):
+    """
+    Flattens the lists that come back from the hook
+    into a single iterable that can be used to populate
+    the menu
+    """
+    from flaskbb.utils.requirements import IsAdmin  # noqa: circular dependency
+    results = [
+        ('management.overview', 'Overview', 'fa fa-tasks'),
+        ('management.unread_reports', 'Reports', 'fa fa-flag'),
+        ('management.users', 'Users', 'fa fa-user')
+    ]
+
+    if Permission(IsAdmin, identity=user):
+        results.extend([
+            ('management.groups', 'Groups', 'fa fa-users'),
+            ('management.forums', 'Forums', 'fa fa-comments'),
+            ('management.settings', 'Settings', 'fa fa-cogs'),
+            ('management.plugins', 'Plugins', 'fa fa-puzzle-piece')
+        ])
+
+    outcome = yield
+    outcome.force_result(chain(results, *outcome.get_result()))

+ 34 - 0
flaskbb/plugins/spec.py

@@ -55,6 +55,12 @@ def flaskbb_jinja_directives(app):
 def flaskbb_additional_setup(app, pluggy):
     """Hook for any additional setup a plugin wants to do after all other
     application setup has finished.
+
+    For example, you could apply a WSGI middleware::
+
+        @impl
+        def flaskbb_additional_setup(app):
+            app.wsgi_app = ProxyFix(app.wsgi_app)
     """
 
 
@@ -116,6 +122,7 @@ def flaskbb_tpl_after_user_details_form():
     in :file:`templates/user/change_user_details.html`.
     """
 
+
 @spec
 def flaskbb_tpl_profile_settings_menu():
     """This hook is emitted on the user settings page in order to populate the
@@ -144,3 +151,30 @@ def flaskbb_tpl_profile_settings_menu():
 
     in :file:`templates/user/settings_layout.html`
     """
+
+
+@spec
+def flaskbb_tpl_admin_settings_menu(user):
+    """This hook is emitted in the admin panel and used to add additional
+    navigation links to the admin menu.
+
+    Implementations of this hook should return a list of tuples
+    that are view name, display text and optionally an icon.
+    The display text will be provided to the translation service so it
+    is unnecessary to supply translated text.
+
+    For example::
+
+        @impl(hookwrapper=True, tryfirst=True)
+        def flaskbb_tpl_admin_settings_menu():
+            # only add this item if the user is an admin
+            if Permission(IsAdmin, identity=current_user):
+                return [
+                    ("myplugin.foobar", "Foobar", "fa fa-foobar")
+                ]
+
+    Hookwrappers for this spec should not be registered as FlaskBB
+    supplies its own hookwrapper to flatten all the lists into a single list.
+
+    in :file:`templates/management/management_layout.html`
+    """

+ 2 - 2
flaskbb/templates/macros.html

@@ -308,8 +308,8 @@
     {%- endif -%}
 {% endmacro %}
 
-{% macro navlink(endpoint, name, icon='', active=False) %}
-<li {% if endpoint == request.endpoint or active %}class="active"{% endif %}>
+{% macro navlink(endpoint, name, icon='', active='') %}
+<li {% if endpoint == request.endpoint or endpoint == active or active == True %}class="active"{% endif %}>
     <a href="{{ url_for(endpoint) }}">{% if icon %}<i class="{{ icon }}"></i> {% endif %} {{ name }}</a>
 </li>
 {% endmacro %}

+ 2 - 2
flaskbb/templates/management/banned_users.html

@@ -1,5 +1,5 @@
 {% set page_title = _("Banned Users") %}
-{% set active_management_user_nav=True %}
+{% set active = "management.users" %}
 
 {% extends theme("management/management_layout.html") %}
 
@@ -12,7 +12,7 @@
 {% endblock %}
 
 {% block management_content %}
-{% from theme('macros.html') import render_pagination, group_field,navlink with context %}
+{% from theme('macros.html') import render_pagination, group_field, navlink with context %}
 
 <div class="col-md-3 settings-col">
     <div class="nav-sidebar">

+ 1 - 1
flaskbb/templates/management/category_form.html

@@ -1,5 +1,5 @@
 {% set page_title = title %}
-{% set active_management_forum_nav=True %}
+{% set active = "management.forums" %}
 
 {% extends theme("management/management_layout.html") %}
 

+ 1 - 1
flaskbb/templates/management/forum_form.html

@@ -1,5 +1,5 @@
 {% set page_title = title %}
-{% set active_management_forum_nav=True %}
+{% set active = "management.forums" %}
 
 {% extends theme("management/management_layout.html") %}
 

+ 1 - 1
flaskbb/templates/management/group_form.html

@@ -1,5 +1,5 @@
 {% set page_title = title %}
-{% set active_management_group_nav=True %}
+{% set active = "management.groups" %}
 
 {% extends theme("management/management_layout.html") %}
 

+ 3 - 11
flaskbb/templates/management/management_layout.html

@@ -9,17 +9,9 @@
 <div class="panel panel-tabs management-panel">
     <div class="panel-heading management-head">
         <ul class="nav nav-tabs nav-justified">
-        {{ navlink('management.overview', _('Overview'), 'fa fa-tasks') }}
-
-        {% if current_user|is_admin %}
-            {{ navlink('management.settings', _('Settings'), 'fa fa-cogs') }}
-            {{ navlink('management.forums', _('Forums'), 'fa fa-comments',active=active_management_forum_nav) }}
-            {{ navlink('management.plugins', _('Plugins'), 'fa fa-puzzle-piece') }}
-            {{ navlink('management.groups', _('Groups'), 'fa fa-users', active=active_management_group_nav) }}
-        {% endif %}
-
-        {{ navlink('management.users', _('Users'), 'fa fa-user', active=active_management_user_nav) }}
-        {{ navlink('management.unread_reports', _('Reports'), 'fa fa-flag', active=active_management_report_nav) }}
+        {% for view, text, icon in run_hook('flaskbb_tpl_admin_settings_menu', user=current_user, is_markup=False) %}
+            {{ navlink(view, _(text), icon, active) }}
+        {% endfor %}
         </ul>
     </div>
     <div class="panel-body management-body">

+ 1 - 1
flaskbb/templates/management/reports.html

@@ -1,5 +1,5 @@
 {% set page_title = _("Reports") %}
-{% set active_management_report_nav=True %}
+{% set active = "management.unread_reports" %}
 
 {% extends theme("management/management_layout.html") %}
 

+ 1 - 1
flaskbb/templates/management/user_form.html

@@ -1,5 +1,5 @@
 {% set page_title = title %}
-{% set active_management_user_nav=True %}
+{% set active = "management.users" %}
 
 {% extends theme("management/management_layout.html") %}
 

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

@@ -1,5 +1,4 @@
 {% set page_title = _("Users") %}
-{% set active_management_user_nav=True %}
 
 {% extends theme("management/management_layout.html") %}