Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #14824 -- Corrected the handling of formats when USE_L10N is di…

…sabled. Thanks to nullie for the report and initial patch, and to idle for the separate report with helpful debug info.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15404 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit d44fb0557a0a9d8fab62b1e48078be498c61287a 1 parent 987fd51
Russell Keith-Magee authored February 04, 2011
12  django/utils/formats.py
@@ -14,11 +14,21 @@
14 14
 _format_cache = {}
15 15
 _format_modules_cache = {}
16 16
 
  17
+def reset_format_cache():
  18
+    """Clear any cached formats.
  19
+
  20
+    This method is provided primarily for testing purposes,
  21
+    so that the effects of cached formats can be removed.
  22
+    """
  23
+    global _format_cache, _format_modules_cache
  24
+    _format_cache = {}
  25
+    _format_modules_cache = {}
  26
+
17 27
 def iter_format_modules(lang):
18 28
     """
19 29
     Does the heavy lifting of finding format modules.
20 30
     """
21  
-    if check_for_language(lang) or settings.USE_L10N:
  31
+    if check_for_language(lang):
22 32
         format_locations = ['django.conf.locale.%s']
23 33
         if settings.FORMAT_MODULE_PATH:
24 34
             format_locations.append(settings.FORMAT_MODULE_PATH + '.%s')
7  django/views/i18n.py
@@ -7,7 +7,7 @@
7 7
 from django.utils.translation import check_for_language, activate, to_locale, get_language
8 8
 from django.utils.text import javascript_quote
9 9
 from django.utils.encoding import smart_unicode
10  
-from django.utils.formats import get_format_modules
  10
+from django.utils.formats import get_format_modules, get_format
11 11
 
12 12
 def set_language(request):
13 13
     """
@@ -49,10 +49,7 @@ def get_formats():
49 49
     result = {}
50 50
     for module in [settings] + get_format_modules(reverse=True):
51 51
         for attr in FORMAT_SETTINGS:
52  
-            try:
53  
-                result[attr] = getattr(module, attr)
54  
-            except AttributeError:
55  
-                pass
  52
+            result[attr] = get_format(attr)
56 53
     src = []
57 54
     for k, v in result.items():
58 55
         if isinstance(v, (basestring, int)):
50  tests/regressiontests/admin_views/tests.py
@@ -48,12 +48,18 @@ class AdminViewBasicTest(TestCase):
48 48
     urlbit = 'admin'
49 49
 
50 50
     def setUp(self):
51  
-        self.old_language_code = settings.LANGUAGE_CODE
  51
+        self.old_USE_I18N = settings.LANGUAGE_CODE
  52
+        self.old_USE_L10N = settings.USE_L10N
  53
+        self.old_LANGUAGE_CODE = settings.LANGUAGE_CODE
52 54
         self.client.login(username='super', password='secret')
  55
+        settings.USE_I18N = True
53 56
 
54 57
     def tearDown(self):
55  
-        settings.LANGUAGE_CODE = self.old_language_code
  58
+        settings.USE_I18N = self.old_USE_I18N
  59
+        settings.USE_L10N = self.old_USE_L10N
  60
+        settings.LANGUAGE_CODE = self.old_LANGUAGE_CODE
56 61
         self.client.logout()
  62
+        formats.reset_format_cache()
57 63
 
58 64
     def testTrailingSlashRequired(self):
59 65
         """
@@ -351,22 +357,42 @@ def testI18NLanguageNonEnglishDefault(self):
351 357
         if the default language is non-English but the selected language
352 358
         is English. See #13388 and #3594 for more details.
353 359
         """
354  
-        settings.LANGUAGE_CODE = 'fr'
355  
-        activate('en-us')
356  
-        response = self.client.get('/test_admin/admin/jsi18n/')
357  
-        self.assertNotContains(response, 'Choisir une heure')
358  
-        deactivate()
  360
+        try:
  361
+            settings.LANGUAGE_CODE = 'fr'
  362
+            activate('en-us')
  363
+            response = self.client.get('/test_admin/admin/jsi18n/')
  364
+            self.assertNotContains(response, 'Choisir une heure')
  365
+        finally:
  366
+            deactivate()
359 367
 
360 368
     def testI18NLanguageNonEnglishFallback(self):
361 369
         """
362 370
         Makes sure that the fallback language is still working properly
363 371
         in cases where the selected language cannot be found.
364 372
         """
365  
-        settings.LANGUAGE_CODE = 'fr'
366  
-        activate('none')
367  
-        response = self.client.get('/test_admin/admin/jsi18n/')
368  
-        self.assertContains(response, 'Choisir une heure')
369  
-        deactivate()
  373
+        try:
  374
+            settings.LANGUAGE_CODE = 'fr'
  375
+            activate('none')
  376
+            response = self.client.get('/test_admin/admin/jsi18n/')
  377
+            self.assertContains(response, 'Choisir une heure')
  378
+        finally:
  379
+            deactivate()
  380
+
  381
+    def testL10NDeactivated(self):
  382
+        """
  383
+        Check if L10N is deactivated, the Javascript i18n view doesn't
  384
+        return localized date/time formats. Refs #14824.
  385
+        """
  386
+        try:
  387
+            settings.LANGUAGE_CODE = 'ru'
  388
+            settings.USE_L10N = False
  389
+            activate('ru')
  390
+            response = self.client.get('/test_admin/admin/jsi18n/')
  391
+            self.assertNotContains(response, '%d.%m.%Y %H:%M:%S')
  392
+            self.assertContains(response, '%Y-%m-%d %H:%M:%S')
  393
+        finally:
  394
+            deactivate()
  395
+
370 396
 
371 397
     def test_disallowed_filtering(self):
372 398
         self.assertRaises(SuspiciousOperation,

0 notes on commit d44fb05

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