aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--mediagoblin/app.py5
-rw-r--r--mediagoblin/init/__init__.py45
-rw-r--r--mediagoblin/plugins/flatpagesfile/README50
-rw-r--r--mediagoblin/plugins/flatpagesfile/__init__.py20
-rw-r--r--mediagoblin/plugins/flatpagesfile/main.py41
-rw-r--r--mediagoblin/plugins/flatpagesfile/templates/flatpagesfile/base.html18
-rw-r--r--mediagoblin/tools/pluginapi.py34
7 files changed, 190 insertions, 23 deletions
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 %}
+ <h1>About this site</h1>
+ <p>
+ This site is a MediaGoblin instance set up to host media for
+ me, my family and my friends.
+ </p>
+ {% 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 <http://www.gnu.org/licenses/>.
+
+
+# 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 <http://www.gnu.org/licenses/>.
+
+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 <http://www.gnu.org/licenses/>.
+-#}
+{% 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