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.
This commit is contained in:
Will Kahn-Greene 2012-06-10 11:50:14 -04:00
parent 62157a898f
commit 8545dd50f0
7 changed files with 186 additions and 19 deletions

View File

@ -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()

View File

@ -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 = []
path_list = []
# user template overrides
if user_template_path:
loader_choices.append(jinja2.FileSystemLoader(user_template_path))
# 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'):
loader_choices.append(
jinja2.FileSystemLoader(
current_theme['templates_dir']))
# 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 the main mediagoblin templates
loader_choices.append(
jinja2.PackageLoader('mediagoblin', 'templates'))
# 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))
return jinja2.ChoiceLoader(loader_choices)
else:
return jinja2.PackageLoader('mediagoblin', 'templates')
# Add core templates last.
path_list.append(jinja2.PackageLoader('mediagoblin', 'templates'))
return jinja2.ChoiceLoader(path_list)
def get_staticdirector(app_config):

View File

@ -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.

View File

@ -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

View File

@ -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'))

View File

@ -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" %}

View File

@ -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