Browse Source

Started writing a portal plugin. Still a WIP

sh4nks 11 years ago
parent
commit
a9066951e0

+ 15 - 1
flaskbb/admin/views.py

@@ -14,7 +14,7 @@ from datetime import datetime
 from flask import (Blueprint, current_app, request, redirect, url_for, flash,
                    __version__ as flask_version)
 from flask.ext.login import current_user
-from flask.ext.plugins import get_plugins_list
+from flask.ext.plugins import get_plugins_list, get_plugin
 
 from flaskbb import __version__ as flaskbb_version
 from flaskbb.forum.forms import UserSearchForm
@@ -101,6 +101,20 @@ def plugins():
     return render_template("admin/plugins.html", plugins=get_plugins_list())
 
 
+@admin.route("/plugins/enable/<plugin>")
+def enable_plugin(plugin):
+    plugin = get_plugin(plugin)
+    current_app.plugin_manager.enable_plugins([plugin])
+    return redirect(url_for("admin.plugins"))
+
+
+@admin.route("/plugins/disable/<plugin>")
+def disable_plugin(plugin):
+    plugin = get_plugin(plugin)
+    current_app.plugin_manager.disable_plugins([plugin])
+    return redirect(url_for("admin.plugins"))
+
+
 @admin.route("/reports/unread")
 @admin_required
 def unread_reports():

+ 0 - 4
flaskbb/forum/views.py

@@ -27,7 +27,6 @@ from flaskbb.forum.models import (Category, Forum, Topic, Post, ForumsRead,
 from flaskbb.forum.forms import (QuickreplyForm, ReplyForm, NewTopicForm,
                                  ReportForm, UserSearchForm, SearchPageForm)
 from flaskbb.user.models import User
-from flaskbb.hooks import hooks
 
 forum = Blueprint("forum", __name__)
 
@@ -42,9 +41,6 @@ def index():
     post_count = Post.query.count()
     newest_user = User.query.order_by(User.id.desc()).first()
 
-    current_app.logger.debug("Runnnig beforeIndex hook...")
-    hooks.call("beforeIndex")
-
     # Check if we use redis or not
     if not current_app.config["REDIS_ENABLED"]:
         online_users = User.query.filter(User.lastseen >= time_diff()).count()

+ 2 - 4
flaskbb/hooks.py

@@ -2,7 +2,5 @@ from flask.ext.plugins import HookManager
 
 hooks = HookManager()
 
-hooks.new("beforeIndex")
-hooks.new("afterIndex")
-hooks.new("beforeBreadcrumb")
-hooks.new("afterBreadcrumb")
+hooks.new("tmpl_before_navigation")
+hooks.new("tmpl_after_navigation")

+ 45 - 0
flaskbb/plugins/__init__.py

@@ -0,0 +1,45 @@
+from flask.ext.plugins import Plugin
+from flask import current_app
+
+
+class FlaskBBPlugin(Plugin):
+        # Some helpers
+    def register_blueprint(self, blueprint, **kwargs):
+        """Registers a blueprint."""
+        current_app.register_blueprint(blueprint, **kwargs)
+
+    def create_table(self, model, db):
+        """Creates the relation for the model
+
+        :param model: The Model which should be created
+        :param db: The database instance.
+        """
+        if not model.__table__.exists(bind=db.engine):
+            model.__table__.create(bind=db.engine)
+
+    def drop_table(self, model, db):
+        """Drops the relation for the bounded model.
+
+        :param model: The model on which the table is bound.
+        :param db: The database instance.
+        """
+        model.__table__.drop(bind=db.engine)
+
+    def create_all_tables(self, models, db):
+        """A interface for creating all models specified in ``models``.
+
+        :param models: A list with models
+        :param db: The database instance
+        """
+        for model in models:
+            self.create_table(model, db)
+
+    def drop_all_tables(self, models, db):
+        """A interface for dropping all models specified in the
+        variable ``models``.
+
+        :param models: A list with models
+        :param db: The database instance.
+        """
+        for model in models:
+            self.drop_table(model, db)

+ 0 - 44
flaskbb/plugins/example/__init__.py

@@ -1,44 +0,0 @@
-from flask import flash
-from flask.ext.plugins import Plugin
-
-from flaskbb.hooks import hooks
-
-#: The name of your plugin class
-__version__ = "1.0.0"
-__plugin__ = "ExamplePlugin"
-
-
-def hello_world():
-    flash("Hello World from {}".format(__plugin__), "success")
-
-
-def inject_hello_world():
-    return "<b>Hello World</b>"
-
-
-class ExamplePlugin(Plugin):
-
-    name = "Example Plugin"
-
-    description = ("This plugin gives a quick insight on how you can write "
-                   "plugins in FlaskBB.")
-
-    author = "sh4nks"
-
-    license = "BSD License. See LICENSE file for more information."
-
-    version = __version__
-
-    def enable(self):
-        hooks.add("beforeIndex", hello_world)
-        hooks.add("beforeBreadcrumb", inject_hello_world)
-
-    def disable(self):
-        hooks.remove("beforeIndex", hello_world)
-        hooks.remove("beforeBreadcrumb", inject_hello_world)
-
-    def install(self):
-        pass
-
-    def uninstall(self):
-        pass

+ 35 - 0
flaskbb/plugins/portal/__init__.py

@@ -0,0 +1,35 @@
+from flaskbb.hooks import hooks
+from flaskbb.plugins import FlaskBBPlugin
+
+from .views import portal, inject_portal_link
+
+__version__ = "0.1"
+__plugin__ = "PortalPlugin"
+
+
+class PortalPlugin(FlaskBBPlugin):
+
+    name = "Portal Plugin"
+
+    description = ("This Plugin provides a simple portal for FlaskBB.")
+
+    author = "sh4nks"
+
+    license = "BSD"
+
+    version = __version__
+
+    def setup(self):
+        self.register_blueprint(portal, url_prefix="/portal")
+
+    def enable(self):
+        hooks.add("tmpl_before_navigation", inject_portal_link)
+
+    def disable(self):
+        hooks.remove("tmpl_before_navigation", inject_portal_link)
+
+    def install(self):
+        pass
+
+    def uninstall(self):
+        pass

+ 0 - 0
flaskbb/plugins/portal/models.py


+ 92 - 0
flaskbb/plugins/portal/templates/index.html

@@ -0,0 +1,92 @@
+{% extends theme("layout.html") %}
+
+{% block content %}
+  <div class="container main-content">
+    <div class="row">
+
+      <!-- Left -->
+      <div class="col-md-4">
+        <div class="panel panel-default">
+          <div class="panel-heading">
+            <h3 class="panel-title">Lorem Ipsum</h3>
+          </div>
+          <div class="panel-body">
+
+
+          </div> <!-- /.panel-body -->
+        </div>
+
+        <div class="panel panel-default">
+          <div class="panel-heading">
+            <h3 class="panel-title">Lorem Ipsum</h3>
+          </div>
+          <div class="panel-body">
+
+          </div>
+        </div>
+      </div>
+
+      <!-- Middle -->
+      <div class="col-md-4">
+        <div class="panel panel-default">
+          <div class="panel-heading">
+            <form class="form-inline" role="form">
+              <div class="form-group">
+                <label class="sr-only" for="searchManga">Search</label>
+                <input type="email" class="form-control" id="search" placeholder="Search Topics">
+              </div>
+            </form>
+          </div>
+        </div>
+
+        <div class="panel panel-default">
+          <div class="panel-heading">
+            <h3 class="panel-title">Lorem Ipsum</h3>
+          </div>
+          <div class="panel-body">
+          </div> <!-- /.panel-body -->
+        </div>
+      </div>
+
+      <!-- Right -->
+      <div class="col-md-4">
+        <div class="panel panel-default">
+          <div class="panel-heading">
+            <h3 class="panel-title">Login</h3>
+          </div>
+          <div class="panel-body">
+            <form role="form">
+              <div class="form-group">
+                <label for="username">Username</label>
+                <input type="email" class="form-control" id="username" placeholder="username">
+              </div>
+              <div class="form-group">
+                <label for="exampleInputPassword1">Password</label>
+                <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
+              </div>
+              <div class="checkbox">
+                <label>
+                  <input type="checkbox"> Remember Me
+                </label>
+              </div>
+              <button type="submit" class="btn btn-default">Login</button>
+            </form>
+          </div>
+        </div>
+
+        <div class="panel panel-default">
+          <div class="panel-heading">
+            <h3 class="panel-title">Lorem Ipsum</h3>
+          </div>
+          <div class="panel-body">
+
+          </div>
+        </div>
+      </div>
+
+    </div>
+  </div>
+
+
+
+{% endblock %}

+ 5 - 0
flaskbb/plugins/portal/templates/navigation_snippet.html

@@ -0,0 +1,5 @@
+<li {% if 'portal.index' == request.endpoint %}class="active"{% endif %}>
+    <a href={{ url_for('portal.index') }}>
+        <i class="fa fa-home"></i> Portal
+    </a>
+</li>

+ 18 - 0
flaskbb/plugins/portal/views.py

@@ -0,0 +1,18 @@
+from flask import Blueprint
+from flaskbb.utils.helpers import render_template
+from flaskbb.forum.models import Topic
+
+
+FORUM_IDS = [1]
+
+portal = Blueprint("portal", __name__, template_folder="templates")
+
+
+def inject_portal_link():
+    return render_template("navigation_snippet.html")
+
+
+@portal.route("/")
+def index():
+    topics = Topic.query.filter(Topic.forum_id.in_(FORUM_IDS)).all()
+    return render_template("index.html", topics=topics)

+ 2 - 0
flaskbb/templates/admin/plugins.html

@@ -22,6 +22,8 @@
                 {{ plugin.author }}
             </td>
             <td>
+                <a href="{{ url_for('admin.enable_plugin', plugin=plugin.name) }}">Enable</a> |
+                <a href="{{ url_for('admin.disable_plugin', plugin=plugin.name) }}">Disable</a>
             </td>
         </tr>
         {% endfor %}

+ 0 - 2
flaskbb/templates/forum/index.html

@@ -1,8 +1,6 @@
 {% extends theme("layout.html") %}
 {% block content %}
 
-    {{ hooks.call("beforeBreadcrumb") | safe }}
-
 <ol class="breadcrumb">
     <li><a href="{{ url_for('forum.index') }}">Forum</a></li>
 </ol>

+ 4 - 0
flaskbb/templates/layout.html

@@ -43,10 +43,14 @@
                     </div>
                     <div class="collapse navbar-collapse navbar-ex1-collapse">
                         <ul class="nav navbar-nav">
+                            {{ hooks.run_template_hook("tmpl_before_navigation") | safe }}
+
                             {# active_forum_nav is set in {forum, category, topic}.html and new_{topic, post}.html #}
                             {{ 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.search', name='Search', icon='fa fa-search') }}
+
+                            {{ hooks.run_template_hook("tmpl_after_navigation") | safe }}
                         </ul>
 
                     {% if current_user and current_user.is_authenticated() %}

+ 4 - 0
flaskbb/themes/bootstrap3/templates/layout.html

@@ -43,10 +43,14 @@
                     </div>
                     <div class="collapse navbar-collapse navbar-ex1-collapse">
                         <ul class="nav navbar-nav">
+                            {{ hooks.run_template_hook("tmpl_before_navigation") | safe }}
+
                             {# active_forum_nav is set in {forum, category, topic}.html and new_{topic, post}.html #}
                             {{ 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.search', name='Search', icon='fa fa-search') }}
+
+                            {{ hooks.run_template_hook("tmpl_after_navigation") | safe }}
                         </ul>
 
                     {% if current_user and current_user.is_authenticated() %}