Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.2.X] Fixed #14823 -- Corrected bootstrapping problems with registe…

…r_serializers. Thanks to miker985@uw.edu for the report and draft patch.

Backport of r15336 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@15339 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 407208b0c2b237f8ba7b1bd9d209b1053e6c77c4 1 parent 81f844a
Russell Keith-Magee authored
4  django/core/serializers/__init__.py
@@ -48,6 +48,8 @@ def register_serializer(format, serializer_module, serializers=None):
48 48
     directly into the global register of serializers. Adding serializers
49 49
     directly is not a thread-safe operation.
50 50
     """
  51
+    if serializers is None and not _serializers:
  52
+        _load_serializers()
51 53
     module = importlib.import_module(serializer_module)
52 54
     if serializers is None:
53 55
         _serializers[format] = module
@@ -56,6 +58,8 @@ def register_serializer(format, serializer_module, serializers=None):
56 58
 
57 59
 def unregister_serializer(format):
58 60
     "Unregister a given serializer. This is not a thread-safe operation."
  61
+    if not _serializers:
  62
+        _load_serializers()
59 63
     del _serializers[format]
60 64
 
61 65
 def get_serializer(format):
52  tests/modeltests/serializers/tests.py
... ...
@@ -1,8 +1,10 @@
1 1
 # -*- coding: utf-8 -*-
2 2
 from datetime import datetime
3 3
 from StringIO import StringIO
  4
+import unittest
4 5
 from xml.dom import minidom
5 6
 
  7
+from django.conf import settings
6 8
 from django.core import serializers
7 9
 from django.db import transaction
8 10
 from django.test import TestCase, TransactionTestCase, Approximate
@@ -11,6 +13,56 @@
11 13
 from models import Category, Author, Article, AuthorProfile, Actor, \
12 14
                                     Movie, Score, Player, Team
13 15
 
  16
+class SerializerRegistrationTests(unittest.TestCase):
  17
+    def setUp(self):
  18
+        self.old_SERIALIZATION_MODULES = getattr(settings, 'SERIALIZATION_MODULES', None)
  19
+        self.old_serializers = serializers._serializers
  20
+
  21
+        serializers._serializers = {}
  22
+        settings.SERIALIZATION_MODULES = {
  23
+            "json2" : "django.core.serializers.json",
  24
+        }
  25
+
  26
+    def tearDown(self):
  27
+        serializers._serializers = self.old_serializers
  28
+        if self.old_SERIALIZATION_MODULES:
  29
+            settings.SERIALIZATION_MODULES = self.old_SERIALIZATION_MODULES
  30
+        else:
  31
+            delattr(settings, 'SERIALIZATION_MODULES')
  32
+
  33
+    def test_register(self):
  34
+        "Registering a new serializer populates the full registry. Refs #14823"
  35
+        serializers.register_serializer('json3', 'django.core.serializers.json')
  36
+
  37
+        public_formats = serializers.get_public_serializer_formats()
  38
+        self.assertTrue('json3' in public_formats)
  39
+        self.assertTrue('json2' in public_formats)
  40
+        self.assertTrue('xml' in public_formats)
  41
+
  42
+    def test_unregister(self):
  43
+        "Unregistering a serializer doesn't cause the registry to be repopulated. Refs #14823"
  44
+        serializers.unregister_serializer('xml')
  45
+        serializers.register_serializer('json3', 'django.core.serializers.json')
  46
+
  47
+        public_formats = serializers.get_public_serializer_formats()
  48
+
  49
+        self.assertFalse('xml' in public_formats)
  50
+        self.assertTrue('json3' in public_formats)
  51
+
  52
+    def test_builtin_serializers(self):
  53
+        "Requesting a list of serializer formats popuates the registry"
  54
+        all_formats = set(serializers.get_serializer_formats())
  55
+        public_formats = set(serializers.get_public_serializer_formats())
  56
+
  57
+        self.assertTrue('xml' in all_formats),
  58
+        self.assertTrue('xml' in public_formats)
  59
+
  60
+        self.assertTrue('json2' in all_formats)
  61
+        self.assertTrue('json2' in public_formats)
  62
+
  63
+        self.assertTrue('python' in all_formats)
  64
+        self.assertFalse('python' in public_formats)
  65
+
14 66
 class SerializersTestBase(object):
15 67
     @staticmethod
16 68
     def _comparison_value(value):

0 notes on commit 407208b

Please sign in to comment.
Something went wrong with that request. Please try again.