Mercurial > public > sg101
comparison downloads/management/commands/dlcatreport.py @ 581:ee87ea74d46b
For Django 1.4, rearranged project structure for new manage.py.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 05 May 2012 17:10:48 -0500 |
parents | gpp/downloads/management/commands/dlcatreport.py@645025056dfa |
children | d6489e6a40f6 |
comparison
equal
deleted
inserted
replaced
580:c525f3e0b5d0 | 581:ee87ea74d46b |
---|---|
1 """ | |
2 dlcatreport - a management command to produce a HTML report of all the downloads | |
3 in a given category. | |
4 | |
5 """ | |
6 from django.core.management.base import LabelCommand, CommandError | |
7 from django.template.loader import render_to_string | |
8 | |
9 from downloads.models import Category, Download | |
10 | |
11 | |
12 class Command(LabelCommand): | |
13 help = "Produce on standard output a report of all downloads in a category." | |
14 args = "category-slug" | |
15 | |
16 def handle_label(self, slug, **options): | |
17 """ | |
18 Render a template using the downloads in a given category and send it to | |
19 stdout. | |
20 | |
21 """ | |
22 try: | |
23 category = Category.objects.get(slug=slug) | |
24 except Category.DoesNotExist: | |
25 raise CommandError("category slug '%s' does not exist" % slug) | |
26 | |
27 downloads = Download.public_objects.filter(category=category).order_by( | |
28 'title').select_related() | |
29 | |
30 report = render_to_string('downloads/commands/category_report.html', { | |
31 'category': category, | |
32 'downloads': downloads, | |
33 }) | |
34 | |
35 # encode it ourselves since it can fail if you try to redirect output to | |
36 # a file and any of the content is not ASCII... | |
37 print report.encode('utf-8') | |
38 |