annotate core/management/commands/make_flatpage.py @ 584:ef4ef8c8ec8d

For bitbucket issue #8, insert .rst files into flatpages.
author Brian Neal <bgneal@gmail.com>
date Thu, 10 May 2012 20:03:26 -0500
parents
children 7ed2101df789
rev   line source
bgneal@584 1 """make_flatpage.py
bgneal@584 2
bgneal@584 3 A management command to update flatpage objects from restructured text files.
bgneal@584 4
bgneal@584 5 """
bgneal@584 6 import os.path
bgneal@584 7
bgneal@584 8 import docutils.core
bgneal@584 9 from django.core.management.base import LabelCommand, CommandError
bgneal@584 10 from django.conf import settings
bgneal@584 11 from django.template.loader import render_to_string
bgneal@584 12 from django.template import TemplateDoesNotExist
bgneal@584 13 from django.contrib.flatpages.models import FlatPage
bgneal@584 14
bgneal@584 15
bgneal@584 16 class Command(LabelCommand):
bgneal@584 17 help = "Update flatpage objects from restructured text files"
bgneal@584 18 args = "<inputfile1> <inputfile2> ... | all"
bgneal@584 19
bgneal@584 20 def handle_label(self, filename, **kwargs):
bgneal@584 21 """Process input file(s)"""
bgneal@584 22
bgneal@584 23 if not hasattr(settings, 'GPP_FLATPAGES'):
bgneal@584 24 raise CommandError("Please add a GPP_FLATPAGES setting")
bgneal@584 25 if not hasattr(settings, 'PROJECT_PATH'):
bgneal@584 26 raise CommandError("Please add a PROJECT_PATH setting")
bgneal@584 27
bgneal@584 28 self.config = settings.GPP_FLATPAGES
bgneal@584 29 self.src_dir = os.path.join(settings.PROJECT_PATH, 'flatpages')
bgneal@584 30
bgneal@584 31 if filename == 'all':
bgneal@584 32 files = self.config.keys()
bgneal@584 33 else:
bgneal@584 34 files = [filename]
bgneal@584 35
bgneal@584 36 for f in files:
bgneal@584 37 self.process_flatpage(f)
bgneal@584 38
bgneal@584 39
bgneal@584 40 def process_flatpage(self, filename):
bgneal@584 41 """Processes one flatpage"""
bgneal@584 42
bgneal@584 43 if filename not in self.config:
bgneal@584 44 raise CommandError("No entry in GPP_FLATPAGES for %s" % filename)
bgneal@584 45 url, template_name = self.config[filename]
bgneal@584 46
bgneal@584 47 # retrieve source text
bgneal@584 48 src_path = os.path.join(self.src_dir, filename)
bgneal@584 49 with open(src_path, 'r') as f:
bgneal@584 50 src_text = f.read()
bgneal@584 51
bgneal@584 52 # transform text
bgneal@584 53 content = self.transform_input(src_text)
bgneal@584 54
bgneal@584 55 # render through a template if requested
bgneal@584 56 if template_name is not None:
bgneal@584 57 try:
bgneal@584 58 content = render_to_string(template_name, dict(content=content))
bgneal@584 59 except TemplateDoesNotExist:
bgneal@584 60 raise CommandError("template not found: %s" % template_name)
bgneal@584 61
bgneal@584 62 # update the flatpage object
bgneal@584 63 try:
bgneal@584 64 obj = FlatPage.objects.get(url=url)
bgneal@584 65 except FlatPage.DoesNotExist:
bgneal@584 66 raise CommandError("flatpage not found: %s" % url)
bgneal@584 67
bgneal@584 68 obj.content = content;
bgneal@584 69 obj.save()
bgneal@584 70
bgneal@584 71 self.stdout.write("%s -> %s\n" % (filename, url))
bgneal@584 72
bgneal@584 73 def transform_input(self, src_text):
bgneal@584 74 """Transforms input restructured text to HTML"""
bgneal@584 75
bgneal@584 76 return docutils.core.publish_parts(src_text, writer_name='html',
bgneal@584 77 settings_overrides={
bgneal@584 78 'doctitle_xform': False,
bgneal@584 79 'initial_header_level': 3,
bgneal@584 80 })['html_body']