Mercurial > public > sg101
comparison downloads/management/commands/dlcatreport.py @ 1188:cb712b2c34af
Add JSON option to dlcatreport management command.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Mon, 27 Dec 2021 17:30:26 -0600 |
parents | b59c154d0163 |
children |
comparison
equal
deleted
inserted
replaced
1187:a38ecbffbd59 | 1188:cb712b2c34af |
---|---|
3 in a given category. | 3 in a given category. |
4 | 4 |
5 """ | 5 """ |
6 from optparse import make_option | 6 from optparse import make_option |
7 | 7 |
8 from django.core import serializers | |
8 from django.core.management.base import LabelCommand, CommandError | 9 from django.core.management.base import LabelCommand, CommandError |
9 from django.template.loader import render_to_string | 10 from django.template.loader import render_to_string |
10 | 11 |
11 from downloads.models import Category, Download | 12 from downloads.models import Category, Download |
12 | 13 |
18 option_list = LabelCommand.option_list + ( | 19 option_list = LabelCommand.option_list + ( |
19 make_option('--titles-only', | 20 make_option('--titles-only', |
20 action='store_true', | 21 action='store_true', |
21 default=False, | 22 default=False, |
22 help='Output a text listing of titles only'), | 23 help='Output a text listing of titles only'), |
24 make_option('--json', | |
25 action='store_true', | |
26 default=False, | |
27 help='Output downloads in JSON format'), | |
23 ) | 28 ) |
24 | 29 |
25 def handle_label(self, slug, **options): | 30 def handle_label(self, slug, **options): |
26 """ | 31 """ |
27 Render a template using the downloads in a given category and send it to | 32 Render a template using the downloads in a given category and send it to |
38 | 43 |
39 if options.get('titles_only'): | 44 if options.get('titles_only'): |
40 self.print_titles(downloads) | 45 self.print_titles(downloads) |
41 return | 46 return |
42 | 47 |
48 if options.get('json'): | |
49 self.print_json(downloads) | |
50 return | |
51 | |
43 report = render_to_string('downloads/commands/category_report.html', { | 52 report = render_to_string('downloads/commands/category_report.html', { |
44 'category': category, | 53 'category': category, |
45 'downloads': downloads, | 54 'downloads': downloads, |
46 }) | 55 }) |
47 | 56 |
51 """Print out the download titles""" | 60 """Print out the download titles""" |
52 | 61 |
53 for dl in dls: | 62 for dl in dls: |
54 self.stdout.write(dl.title) | 63 self.stdout.write(dl.title) |
55 | 64 |
65 def print_json(self, dls): | |
66 """Output downloads in JSON format""" | |
67 data = serializers.serialize('json', dls) | |
68 self.stdout.write(data) | |
56 | 69 |
70 |