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@1188: from django.core import serializers 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@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@1188: make_option('--json', bgneal@1188: action='store_true', bgneal@1188: default=False, bgneal@1188: help='Output downloads in JSON format'), 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@684: self.print_titles(downloads) bgneal@634: return bgneal@634: bgneal@1188: if options.get('json'): bgneal@1188: self.print_json(downloads) bgneal@1188: return bgneal@1188: bgneal@527: report = render_to_string('downloads/commands/category_report.html', { bgneal@527: 'category': category, bgneal@527: 'downloads': downloads, bgneal@527: }) bgneal@527: bgneal@874: self.stdout.write(report) bgneal@527: bgneal@684: def print_titles(self, dls): bgneal@684: """Print out the download titles""" bgneal@684: bgneal@684: for dl in dls: bgneal@874: self.stdout.write(dl.title) bgneal@684: bgneal@1188: def print_json(self, dls): bgneal@1188: """Output downloads in JSON format""" bgneal@1188: data = serializers.serialize('json', dls) bgneal@1188: self.stdout.write(data) bgneal@684: bgneal@1188: