view downloads/management/commands/dlcatreport.py @ 1200:b9514abc2a67

Initial commit of ssg101.
author Brian Neal <bgneal@gmail.com>
date Sat, 24 Jun 2023 16:06:51 -0500
parents cb712b2c34af
children
line wrap: on
line source
"""
dlcatreport - a management command to produce a HTML report of all the downloads
in a given category.

"""
from optparse import make_option

from django.core import serializers
from django.core.management.base import LabelCommand, CommandError
from django.template.loader import render_to_string

from downloads.models import Category, Download


class Command(LabelCommand):
    help = "Produce on standard output a report of all downloads in a category."
    args = "category-slug"

    option_list = LabelCommand.option_list + (
            make_option('--titles-only',
                action='store_true',
                default=False,
                help='Output a text listing of titles only'),
            make_option('--json',
                action='store_true',
                default=False,
                help='Output downloads in JSON format'),
            )

    def handle_label(self, slug, **options):
        """
        Render a template using the downloads in a given category and send it to
        stdout.

        """
        try:
            category = Category.objects.get(slug=slug)
        except Category.DoesNotExist:
            raise CommandError("category slug '%s' does not exist" % slug)

        downloads = Download.public_objects.filter(category=category).order_by(
                'title').select_related()

        if options.get('titles_only'):
            self.print_titles(downloads)
            return

        if options.get('json'):
            self.print_json(downloads)
            return

        report = render_to_string('downloads/commands/category_report.html', {
            'category': category,
            'downloads': downloads,
            })

        self.stdout.write(report)

    def print_titles(self, dls):
        """Print out the download titles"""

        for dl in dls:
            self.stdout.write(dl.title)

    def print_json(self, dls):
        """Output downloads in JSON format"""
        data = serializers.serialize('json', dls)
        self.stdout.write(data)