# HG changeset patch # User Brian Neal # Date 1364062688 18000 # Node ID 73eb098761a1b41c60f26af012b8d9104379f008 # Parent 99f7917702ca53f8708f96d79b85394ad672811e Implement mgmt command to export forum topic. BB issue #36. diff -r 99f7917702ca -r 73eb098761a1 forums/management/commands/topic_export.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/forums/management/commands/topic_export.py Sat Mar 23 13:18:08 2013 -0500 @@ -0,0 +1,67 @@ +""" +topic_export.py + +A management command to export a forum topic by rendering it through a given +template. + +""" +from optparse import make_option +import re + +from django.core.management.base import LabelCommand, CommandError +from django.template.loader import render_to_string, TemplateDoesNotExist + +from forums.models import Topic + + +SRC_RE = re.compile(r'src="/media/') +SRC_REPL = 'src="http://surfguitar101.com/media/' + + +class Command(LabelCommand): + help = "Exports a forum topic thread by rendering it through a given template" + option_list = LabelCommand.option_list + ( + make_option('-t', '--template', + default='forums/topic_export.html', + help='template to render'), + make_option('-o', '--output', + default=None, + help='output filename [default: stdout]'), + ) + + def handle_label(self, tid, **opts): + """Fetch the topic and related posts. Render through a template. + Optionally write content to an output file. + + """ + try: + tid = int(tid) + except ValueError: + raise CommandError('topic ID must be an integer') + + template_name = opts['template'] + output_filename = opts['output'] + + try: + topic = Topic.objects.get(pk=tid) + except Topic.DoesNotExist: + raise CommandError('topic ID does not exist') + + posts = topic.posts.select_related(depth=1) + + try: + content = render_to_string(template_name, {'topic': topic, 'posts': + posts}) + except TemplateDoesNotExist: + raise CommandError('template does not exist') + + # fix up smiley images + content = SRC_RE.sub(SRC_REPL, content) + + content = content.encode('utf-8') + + if output_filename: + with open(output_filename, 'w') as fp: + fp.write(content) + else: + self.stdout.write(content) diff -r 99f7917702ca -r 73eb098761a1 sg101/templates/forums/topic_export.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sg101/templates/forums/topic_export.html Sat Mar 23 13:18:08 2013 -0500 @@ -0,0 +1,16 @@ + + + + + {{ topic.name }} - SG101 topic #{{ topic.id }} + + +

{{ topic.name }}

+ {% for post in posts %} +
+

{{ post.user.username }}:

+
{{ post.html|safe }}
+
+ {% endfor %} + +