Mercurial > public > sg101
diff gpp/downloads/management/commands/dlcatreport.py @ 527:645025056dfa
Adding some management commands to the downloads application to help manage
the SG101 MP3 compilations. Added a command to generate a HTML report of all
the downloads in a given category. Added a command that generates a bash
script that wgets all the files in a category.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Wed, 21 Dec 2011 01:08:21 +0000 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/downloads/management/commands/dlcatreport.py Wed Dec 21 01:08:21 2011 +0000 @@ -0,0 +1,38 @@ +""" +dlcatreport - a management command to produce a HTML report of all the downloads +in a given category. + +""" +from django.core.management.base import LabelCommand, CommandError +from django.template.loader import render_to_string + +from downloads.models import Category, Download + + +class Command(LabelCommand): + help = "Produce on standard output a report of all downloads in a category." + args = "category-slug" + + def handle_label(self, slug, **options): + """ + Render a template using the downloads in a given category and send it to + stdout. + + """ + try: + category = Category.objects.get(slug=slug) + except Category.DoesNotExist: + raise CommandError("category slug '%s' does not exist" % slug) + + downloads = Download.public_objects.filter(category=category).order_by( + 'title').select_related() + + report = render_to_string('downloads/commands/category_report.html', { + 'category': category, + 'downloads': downloads, + }) + + # encode it ourselves since it can fail if you try to redirect output to + # a file and any of the content is not ASCII... + print report.encode('utf-8') +