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