From 8545dd50f0cd588d505c217d367450198199a2b0 Mon Sep 17 00:00:00 2001 From: Will Kahn-Greene Date: Sun, 10 Jun 2012 11:50:14 -0400 Subject: [PATCH] Flatpages first pass This fixes the template loader so that it can load plugin templates. This adds code for registering template paths so that plugins can add their own templates. This adds the base code for the flatpagesfile plugin. It doesn't serve pages, yet, but it's pretty close. --- mediagoblin/app.py | 5 +- mediagoblin/init/__init__.py | 45 +++++++++-------- mediagoblin/plugins/flatpagesfile/README | 50 +++++++++++++++++++ mediagoblin/plugins/flatpagesfile/__init__.py | 20 ++++++++ mediagoblin/plugins/flatpagesfile/main.py | 41 +++++++++++++++ .../templates/flatpagesfile/base.html | 18 +++++++ mediagoblin/tools/pluginapi.py | 34 ++++++++++++- 7 files changed, 190 insertions(+), 23 deletions(-) create mode 100644 mediagoblin/plugins/flatpagesfile/README create mode 100644 mediagoblin/plugins/flatpagesfile/__init__.py create mode 100644 mediagoblin/plugins/flatpagesfile/main.py create mode 100644 mediagoblin/plugins/flatpagesfile/templates/flatpagesfile/base.html diff --git a/mediagoblin/app.py b/mediagoblin/app.py index dfc715b9..4870b89c 100644 --- a/mediagoblin/app.py +++ b/mediagoblin/app.py @@ -32,6 +32,7 @@ from mediagoblin.init.plugins import setup_plugins from mediagoblin.init import (get_jinja_loader, get_staticdirector, setup_global_and_app_config, setup_workbench, setup_database, setup_storage, setup_beaker_cache) +from mediagoblin.tools.pluginapi import PluginCache _log = logging.getLogger(__name__) @@ -80,7 +81,9 @@ class MediaGoblinApp(object): # Get the template environment self.template_loader = get_jinja_loader( app_config.get('local_templates'), - self.current_theme) + self.current_theme, + PluginCache().get_template_paths() + ) # Set up storage systems self.public_store, self.queue_store = setup_storage() diff --git a/mediagoblin/init/__init__.py b/mediagoblin/init/__init__.py index f7bef421..9b0025c9 100644 --- a/mediagoblin/init/__init__.py +++ b/mediagoblin/init/__init__.py @@ -71,7 +71,8 @@ def setup_database(): return connection, db -def get_jinja_loader(user_template_path=None, current_theme=None): +def get_jinja_loader(user_template_path=None, current_theme=None, + plugin_template_paths=None): """ Set up the Jinja template loaders, possibly allowing for user overridden templates. @@ -79,26 +80,28 @@ def get_jinja_loader(user_template_path=None, current_theme=None): (In the future we may have another system for providing theming; for now this is good enough.) """ - if user_template_path or current_theme: - loader_choices = [] - - # user template overrides - if user_template_path: - loader_choices.append(jinja2.FileSystemLoader(user_template_path)) - - # Any theme directories in the registry - if current_theme and current_theme.get('templates_dir'): - loader_choices.append( - jinja2.FileSystemLoader( - current_theme['templates_dir'])) - - # Add the main mediagoblin templates - loader_choices.append( - jinja2.PackageLoader('mediagoblin', 'templates')) - - return jinja2.ChoiceLoader(loader_choices) - else: - return jinja2.PackageLoader('mediagoblin', 'templates') + path_list = [] + + # Add user path first--this takes precedence over everything. + if user_template_path is not None: + path_list.append(jinja2.FileSystemLoader(user_template_path)) + + # Any theme directories in the registry + if current_theme and current_theme.get('templates_dir'): + path_list.append( + jinja2.FileSystemLoader( + current_theme['templates_dir'])) + + # Add plugin template paths next--takes precedence over + # core templates. + if plugin_template_paths is not None: + path_list.extend((jinja2.FileSystemLoader(path) + for path in plugin_template_paths)) + + # Add core templates last. + path_list.append(jinja2.PackageLoader('mediagoblin', 'templates')) + + return jinja2.ChoiceLoader(path_list) def get_staticdirector(app_config): diff --git a/mediagoblin/plugins/flatpagesfile/README b/mediagoblin/plugins/flatpagesfile/README new file mode 100644 index 00000000..23a675e1 --- /dev/null +++ b/mediagoblin/plugins/flatpagesfile/README @@ -0,0 +1,50 @@ +======== + README +======== + +This is the flatpages file plugin. It allows you to add pages to your +MediaGoblin instance which are not generated from user content. For +example, this is useful for these pages: + +* About this site +* Terms of service +* Privacy policy +* How to get an account here +* ... + + +How to add pages +================ + +To add pages, you must edit template files on the file system in your +`local_templates` directory. + +The directory structure looks kind of like this:: + + local_templates + |- flatpagesfile + |- flatpage1.html + |- flatpage2.html + |- ... + + +The ``.html`` file contains the content of your page. It's just a +template like all the other templates you have. + +Here's an example:: + + {% extends "flatpagesfile/base.html" %} + {% block mediagoblin_content %} +

About this site

+

+ This site is a MediaGoblin instance set up to host media for + me, my family and my friends. +

+ {% endblock %} + + +.. Note:: + + If you have a bunch of flatpages that kind of look like one + another, take advantage of Jinja2 template extending and create a + base template that the others extend. diff --git a/mediagoblin/plugins/flatpagesfile/__init__.py b/mediagoblin/plugins/flatpagesfile/__init__.py new file mode 100644 index 00000000..69c40a77 --- /dev/null +++ b/mediagoblin/plugins/flatpagesfile/__init__.py @@ -0,0 +1,20 @@ +# GNU MediaGoblin -- federated, autonomous media hosting +# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + + +# This imports the main module which has the FlatPagesPlugin class +# which does all the work. +import mediagoblin.plugins.flatpagesfile.main diff --git a/mediagoblin/plugins/flatpagesfile/main.py b/mediagoblin/plugins/flatpagesfile/main.py new file mode 100644 index 00000000..b73f9b90 --- /dev/null +++ b/mediagoblin/plugins/flatpagesfile/main.py @@ -0,0 +1,41 @@ +# GNU MediaGoblin -- federated, autonomous media hosting +# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +import logging +import os + +from mediagoblin.tools import pluginapi + + +PLUGIN_DIR = os.path.dirname(__file__) + + +_log = logging.getLogger(__name__) + + +class FlatpagesPlugin(pluginapi.Plugin): + """ + This is the flatpages plugin class. See the README for how to use + flatpages. + """ + def __init__(self): + self._setup_plugin_called = 0 + + def setup_plugin(self): + self.config = pluginapi.get_config('mediagoblin.plugins.flatpagesfile') + + _log.info('Setting up flatpages....') + pluginapi.register_template_path(os.path.join(PLUGIN_DIR, 'templates')) diff --git a/mediagoblin/plugins/flatpagesfile/templates/flatpagesfile/base.html b/mediagoblin/plugins/flatpagesfile/templates/flatpagesfile/base.html new file mode 100644 index 00000000..2b72a477 --- /dev/null +++ b/mediagoblin/plugins/flatpagesfile/templates/flatpagesfile/base.html @@ -0,0 +1,18 @@ +{# +# GNU MediaGoblin -- federated, autonomous media hosting +# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +-#} +{% extend "'mediagoblin/base.html" %} diff --git a/mediagoblin/tools/pluginapi.py b/mediagoblin/tools/pluginapi.py index 74d05def..1f5a9887 100644 --- a/mediagoblin/tools/pluginapi.py +++ b/mediagoblin/tools/pluginapi.py @@ -76,7 +76,10 @@ class PluginCache(object): "plugin_classes": [], # list of plugin objects - "plugin_objects": [] + "plugin_objects": [], + + # list of registered template paths + "template_paths": set(), } def clear(self): @@ -95,6 +98,14 @@ class PluginCache(object): """Registers a plugin object""" self.plugin_objects.append(plugin_obj) + def register_template_path(self, path): + """Registers a template path""" + self.template_paths.add(path) + + def get_template_paths(self): + """Returns a tuple of registered template paths""" + return tuple(self.template_paths) + class MetaPluginClass(type): """Metaclass for PluginBase derivatives""" @@ -128,6 +139,27 @@ class Plugin(object): pass +def register_template_path(path): + """Registers a path for template loading + + If your plugin has templates, then you need to call this with + the absolute path of the root of templates directory. + + Example: + + >>> my_plugin_dir = os.path.dirname(__file__) + >>> template_dir = os.path.join(my_plugin_dir, 'templates') + >>> register_template_path(template_dir) + + .. Note:: + + You can only do this in `setup_plugins()`. Doing this after + that will have no effect on template loading. + + """ + PluginCache().register_template_path(path) + + def get_config(key): """Retrieves the configuration for a specified plugin by key -- 2.25.1