bgneal@527: """ bgneal@527: dlcatreport - a management command to produce a HTML report of all the downloads bgneal@527: in a given category. bgneal@527: bgneal@527: """ bgneal@634: from optparse import make_option bgneal@634: bgneal@527: from django.core.management.base import LabelCommand, CommandError bgneal@527: from django.template.loader import render_to_string bgneal@527: bgneal@527: from downloads.models import Category, Download bgneal@527: bgneal@527: bgneal@634: def print_titles(dls): bgneal@634: """Print out the download titles""" bgneal@634: bgneal@634: for dl in dls: bgneal@634: print dl.title.encode('utf-8') bgneal@634: bgneal@634: bgneal@527: class Command(LabelCommand): bgneal@527: help = "Produce on standard output a report of all downloads in a category." bgneal@527: args = "category-slug" bgneal@527: bgneal@634: option_list = LabelCommand.option_list + ( bgneal@634: make_option('--titles-only', bgneal@634: action='store_true', bgneal@634: default=False, bgneal@634: help='Output a text listing of titles only'), bgneal@634: ) bgneal@634: bgneal@527: def handle_label(self, slug, **options): bgneal@527: """ bgneal@527: Render a template using the downloads in a given category and send it to bgneal@527: stdout. bgneal@527: bgneal@527: """ bgneal@527: try: bgneal@527: category = Category.objects.get(slug=slug) bgneal@527: except Category.DoesNotExist: bgneal@527: raise CommandError("category slug '%s' does not exist" % slug) bgneal@527: bgneal@527: downloads = Download.public_objects.filter(category=category).order_by( bgneal@527: 'title').select_related() bgneal@527: bgneal@634: if options.get('titles_only'): bgneal@634: print_titles(downloads) bgneal@634: return bgneal@634: bgneal@527: report = render_to_string('downloads/commands/category_report.html', { bgneal@527: 'category': category, bgneal@527: 'downloads': downloads, bgneal@527: }) bgneal@527: bgneal@527: # encode it ourselves since it can fail if you try to redirect output to bgneal@527: # a file and any of the content is not ASCII... bgneal@527: print report.encode('utf-8') bgneal@527: