From: Will Kahn-Greene Date: Thu, 12 Jul 2012 23:18:15 +0000 (-0400) Subject: Overhaul flatpages X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=54b42ee5645ff40aa572f89e60184bf540b50041;p=mediagoblin.git Overhaul flatpages * move contents of main.py to __init__.py * update documentation in README * change the key/value configuration specification * added a recipe for passing values from the url to the template * removed some unused code --- diff --git a/mediagoblin/plugins/flatpagesfile/README.rst b/mediagoblin/plugins/flatpagesfile/README.rst index b31f6017..525fb9d9 100644 --- a/mediagoblin/plugins/flatpagesfile/README.rst +++ b/mediagoblin/plugins/flatpagesfile/README.rst @@ -44,27 +44,24 @@ First, let's talk about the route. A route is a key/value in your configuration file. -The key starts with ``path`` and then has a number after that. For -example: ``path1``, ``path2``, ... ``path15``, ... +The key for the route is the route name You can use this with `url()` +in templates to have MediaGoblin automatically build the urls for +you. It's very handy. -The value has three parts separated by commas: +It should be "unique" and it should be alphanumeric characters and +hyphens. I wouldn't put spaces in there. -1. **route name**: You can use this with `url()` in templates to have - MediaGoblin automatically build the urls for you. It's very handy. +Examples: ``flatpages-about``, ``about-view``, ``contact-view``, ... - It should be "unique" and it should be alphanumeric characters and - hyphens. I wouldn't put spaces in there. +The value has two parts separated by commas: - Examples: ``flatpages-about``, ``about-view``, ``contact-view``, ... - -2. **route path**: This is the url that this route matches. +1. **route path**: This is the url that this route matches. Examples: ``/about``, ``/contact``, ``/pages/about``, ... - Technically, this is a regular expression and you can do anything - with this that you can do with the routepath parameter of - `routes.Route`. For more details, see `the routes documentation - `_. + You can do anything with this that you can do with the routepath + parameter of `routes.Route`. For more details, see `the routes + documentation `_. Example: ``/siteadmin/{adminname:\w+}`` @@ -75,7 +72,7 @@ The value has three parts separated by commas: For example: ``'/siteadmin/{adminname:\w+}'`` -3. **template**: The template to use for this url. The template is in +2. **template**: The template to use for this url. The template is in the flatpagesfile template directory, so you just need to specify the file name. @@ -89,8 +86,14 @@ Here's an example configuration that adds two flat pages: one for an "About this site" page and one for a "Terms of service" page:: [[mediagoblin.plugins.flatpagesfile]] - page1 = about-view, '/about', about.html - page2 = terms-view, '/terms', terms.html + about-view = '/about', about.html + terms-view = '/terms', terms.html + + +.. Note:: + + The order in which you define the routes in the config file is the + order in which they're checked for incoming requests. Templates @@ -130,3 +133,29 @@ template:: another, take advantage of Jinja2 template extending and create a base template that the others extend. + +Recipes +======= + +Url variables +------------- + +You can handle urls like ``/about/{name}`` and access the name that's +passed in in the template. + +Sample route:: + + about-page = '/about/{name}', about.html + +Sample template:: + + {% extends "flatpagesfile/base.html" %} + {% block mediagoblin_content %} + +

About page for {{ request.matchdict['name'] }}

+ + {% endblock %} + +See the `the routes documentation +`_ for syntax details for +the route. Values will end up in the ``request.matchdict`` dict. diff --git a/mediagoblin/plugins/flatpagesfile/__init__.py b/mediagoblin/plugins/flatpagesfile/__init__.py index 69c40a77..9ed26102 100644 --- a/mediagoblin/plugins/flatpagesfile/__init__.py +++ b/mediagoblin/plugins/flatpagesfile/__init__.py @@ -15,6 +15,65 @@ # 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 +import logging +import os + +import jinja2 +from routes.route import Route + +from mediagoblin.tools import pluginapi +from mediagoblin.tools.response import render_to_response + + +PLUGIN_DIR = os.path.dirname(__file__) + + +_log = logging.getLogger(__name__) + + +@jinja2.contextfunction +def print_context(c): + s = [] + for key, val in c.items(): + s.append('%s: %s' % (key, repr(val))) + return '\n'.join(s) + + +def flatpage_handler_builder(template): + """Flatpage view generator + + Given a template, generates the controller function for handling that + route. + + """ + def _flatpage_handler_builder(request): + return render_to_response( + request, 'flatpagesfile/%s' % template, + {'request': request}) + return _flatpage_handler_builder + + +class FlatpagesFilePlugin(pluginapi.Plugin): + """ + This is the flatpages plugin class. See the README for how to use + flatpages. + """ + def setup_plugin(self): + self.config = pluginapi.get_config('mediagoblin.plugins.flatpagesfile') + + _log.info('Setting up flatpagesfile....') + + # Register the template path. + pluginapi.register_template_path(os.path.join(PLUGIN_DIR, 'templates')) + + pages = self.config.items() + + routes = [] + for name, (url, template) in pages: + name = 'flatpagesfile.%s' % name.strip() + controller = flatpage_handler_builder(template) + routes.append( + Route(name, url, controller=controller)) + + pluginapi.register_routes(routes) + _log.info('Done setting up flatpagesfile!') diff --git a/mediagoblin/plugins/flatpagesfile/main.py b/mediagoblin/plugins/flatpagesfile/main.py deleted file mode 100644 index b0f5ea42..00000000 --- a/mediagoblin/plugins/flatpagesfile/main.py +++ /dev/null @@ -1,73 +0,0 @@ -# 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 routes.route import Route - -from mediagoblin.tools import pluginapi -from mediagoblin.tools.response import render_to_response - - -PLUGIN_DIR = os.path.dirname(__file__) - - -_log = logging.getLogger(__name__) - - -def flatpage_handler(template): - """Flatpage view generator - - Given a template, generates the controller function for handling that - route. - - """ - def _flatpage_handler(request, *args, **kwargs): - return render_to_response( - request, 'flatpagesfile/%s' % template, - {'args': args, 'kwargs': kwargs}) - return _flatpage_handler - - -class FlatpagesFilePlugin(pluginapi.Plugin): - """ - This is the flatpages plugin class. See the README for how to use - flatpages. - """ - def setup_plugin(self): - self.config = pluginapi.get_config('mediagoblin.plugins.flatpagesfile') - - _log.info('Setting up flatpagesfile....') - - # Register the template path. - pluginapi.register_template_path(os.path.join(PLUGIN_DIR, 'templates')) - - # Set up and register routes. - pages = [(int(key.replace('page', '')), val) - for key, val in self.config.items() - if key.startswith('page')] - - pages = [mapping for index, mapping in sorted(pages)] - routes = [] - for name, url, template in pages: - name = 'flatpagesfile.%s' % name.strip() - controller = flatpage_handler(template) - routes.append( - Route(name, url, controller=controller)) - - pluginapi.register_routes(routes) - _log.info('Done setting up flatpagesfile!')