Mercurial > public > sg101
comparison downloads/management/commands/dlcatreport.py @ 634:d6489e6a40f6
Add an encoding header to the downloads category report.
Added a --titles-only option to the downloads category report.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Fri, 21 Dec 2012 10:28:52 -0600 |
parents | ee87ea74d46b |
children | 161b56849114 |
comparison
equal
deleted
inserted
replaced
633:efac466a05d4 | 634:d6489e6a40f6 |
---|---|
1 """ | 1 """ |
2 dlcatreport - a management command to produce a HTML report of all the downloads | 2 dlcatreport - a management command to produce a HTML report of all the downloads |
3 in a given category. | 3 in a given category. |
4 | 4 |
5 """ | 5 """ |
6 from optparse import make_option | |
7 | |
6 from django.core.management.base import LabelCommand, CommandError | 8 from django.core.management.base import LabelCommand, CommandError |
7 from django.template.loader import render_to_string | 9 from django.template.loader import render_to_string |
8 | 10 |
9 from downloads.models import Category, Download | 11 from downloads.models import Category, Download |
10 | 12 |
11 | 13 |
14 def print_titles(dls): | |
15 """Print out the download titles""" | |
16 | |
17 for dl in dls: | |
18 print dl.title.encode('utf-8') | |
19 | |
20 | |
12 class Command(LabelCommand): | 21 class Command(LabelCommand): |
13 help = "Produce on standard output a report of all downloads in a category." | 22 help = "Produce on standard output a report of all downloads in a category." |
14 args = "category-slug" | 23 args = "category-slug" |
24 | |
25 option_list = LabelCommand.option_list + ( | |
26 make_option('--titles-only', | |
27 action='store_true', | |
28 default=False, | |
29 help='Output a text listing of titles only'), | |
30 ) | |
15 | 31 |
16 def handle_label(self, slug, **options): | 32 def handle_label(self, slug, **options): |
17 """ | 33 """ |
18 Render a template using the downloads in a given category and send it to | 34 Render a template using the downloads in a given category and send it to |
19 stdout. | 35 stdout. |
25 raise CommandError("category slug '%s' does not exist" % slug) | 41 raise CommandError("category slug '%s' does not exist" % slug) |
26 | 42 |
27 downloads = Download.public_objects.filter(category=category).order_by( | 43 downloads = Download.public_objects.filter(category=category).order_by( |
28 'title').select_related() | 44 'title').select_related() |
29 | 45 |
46 if options.get('titles_only'): | |
47 print_titles(downloads) | |
48 return | |
49 | |
30 report = render_to_string('downloads/commands/category_report.html', { | 50 report = render_to_string('downloads/commands/category_report.html', { |
31 'category': category, | 51 'category': category, |
32 'downloads': downloads, | 52 'downloads': downloads, |
33 }) | 53 }) |
34 | 54 |