Ticket #5423: dumpdata_streamed_output_django1.0-fix.diff

File dumpdata_streamed_output_django1.0-fix.diff, 6.7 KB (added by emes, 6 years ago)

Corrected bugs.

  • django/core/management/commands/dumpdata.py

    diff -ura Django-1.0-v/django/core/management/commands/dumpdata.py Django-1.0/django/core/management/commands/dumpdata.py
    old new  
    22from django.core import serializers
    33
    44from optparse import make_option
     5import sys
    56
    67class Command(BaseCommand):
    78    option_list = BaseCommand.option_list + (
     
    4041        except KeyError:
    4142            raise CommandError("Unknown serialization format: %s" % format)
    4243
    43         objects = []
    44         for app in app_list:
    45             for model in get_models(app):
    46                 objects.extend(model._default_manager.all())
     44        def get_objects ():
     45            for app in app_list:
     46                for model in get_models(app):
     47                    for obj in model._default_manager.order_by(model._meta.pk.name).iterator():
     48                        yield obj
    4749        try:
    48             return serializers.serialize(format, objects, indent=indent)
     50            serializers.serialize(format, get_objects(), indent=indent, stream=sys.stdout)
    4951        except Exception, e:
    5052            if show_traceback:
    5153                raise
  • django/core/management/__init__.py

    diff -ura Django-1.0-v/django/core/management/__init__.py Django-1.0/django/core/management/__init__.py
    old new  
    2222    """
    2323    command_dir = os.path.join(management_dir, 'commands')
    2424    try:
    25         return [f[:-3] for f in os.listdir(command_dir)
    26                 if not f.startswith('_') and f.endswith('.py')]
     25        return [f[:f.rfind('.')] for f in os.listdir(command_dir)
     26                if not f.startswith('_') and (f.endswith('.py') or f.endswith('.pyc'))]
    2727    except OSError:
    2828        return []
    2929
  • django/core/serializers/base.py

    diff -ura Django-1.0-v/django/core/serializers/base.py Django-1.0/django/core/serializers/base.py
    old new  
    3535        self.selected_fields = options.get("fields")
    3636
    3737        self.start_serialization()
     38        self.first = True
    3839        for obj in queryset:
    3940            self.start_object(obj)
    4041            for field in obj._meta.local_fields:
     
    5051                    if self.selected_fields is None or field.attname in self.selected_fields:
    5152                        self.handle_m2m_field(obj, field)
    5253            self.end_object(obj)
     54            if self.first:
     55                self.first = False
    5356        self.end_serialization()
    5457        return self.getvalue()
    5558
  • django/core/serializers/json.py

    diff -ura Django-1.0-v/django/core/serializers/json.py Django-1.0/django/core/serializers/json.py
    old new  
    2121    """
    2222    internal_use_only = False
    2323
     24    def start_serialization(self):
     25        self._current = None
     26        self.json_kwargs = self.options.copy()
     27        self.json_kwargs.pop('stream', None)
     28        self.json_kwargs.pop('fields', None)
     29        self.stream.write("[")
     30   
    2431    def end_serialization(self):
    25         self.options.pop('stream', None)
    26         self.options.pop('fields', None)
    27         simplejson.dump(self.objects, self.stream, cls=DjangoJSONEncoder, **self.options)
    28 
     32         if self.options.get("indent"):
     33            self.stream.write("\n")
     34            self.stream.write("]")
     35            if self.options.get("indent"):
     36                self.stream.write("\n")
     37
     38    def end_object(self, obj):
     39        # self._current has the field data
     40        if not self.first:
     41            self.stream.write(",")
     42        if not self.options.get("indent"):
     43            self.stream.write(" ")
     44        if self.options.get("indent"):
     45            self.stream.write("\n")
     46        simplejson.dump(self.get_dump_object(obj), self.stream,
     47                        cls=DjangoJSONEncoder, **self.json_kwargs)
     48        self._current = None
     49           
    2950    def getvalue(self):
     51        # overwrite PythonSerializer.getvalue() with base Serializer.getvalue()
    3052        if callable(getattr(self.stream, 'getvalue', None)):
    3153            return self.stream.getvalue()
    3254
  • django/core/serializers/python.py

    diff -ura Django-1.0-v/django/core/serializers/python.py Django-1.0/django/core/serializers/python.py
    old new  
    2727        self._current = {}
    2828
    2929    def end_object(self, obj):
    30         self.objects.append({
     30        self.objects.append(self.get_dump_object(obj))
     31        self._current = None
     32
     33    def get_dump_object (self, obj):
     34        return {
    3135            "model"  : smart_unicode(obj._meta),
    3236            "pk"     : smart_unicode(obj._get_pk_val(), strings_only=True),
    3337            "fields" : self._current
    34         })
    35         self._current = None
     38        }
    3639
    3740    def handle_field(self, obj, field):
    38         self._current[field.name] = smart_unicode(getattr(obj, field.name), strings_only=True)
     41        value = smart_unicode(getattr(obj, field.name), strings_only=True)
     42        self._current[field.name] = value
    3943
    4044    def handle_fk_field(self, obj, field):
    4145        related = getattr(obj, field.name)
  • tests/modeltests/fixtures/models.py

    diff -ura Django-1.0-v/tests/modeltests/fixtures/models.py Django-1.0/tests/modeltests/fixtures/models.py
    old new  
    8181
    8282# Dump the current contents of the database as a JSON fixture
    8383>>> management.call_command('dumpdata', 'fixtures', format='json')
    84 [{"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"}}]
     84[{"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"}}]
    8585"""
    8686
    8787from django.test import TestCase
Back to Top