Code

Ticket #5423: dumpdata_streamed_output_django1.0.diff

File dumpdata_streamed_output_django1.0.diff, 5.8 KB (added by captnswing, 6 years ago)

fixed patch to work with django 1.0

Line 
1Index: django/core/serializers/json.py
2===================================================================
3--- django/core/serializers/json.py     (revision 9404)
4+++ django/core/serializers/json.py     (arbetskopia)
5@@ -21,12 +21,34 @@
6     """
7     internal_use_only = False
8 
9+    def start_serialization(self):
10+        self._current = None
11+        self.json_kwargs = self.options.copy()
12+        self.json_kwargs.pop('stream', None)
13+        self.json_kwargs.pop('fields', None)
14+        self.stream.write("[")
15+   
16     def end_serialization(self):
17-        self.options.pop('stream', None)
18-        self.options.pop('fields', None)
19-        simplejson.dump(self.objects, self.stream, cls=DjangoJSONEncoder, **self.options)
20-
21+         if self.options.get("indent"):
22+            self.stream.write("\n")
23+            self.stream.write("]")
24+            if self.options.get("indent"):
25+                self.stream.write("\n")
26+
27+    def end_object(self, obj):
28+        # self._current has the field data
29+        if not self.first:
30+            self.stream.write(",")
31+        if not self.options.get("indent"):
32+            self.stream.write(" ")
33+        if self.options.get("indent"):
34+            self.stream.write("\n")
35+        simplejson.dump(self.get_dump_object(obj), self.stream,
36+                        cls=DjangoJSONEncoder, **self.json_kwargs)
37+        self._current = None
38+           
39     def getvalue(self):
40+        # overwrite PythonSerializer.getvalue() with base Serializer.getvalue()
41         if callable(getattr(self.stream, 'getvalue', None)):
42             return self.stream.getvalue()
43 
44Index: django/core/serializers/base.py
45===================================================================
46--- django/core/serializers/base.py     (revision 9404)
47+++ django/core/serializers/base.py     (arbetskopia)
48@@ -37,6 +37,7 @@
49         self.start_serialization()
50         for obj in queryset:
51             self.start_object(obj)
52+            self.first = True
53             for field in obj._meta.local_fields:
54                 if field.serialize:
55                     if field.rel is None:
56@@ -50,6 +51,8 @@
57                     if self.selected_fields is None or field.attname in self.selected_fields:
58                         self.handle_m2m_field(obj, field)
59             self.end_object(obj)
60+            if self.first:
61+                self.first = False
62         self.end_serialization()
63         return self.getvalue()
64 
65Index: django/core/serializers/python.py
66===================================================================
67--- django/core/serializers/python.py   (revision 9404)
68+++ django/core/serializers/python.py   (arbetskopia)
69@@ -27,15 +27,19 @@
70         self._current = {}
71 
72     def end_object(self, obj):
73-        self.objects.append({
74+        self.objects.append(self.get_dump_object(obj))
75+        self._current = None
76+
77+    def get_dump_object (self, obj):
78+        return {
79             "model"  : smart_unicode(obj._meta),
80             "pk"     : smart_unicode(obj._get_pk_val(), strings_only=True),
81             "fields" : self._current
82-        })
83-        self._current = None
84+        }
85 
86     def handle_field(self, obj, field):
87-        self._current[field.name] = smart_unicode(getattr(obj, field.name), strings_only=True)
88+        value = smart_unicode(getattr(obj, field.name), strings_only=True)
89+        self._current[field.name] = value
90 
91     def handle_fk_field(self, obj, field):
92         related = getattr(obj, field.name)
93Index: django/core/management/commands/dumpdata.py
94===================================================================
95--- django/core/management/commands/dumpdata.py (revision 9404)
96+++ django/core/management/commands/dumpdata.py (arbetskopia)
97@@ -2,6 +2,7 @@
98 from django.core import serializers
99 
100 from optparse import make_option
101+import sys
102 
103 class Command(BaseCommand):
104     option_list = BaseCommand.option_list + (
105@@ -40,12 +41,13 @@
106         except KeyError:
107             raise CommandError("Unknown serialization format: %s" % format)
108 
109-        objects = []
110-        for app in app_list:
111-            for model in get_models(app):
112-                objects.extend(model._default_manager.all())
113+        def get_objects ():
114+            for app in app_list:
115+                for model in get_models(app):
116+                    for obj in model._default_manager.order_by(model._meta.pk.attname).iterator():
117+                        yield obj
118         try:
119-            return serializers.serialize(format, objects, indent=indent)
120+            serializers.serialize(format, get_objects(), indent=indent, stream=sys.stdout)
121         except Exception, e:
122             if show_traceback:
123                 raise
124Index: tests/modeltests/fixtures/models.py
125===================================================================
126--- tests/modeltests/fixtures/models.py (revision 9404)
127+++ tests/modeltests/fixtures/models.py (arbetskopia)
128@@ -81,7 +81,7 @@
129 
130 # Dump the current contents of the database as a JSON fixture
131 >>> management.call_command('dumpdata', 'fixtures', format='json')
132-[{"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}]
133+[{"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}]
134 """
135 
136 from django.test import TestCase