comparison downloads/management/commands/dlcatreport.py @ 684:161b56849114

For Django 1.5: management commands should use self.stdout, etc. for output. Note that I did not update the legacy commands since it is likely I will never run or test these commands again.
author Brian Neal <bgneal@gmail.com>
date Fri, 23 Aug 2013 19:17:40 -0500
parents d6489e6a40f6
children b59c154d0163
comparison
equal deleted inserted replaced
683:c83d330cb65f 684:161b56849114
7 7
8 from django.core.management.base import LabelCommand, CommandError 8 from django.core.management.base import LabelCommand, CommandError
9 from django.template.loader import render_to_string 9 from django.template.loader import render_to_string
10 10
11 from downloads.models import Category, Download 11 from downloads.models import Category, Download
12
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 12
20 13
21 class Command(LabelCommand): 14 class Command(LabelCommand):
22 help = "Produce on standard output a report of all downloads in a category." 15 help = "Produce on standard output a report of all downloads in a category."
23 args = "category-slug" 16 args = "category-slug"
42 35
43 downloads = Download.public_objects.filter(category=category).order_by( 36 downloads = Download.public_objects.filter(category=category).order_by(
44 'title').select_related() 37 'title').select_related()
45 38
46 if options.get('titles_only'): 39 if options.get('titles_only'):
47 print_titles(downloads) 40 self.print_titles(downloads)
48 return 41 return
49 42
50 report = render_to_string('downloads/commands/category_report.html', { 43 report = render_to_string('downloads/commands/category_report.html', {
51 'category': category, 44 'category': category,
52 'downloads': downloads, 45 'downloads': downloads,
53 }) 46 })
54 47
55 # encode it ourselves since it can fail if you try to redirect output to 48 # encode it ourselves since it can fail if you try to redirect output to
56 # a file and any of the content is not ASCII... 49 # a file and any of the content is not ASCII...
57 print report.encode('utf-8') 50 self.stdout.write(report.encode('utf-8'))
58 51
52 def print_titles(self, dls):
53 """Print out the download titles"""
54
55 for dl in dls:
56 self.stdout.write(dl.title.encode('utf-8'))
57
58