Code

Ticket #7254: dumpdata.py.diff

File dumpdata.py.diff, 3.5 KB (added by Carl Karsten <carl@…>, 6 years ago)
Line 
1Index: django/core/management/commands/dumpdata.py
2===================================================================
3--- django/core/management/commands/dumpdata.py (revision 7534)
4+++ django/core/management/commands/dumpdata.py (working copy)
5@@ -2,13 +2,19 @@
6 from django.core import serializers
7 
8 from optparse import make_option
9+from sys import stderr
10 
11 class Command(BaseCommand):
12     option_list = BaseCommand.option_list + (
13-        make_option('--format', default='json', dest='format',
14+        make_option('--format', dest='format', default='json',
15             help='Specifies the output serialization format for fixtures.'),
16-        make_option('--indent', default=None, dest='indent', type='int',
17+        make_option('--indent', dest='indent', type='int', default=None,
18             help='Specifies the indent level to use when pretty-printing output'),
19+        make_option('-e', '--exclude', dest='exclude',action='append', default=None,
20+            help='App to exclude (use multiple --exclude to exclude multiple apps).'),
21+        make_option('-v', '--verbosity', action='store', dest='verbosity', default='0',
22+            type='choice', choices=['0', '1', '2'],
23+            help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'),
24     )
25     help = 'Output the contents of the database as a fixture of the given format.'
26     args = '[appname ...]'
27@@ -16,15 +22,34 @@
28     def handle(self, *app_labels, **options):
29         from django.db.models import get_app, get_apps, get_models
30 
31-        format = options.get('format', 'json')
32-        indent = options.get('indent', None)
33-        show_traceback = options.get('traceback', False)
34+        format = options.get('format')
35+        indent = options.get('indent')
36+        exclude = options.get('exclude')
37+        verbosity = int(options.get('verbosity'))
38+        show_traceback = options.get('traceback')
39 
40+        if verbosity >= 2:
41+            print >>stderr, \
42+                "format %s\nindent %s\nexclude %s\nshow_traceback %s\n" % \
43+                (format, indent, exclude, show_traceback)
44+            print >>stderr, "apps %s" % (app_labels)
45+
46         if len(app_labels) == 0:
47             app_list = get_apps()
48         else:
49             app_list = [get_app(app_label) for app_label in app_labels]
50 
51+        if exclude:
52+            excl_apps = [get_app(app_label) for app_label in exclude]
53+            for excl_app in excl_apps:
54+                app_list.remove(excl_app)
55+                if verbosity >= 2:
56+                    print >>stderr, "excluded: %s" % excl_app.__name__
57+       
58+        if verbosity >= 2:
59+            for app in app_list:
60+                print >>stderr, "included: %s" % app.__name__
61+           
62         # Check that the serialization format exists; this is a shortcut to
63         # avoid collating all the objects and _then_ failing.
64         if format not in serializers.get_public_serializer_formats():
65@@ -37,7 +62,13 @@
66 
67         objects = []
68         for app in app_list:
69+            if verbosity >= 1:
70+                print >>stderr, "dumping %s" % (app.__name__)
71             for model in get_models(app):
72+                if verbosity >= 1:
73+                    # model dosn't have a good __str,
74+                    # so for now "<class... model_name>" is better than nothing.
75+                    print >>stderr, "dumping %s,%s" % (app.__name__, model)
76                 objects.extend(model._default_manager.all())
77         try:
78             return serializers.serialize(format, objects, indent=indent)