Ticket #15938: unlocalize_maxLength.diff

File unlocalize_maxLength.diff, 3.1 KB (added by Mathieu Agopian, 7 years ago)

Lev's patch with added regression tests

  • django/contrib/admin/templates/admin/prepopulated_fields_js.html

    diff --git a/django/contrib/admin/templates/admin/prepopulated_fields_js.html b/django/contrib/admin/templates/admin/prepopulated_fields_js.html
    index 43ef5ba..7300bea 100644
    a b  
     1{% load l10n %}
    12<script type="text/javascript">
    23(function($) {
    34    var field = null;
     
    78        id: '#{{ field.field.auto_id }}',
    89        dependency_ids: [],
    910        dependency_list: [],
    10         maxLength: {{ field.field.field.max_length|default_if_none:"50" }}
     11        maxLength: {{ field.field.field.max_length|default_if_none:"50"|unlocalize }}
    1112    };
    1213
    1314    {% for dependency in field.dependencies %}
  • tests/regressiontests/admin_views/models.py

    diff --git a/tests/regressiontests/admin_views/models.py b/tests/regressiontests/admin_views/models.py
    index 97f6708..3b7bea8 100644
    a b class PrePopulatedSubPost(models.Model): 
    539539    subtitle = models.CharField(max_length=100)
    540540    subslug = models.SlugField()
    541541
     542class PrePopulatedPostLargeSlug(models.Model):
     543    """
     544    Regression test for #15938: a large max_length for the slugfield must not
     545    be localized in prepopulated_fields_js.html or it might end up breaking
     546    the javascript (ie, using THOUSAND_SEPARATOR ends up with maxLength=1,000)
     547    """
     548    title = models.CharField(max_length=100)
     549    published = models.BooleanField()
     550    slug = models.SlugField(max_length=1000)
     551
     552class PrePopulatedPostLargeSlugAdmin(admin.ModelAdmin):
     553    prepopulated_fields = {
     554        'slug' : ('title',)
     555    }
     556
    542557class SubPostInline(admin.TabularInline):
    543558    model = PrePopulatedSubPost
    544559
    admin.site.register(Album, AlbumAdmin) 
    864879admin.site.register(Question)
    865880admin.site.register(Answer)
    866881admin.site.register(PrePopulatedPost, PrePopulatedPostAdmin)
     882admin.site.register(PrePopulatedPostLargeSlug, PrePopulatedPostLargeSlugAdmin)
  • tests/regressiontests/admin_views/tests.py

    diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py
    index 0537160..1102d64 100644
    a b class PrePopulatedTest(TestCase): 
    26142614        self.assertNotContains(response, "field['dependency_ids'].push('#id_title');")
    26152615        self.assertNotContains(response, "id: '#id_prepopulatedsubpost_set-0-subslug',")
    26162616
     2617    def test_prepopulated_maxlength_localized(self):
     2618        """
     2619        Regression test for #15938: if USE_THOUSAND_SEPARATOR is set, make sure
     2620        that maxLength (in the javascript) is rendered without separators.
     2621        """
     2622        self.old_USE_THOUSAND_SEPARATOR = settings.USE_THOUSAND_SEPARATOR
     2623        self.old_USE_L10N = settings.USE_L10N
     2624        settings.USE_THOUSAND_SEPARATOR = True
     2625        settings.USE_L10N = True
     2626        response = self.client.get('/test_admin/admin/admin_views/prepopulatedpostlargeslug/add/')
     2627        self.assertContains(response, "maxLength: 1000") # instead of 1,000
     2628
     2629
    26172630class ReadonlyTest(TestCase):
    26182631    fixtures = ['admin-views-users.xml']
    26192632
Back to Top