Mercurial > public > sg101
changeset 585:3a1a8ed94e7f
Merge.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Thu, 10 May 2012 20:04:36 -0500 |
parents | ef4ef8c8ec8d (diff) 9dbd9a2a76d6 (current diff) |
children | a7ef954298ea |
files | |
diffstat | 2 files changed, 109 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/management/commands/make_flatpage.py Thu May 10 20:04:36 2012 -0500 @@ -0,0 +1,80 @@ +"""make_flatpage.py + +A management command to update flatpage objects from restructured text files. + +""" +import os.path + +import docutils.core +from django.core.management.base import LabelCommand, CommandError +from django.conf import settings +from django.template.loader import render_to_string +from django.template import TemplateDoesNotExist +from django.contrib.flatpages.models import FlatPage + + +class Command(LabelCommand): + help = "Update flatpage objects from restructured text files" + args = "<inputfile1> <inputfile2> ... | all" + + def handle_label(self, filename, **kwargs): + """Process input file(s)""" + + if not hasattr(settings, 'GPP_FLATPAGES'): + raise CommandError("Please add a GPP_FLATPAGES setting") + if not hasattr(settings, 'PROJECT_PATH'): + raise CommandError("Please add a PROJECT_PATH setting") + + self.config = settings.GPP_FLATPAGES + self.src_dir = os.path.join(settings.PROJECT_PATH, 'flatpages') + + if filename == 'all': + files = self.config.keys() + else: + files = [filename] + + for f in files: + self.process_flatpage(f) + + + def process_flatpage(self, filename): + """Processes one flatpage""" + + if filename not in self.config: + raise CommandError("No entry in GPP_FLATPAGES for %s" % filename) + url, template_name = self.config[filename] + + # retrieve source text + src_path = os.path.join(self.src_dir, filename) + with open(src_path, 'r') as f: + src_text = f.read() + + # transform text + content = self.transform_input(src_text) + + # render through a template if requested + if template_name is not None: + try: + content = render_to_string(template_name, dict(content=content)) + except TemplateDoesNotExist: + raise CommandError("template not found: %s" % template_name) + + # update the flatpage object + try: + obj = FlatPage.objects.get(url=url) + except FlatPage.DoesNotExist: + raise CommandError("flatpage not found: %s" % url) + + obj.content = content; + obj.save() + + self.stdout.write("%s -> %s\n" % (filename, url)) + + def transform_input(self, src_text): + """Transforms input restructured text to HTML""" + + return docutils.core.publish_parts(src_text, writer_name='html', + settings_overrides={ + 'doctitle_xform': False, + 'initial_header_level': 3, + })['html_body']
--- a/sg101/settings/base.py Sun May 06 11:03:38 2012 -0500 +++ b/sg101/settings/base.py Thu May 10 20:04:36 2012 -0500 @@ -297,6 +297,35 @@ ' SurfGuitar101.com!') ####################################################################### +# Homegrown flatpage system configuration +####################################################################### +# +# GPP_FLATPAGES is a dictionary where each key is a filename that is +# used as input to generate HTML that is inserted into an existing +# django.contrib.flatpages model. The value part of each entry is a +# 2-tuple with the following members, in order: +# +# * 'url' - The url of the flatpage model to update. This object must +# already exist. +# +# * 'template' - The template to be used to generate the flatpage; +# The processed text will be passed to the template in the context +# variable {{ content }}. If template is None, no template will +# be used; the generated text will be inserted directly into the +# flatpage object. +# +# The custom management command make_flatpage is used to run the +# flatpage generation process. E.g. +# +# $ python manage.py make_flatpage about.rst privacy.rst +# or +# $ python manage.py make_flatpage all +# +GPP_FLATPAGES = { + #'about.rst': ('/flatpage_test/', None), +} + +####################################################################### # URL's of 3rd party Javascript and CSS files. # These dictionaries are used by core/templatetags/script_tags, and # should also be used by developers when creating form media classes.