bgneal@584: """make_flatpage.py bgneal@584: bgneal@584: A management command to update flatpage objects from restructured text files. bgneal@584: bgneal@584: """ bgneal@584: import os.path bgneal@584: bgneal@584: import docutils.core bgneal@584: from django.core.management.base import LabelCommand, CommandError bgneal@584: from django.conf import settings bgneal@584: from django.template.loader import render_to_string bgneal@584: from django.template import TemplateDoesNotExist bgneal@584: from django.contrib.flatpages.models import FlatPage bgneal@584: bgneal@584: bgneal@584: class Command(LabelCommand): bgneal@584: help = "Update flatpage objects from restructured text files" bgneal@584: args = " ... | all" bgneal@584: bgneal@584: def handle_label(self, filename, **kwargs): bgneal@584: """Process input file(s)""" bgneal@584: bgneal@584: if not hasattr(settings, 'GPP_FLATPAGES'): bgneal@584: raise CommandError("Please add a GPP_FLATPAGES setting") bgneal@584: if not hasattr(settings, 'PROJECT_PATH'): bgneal@584: raise CommandError("Please add a PROJECT_PATH setting") bgneal@584: bgneal@584: self.config = settings.GPP_FLATPAGES bgneal@584: self.src_dir = os.path.join(settings.PROJECT_PATH, 'flatpages') bgneal@584: bgneal@584: if filename == 'all': bgneal@584: files = self.config.keys() bgneal@584: else: bgneal@584: files = [filename] bgneal@584: bgneal@584: for f in files: bgneal@584: self.process_flatpage(f) bgneal@584: bgneal@584: bgneal@584: def process_flatpage(self, filename): bgneal@584: """Processes one flatpage""" bgneal@584: bgneal@584: if filename not in self.config: bgneal@584: raise CommandError("No entry in GPP_FLATPAGES for %s" % filename) bgneal@584: url, template_name = self.config[filename] bgneal@584: bgneal@584: # retrieve source text bgneal@584: src_path = os.path.join(self.src_dir, filename) bgneal@584: with open(src_path, 'r') as f: bgneal@584: src_text = f.read() bgneal@584: bgneal@584: # transform text bgneal@584: content = self.transform_input(src_text) bgneal@584: bgneal@584: # render through a template if requested bgneal@584: if template_name is not None: bgneal@584: try: bgneal@584: content = render_to_string(template_name, dict(content=content)) bgneal@584: except TemplateDoesNotExist: bgneal@584: raise CommandError("template not found: %s" % template_name) bgneal@584: bgneal@584: # update the flatpage object bgneal@584: try: bgneal@584: obj = FlatPage.objects.get(url=url) bgneal@584: except FlatPage.DoesNotExist: bgneal@584: raise CommandError("flatpage not found: %s" % url) bgneal@584: bgneal@584: obj.content = content; bgneal@584: obj.save() bgneal@584: bgneal@584: self.stdout.write("%s -> %s\n" % (filename, url)) bgneal@584: bgneal@584: def transform_input(self, src_text): bgneal@584: """Transforms input restructured text to HTML""" bgneal@584: bgneal@584: return docutils.core.publish_parts(src_text, writer_name='html', bgneal@584: settings_overrides={ bgneal@584: 'doctitle_xform': False, bgneal@584: 'initial_header_level': 3, bgneal@584: })['html_body']