|
0
|
1 |
from django.core.exceptions import ImproperlyConfigured |
|
|
2 |
from django.core.management.base import BaseCommand, CommandError |
|
|
3 |
from django.core import serializers |
|
|
4 |
from django.utils.datastructures import SortedDict |
|
|
5 |
|
|
|
6 |
from optparse import make_option |
|
|
7 |
|
|
|
8 |
class Command(BaseCommand): |
|
|
9 |
option_list = BaseCommand.option_list + ( |
|
|
10 |
make_option('--format', default='json', dest='format', |
|
|
11 |
help='Specifies the output serialization format for fixtures.'), |
|
|
12 |
make_option('--indent', default=None, dest='indent', type='int', |
|
|
13 |
help='Specifies the indent level to use when pretty-printing output'), |
|
|
14 |
make_option('-e', '--exclude', dest='exclude',action='append', default=[], |
|
|
15 |
help='App to exclude (use multiple --exclude to exclude multiple apps).'), |
|
|
16 |
) |
|
|
17 |
help = 'Output the contents of the database as a fixture of the given format.' |
|
|
18 |
args = '[appname ...]' |
|
|
19 |
|
|
|
20 |
def handle(self, *app_labels, **options): |
|
|
21 |
from django.db.models import get_app, get_apps, get_models, get_model |
|
|
22 |
|
|
|
23 |
format = options.get('format','json') |
|
|
24 |
indent = options.get('indent',None) |
|
|
25 |
exclude = options.get('exclude',[]) |
|
|
26 |
show_traceback = options.get('traceback', False) |
|
|
27 |
|
|
|
28 |
excluded_apps = [get_app(app_label) for app_label in exclude] |
|
|
29 |
|
|
|
30 |
if len(app_labels) == 0: |
|
|
31 |
app_list = SortedDict([(app, None) for app in get_apps() if app not in excluded_apps]) |
|
|
32 |
else: |
|
|
33 |
app_list = SortedDict() |
|
|
34 |
for label in app_labels: |
|
|
35 |
try: |
|
|
36 |
app_label, model_label = label.split('.') |
|
|
37 |
try: |
|
|
38 |
app = get_app(app_label) |
|
|
39 |
except ImproperlyConfigured: |
|
|
40 |
raise CommandError("Unknown application: %s" % app_label) |
|
|
41 |
|
|
|
42 |
model = get_model(app_label, model_label) |
|
|
43 |
if model is None: |
|
|
44 |
raise CommandError("Unknown model: %s.%s" % (app_label, model_label)) |
|
|
45 |
|
|
|
46 |
if app in app_list.keys(): |
|
|
47 |
if app_list[app] and model not in app_list[app]: |
|
|
48 |
app_list[app].append(model) |
|
|
49 |
else: |
|
|
50 |
app_list[app] = [model] |
|
|
51 |
except ValueError: |
|
|
52 |
# This is just an app - no model qualifier |
|
|
53 |
app_label = label |
|
|
54 |
try: |
|
|
55 |
app = get_app(app_label) |
|
|
56 |
except ImproperlyConfigured: |
|
|
57 |
raise CommandError("Unknown application: %s" % app_label) |
|
|
58 |
app_list[app] = None |
|
|
59 |
|
|
|
60 |
# Check that the serialization format exists; this is a shortcut to |
|
|
61 |
# avoid collating all the objects and _then_ failing. |
|
|
62 |
if format not in serializers.get_public_serializer_formats(): |
|
|
63 |
raise CommandError("Unknown serialization format: %s" % format) |
|
|
64 |
|
|
|
65 |
try: |
|
|
66 |
serializers.get_serializer(format) |
|
|
67 |
except KeyError: |
|
|
68 |
raise CommandError("Unknown serialization format: %s" % format) |
|
|
69 |
|
|
|
70 |
objects = [] |
|
|
71 |
for app, model_list in app_list.items(): |
|
|
72 |
if model_list is None: |
|
|
73 |
model_list = get_models(app) |
|
|
74 |
|
|
|
75 |
for model in model_list: |
|
|
76 |
if not model._meta.proxy: |
|
|
77 |
objects.extend(model._default_manager.all()) |
|
|
78 |
|
|
|
79 |
try: |
|
|
80 |
return serializers.serialize(format, objects, indent=indent) |
|
|
81 |
except Exception, e: |
|
|
82 |
if show_traceback: |
|
|
83 |
raise |
|
|
84 |
raise CommandError("Unable to serialize database: %s" % e) |