Mercurial > public > sg101
annotate 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 |
rev | line source |
---|---|
bgneal@527 | 1 """ |
bgneal@527 | 2 dlcatreport - a management command to produce a HTML report of all the downloads |
bgneal@527 | 3 in a given category. |
bgneal@527 | 4 |
bgneal@527 | 5 """ |
bgneal@527 | 6 from django.core.management.base import LabelCommand, CommandError |
bgneal@527 | 7 from django.template.loader import render_to_string |
bgneal@527 | 8 |
bgneal@527 | 9 from downloads.models import Category, Download |
bgneal@527 | 10 |
bgneal@527 | 11 |
bgneal@527 | 12 class Command(LabelCommand): |
bgneal@527 | 13 help = "Produce on standard output a report of all downloads in a category." |
bgneal@527 | 14 args = "category-slug" |
bgneal@527 | 15 |
bgneal@527 | 16 def handle_label(self, slug, **options): |
bgneal@527 | 17 """ |
bgneal@527 | 18 Render a template using the downloads in a given category and send it to |
bgneal@527 | 19 stdout. |
bgneal@527 | 20 |
bgneal@527 | 21 """ |
bgneal@527 | 22 try: |
bgneal@527 | 23 category = Category.objects.get(slug=slug) |
bgneal@527 | 24 except Category.DoesNotExist: |
bgneal@527 | 25 raise CommandError("category slug '%s' does not exist" % slug) |
bgneal@527 | 26 |
bgneal@527 | 27 downloads = Download.public_objects.filter(category=category).order_by( |
bgneal@527 | 28 'title').select_related() |
bgneal@527 | 29 |
bgneal@527 | 30 report = render_to_string('downloads/commands/category_report.html', { |
bgneal@527 | 31 'category': category, |
bgneal@527 | 32 'downloads': downloads, |
bgneal@527 | 33 }) |
bgneal@527 | 34 |
bgneal@527 | 35 # encode it ourselves since it can fail if you try to redirect output to |
bgneal@527 | 36 # a file and any of the content is not ASCII... |
bgneal@527 | 37 print report.encode('utf-8') |
bgneal@527 | 38 |