From fc413b8f611677e51ea8c24e3b56a3716652a619 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Mon, 17 Dec 2007 06:53:15 +0000 Subject: [PATCH] Fixed #6110 -- Mark the python format serializer as for internal use only. Thanks, empty. git-svn-id: http://code.djangoproject.com/svn/django/trunk@6922 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/management/commands/dumpdata.py | 8 ++++++-- django/core/management/commands/loaddata.py | 4 ++-- django/core/serializers/__init__.py | 5 +++++ django/core/serializers/base.py | 4 ++++ django/core/serializers/json.py | 2 ++ django/core/serializers/python.py | 4 +++- django/core/serializers/pyyaml.py | 2 ++ 7 files changed, 24 insertions(+), 5 deletions(-) diff --git a/django/core/management/commands/dumpdata.py b/django/core/management/commands/dumpdata.py index 7bdcc4271f..fdc8fe9293 100644 --- a/django/core/management/commands/dumpdata.py +++ b/django/core/management/commands/dumpdata.py @@ -1,11 +1,13 @@ from django.core.management.base import BaseCommand, CommandError +from django.core import serializers from optparse import make_option class Command(BaseCommand): + serializer_formats = serializers.get_public_serializer_formats() option_list = BaseCommand.option_list + ( make_option('--format', default='json', dest='format', - help='Specifies the output serialization format for fixtures'), + help='Specifies the output serialization format for fixtures. Formats available: %s' % serializer_formats), make_option('--indent', default=None, dest='indent', type='int', help='Specifies the indent level to use when pretty-printing output'), ) @@ -14,7 +16,6 @@ class Command(BaseCommand): def handle(self, *app_labels, **options): from django.db.models import get_app, get_apps, get_models - from django.core import serializers format = options.get('format', 'json') indent = options.get('indent', None) @@ -26,6 +27,9 @@ class Command(BaseCommand): # Check that the serialization format exists; this is a shortcut to # avoid collating all the objects and _then_ failing. + if format not in self.serializer_formats: + raise CommandError("Unknown serialization format: %s" % format) + try: serializers.get_serializer(format) except KeyError: diff --git a/django/core/management/commands/loaddata.py b/django/core/management/commands/loaddata.py index 216db09141..e22b78d517 100644 --- a/django/core/management/commands/loaddata.py +++ b/django/core/management/commands/loaddata.py @@ -50,10 +50,10 @@ class Command(BaseCommand): parts = fixture_label.split('.') if len(parts) == 1: fixture_name = fixture_label - formats = serializers.get_serializer_formats() + formats = serializers.get_public_serializer_formats() else: fixture_name, format = '.'.join(parts[:-1]), parts[-1] - if format in serializers.get_serializer_formats(): + if format in serializers.get_public_serializer_formats(): formats = [format] else: formats = [] diff --git a/django/core/serializers/__init__.py b/django/core/serializers/__init__.py index 1e24e2bd22..47dfb0c87a 100644 --- a/django/core/serializers/__init__.py +++ b/django/core/serializers/__init__.py @@ -53,6 +53,11 @@ def get_serializer_formats(): _load_serializers() return _serializers.keys() +def get_public_serializer_formats(): + if not _serializers: + _load_serializers() + return [k for k, v in _serializers.iteritems() if not v.Serializer.internal_use_only] + def get_deserializer(format): if not _serializers: _load_serializers() diff --git a/django/core/serializers/base.py b/django/core/serializers/base.py index ee9e4dd621..3c7dcfa02e 100644 --- a/django/core/serializers/base.py +++ b/django/core/serializers/base.py @@ -22,6 +22,10 @@ class Serializer(object): Abstract serializer base class. """ + # Indicates if the implemented serializer is only available for + # internal Django use. + internal_use_only = False + def serialize(self, queryset, **options): """ Serialize a queryset. diff --git a/django/core/serializers/json.py b/django/core/serializers/json.py index fa2dca7295..e17b821f52 100644 --- a/django/core/serializers/json.py +++ b/django/core/serializers/json.py @@ -20,6 +20,8 @@ class Serializer(PythonSerializer): """ Convert a queryset to JSON. """ + internal_use_only = False + def end_serialization(self): self.options.pop('stream', None) self.options.pop('fields', None) diff --git a/django/core/serializers/python.py b/django/core/serializers/python.py index 6fc13d76b5..cedab06be9 100644 --- a/django/core/serializers/python.py +++ b/django/core/serializers/python.py @@ -13,7 +13,9 @@ class Serializer(base.Serializer): """ Serializes a QuerySet to basic Python objects. """ - + + internal_use_only = True + def start_serialization(self): self._current = None self.objects = [] diff --git a/django/core/serializers/pyyaml.py b/django/core/serializers/pyyaml.py index 772f34a2e3..4c32a9686f 100644 --- a/django/core/serializers/pyyaml.py +++ b/django/core/serializers/pyyaml.py @@ -19,6 +19,8 @@ class Serializer(PythonSerializer): Convert a queryset to YAML. """ + internal_use_only = False + def handle_field(self, obj, field): # A nasty special case: base YAML doesn't support serialization of time # types (as opposed to dates or datetimes, which it does support). Since