diff 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
line wrap: on
line diff
--- a/downloads/management/commands/dlcatreport.py	Mon Dec 27 16:58:02 2021 -0600
+++ b/downloads/management/commands/dlcatreport.py	Mon Dec 27 17:30:26 2021 -0600
@@ -5,6 +5,7 @@
 """
 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
 
@@ -20,6 +21,10 @@
                 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):
@@ -40,6 +45,10 @@
             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,
@@ -53,4 +62,9 @@
         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)
 
+