comparison 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
comparison
equal deleted inserted replaced
526:dd97341788fa 527:645025056dfa
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