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