Ticket #16226: ticket16226_v2.diff

File ticket16226_v2.diff, 6.3 KB (added by Peter Kese, 13 years ago)

Small code cleanups

  • django/contrib/admin/media/js/prepopulate.js

    diff --git a/django/contrib/admin/media/js/prepopulate.js b/django/contrib/admin/media/js/prepopulate.js
    index 24f24f9..6d8c817 100644
    a b  
    2525                      values.push($(field).val());
    2626                  }
    2727                })
    28                 field.val(URLify(values.join(' '), maxLength));
     28
     29                var newval = values.join(' ');
     30                if (field.hasClass('vSlugField')) newval = URLify(newval, maxLength)
     31                field.val(newval);
    2932            };
    3033
    3134            $(dependencies.join(',')).keyup(populate).change(populate).focus(populate);
  • django/contrib/admin/media/js/prepopulate.min.js

    diff --git a/django/contrib/admin/media/js/prepopulate.min.js b/django/contrib/admin/media/js/prepopulate.min.js
    index aa94937..0c442f8 100644
    a b  
    1 (function(a){a.fn.prepopulate=function(d,g){return this.each(function(){var b=a(this);b.data("_changed",false);b.change(function(){b.data("_changed",true)});var c=function(){if(b.data("_changed")!=true){var e=[];a.each(d,function(h,f){a(f).val().length>0&&e.push(a(f).val())});b.val(URLify(e.join(" "),g))}};a(d.join(",")).keyup(c).change(c).focus(c)})}})(django.jQuery);
     1(function(b){b.fn.prepopulate=function(e,f){return this.each(function(){var a=b(this);a.data("_changed",!1);a.change(function(){a.data("_changed",!0)});var d=function(){if(a.data("_changed")!=!0){var d=[];b.each(e,function(a,c){b(c).val().length>0&&d.push(b(c).val())});var c=d.join(" ");a.hasClass("vSlugField")&&(c=URLify(c,f));a.val(c)}};b(e.join(",")).keyup(d).change(d).focus(d)})}})(django.jQuery);
     2 No newline at end of file
  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    index e9064bb..c83f56e 100644
    a b FORMFIELD_FOR_DBFIELD_DEFAULTS = {  
    5050    models.CharField:       {'widget': widgets.AdminTextInputWidget},
    5151    models.ImageField:      {'widget': widgets.AdminFileWidget},
    5252    models.FileField:       {'widget': widgets.AdminFileWidget},
     53    models.SlugField:       {'widget': widgets.AdminSlugFieldWidget},
    5354}
    5455
    5556csrf_protect_m = method_decorator(csrf_protect)
  • 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..5a4ef06 100644
    a b  
    77        id: '#{{ field.field.auto_id }}',
    88        dependency_ids: [],
    99        dependency_list: [],
    10         maxLength: {{ field.field.field.max_length|default_if_none:"50" }}
     10        maxLength: {{ field.field.field.max_length|default:'null' }}
    1111    };
    1212
    1313    {% for dependency in field.dependencies %}
  • django/contrib/admin/widgets.py

    diff --git a/django/contrib/admin/widgets.py b/django/contrib/admin/widgets.py
    index 957f94f..a7f449b 100644
    a b class AdminCommaSeparatedIntegerFieldWidget(forms.TextInput):  
    293293        if attrs is not None:
    294294            final_attrs.update(attrs)
    295295        super(AdminCommaSeparatedIntegerFieldWidget, self).__init__(attrs=final_attrs)
     296
     297class AdminSlugFieldWidget(forms.TextInput):
     298    def __init__(self, attrs=None):
     299        final_attrs = {'class': 'vSlugField'}
     300        if attrs is not None:
     301            final_attrs.update(attrs)
     302        super(AdminSlugFieldWidget, self).__init__(attrs=final_attrs)
  • tests/regressiontests/admin_widgets/models.py

    diff --git a/tests/regressiontests/admin_widgets/models.py b/tests/regressiontests/admin_widgets/models.py
    index c187584..59d8aaf 100644
    a b class Member(models.Model):  
    1515
    1616class Band(models.Model):
    1717    name = models.CharField(max_length=100)
     18    slug = models.SlugField(max_length=100)
    1819    members = models.ManyToManyField(Member)
    1920
    2021    def __unicode__(self):
  • tests/regressiontests/admin_widgets/tests.py

    diff --git a/tests/regressiontests/admin_widgets/tests.py b/tests/regressiontests/admin_widgets/tests.py
    index 8f7a36c..bda4181 100644
    a b class AdminFormfieldForDBFieldTests(TestCase):  
    119119    def testInheritance(self):
    120120        self.assertFormfield(models.Album, 'backside_art', widgets.AdminFileWidget)
    121121
     122    def testSlugField(self):
     123        self.assertFormfield(models.Band, 'slug', widgets.AdminSlugFieldWidget)
    122124
    123125class AdminFormfieldForDBFieldWithRequestTests(DjangoTestCase):
    124126    fixtures = ["admin-widgets-users.xml"]
    class RelatedFieldWidgetWrapperTests(DjangoTestCase):  
    328330        # Used to fail with a name error.
    329331        w = RelatedFieldWidgetWrapper(w, rel, admin.site)
    330332        self.assertFalse(w.can_add_related)
     333
     334class AdminSlugWidgetPrepopulate(DjangoTestCase):
     335    fixtures = ["admin-widgets-users.xml"]
     336    admin_root = '/widget_admin'
     337
     338    def setUp(self):
     339        self.client.login(username="super", password="secret")
     340
     341    def tearDown(self):
     342        self.client.logout()
     343
     344    def test_prepopulated_slug_field_existence_and_js(self):
     345        response = self.client.get('%s/admin_widgets/band/add/' % self.admin_root)
     346        self.assertTrue('<input id="id_slug" type="text" class="vSlugField" name="slug" maxlength="100" />' in response.content)
     347        self.assertTrue("$('.empty-form .slug').addClass('prepopulated_field');" in response.content)
  • tests/regressiontests/admin_widgets/widgetadmin.py

    diff --git a/tests/regressiontests/admin_widgets/widgetadmin.py b/tests/regressiontests/admin_widgets/widgetadmin.py
    index 6f15d92..e527f43 100644
    a b import models  
    88class WidgetAdmin(admin.AdminSite):
    99    pass
    1010
     11class BandAdmin(admin.ModelAdmin):
     12    prepopulated_fields = {'slug': ['name']}
     13
    1114class CarAdmin(admin.ModelAdmin):
    1215    list_display = ['make', 'model', 'owner']
    1316    list_editable = ['owner']
    class EventAdmin(admin.ModelAdmin):  
    2528site = WidgetAdmin(name='widget-admin')
    2629
    2730site.register(models.User)
     31site.register(models.Band, BandAdmin)
    2832site.register(models.Car, CarAdmin)
    2933site.register(models.CarTire, CarTireAdmin)
    3034site.register(models.Event, EventAdmin)
Back to Top