Ticket #5986: django-fields-order.2.patch

File django-fields-order.2.patch, 4.9 KB (added by Patryk Zawadzki <patrys@…>, 17 years ago)

Correct patch including regression tests

  • django/newforms/forms.py

     
    88from django.utils.html import escape
    99from django.utils.encoding import StrAndUnicode, smart_unicode, force_unicode
    1010from django.utils.safestring import mark_safe
     11from django.core.management.base import CommandError
    1112
    1213from fields import Field
    1314from widgets import TextInput, Textarea
     
    3839            if hasattr(base, 'base_fields'):
    3940                fields = base.base_fields.items() + fields
    4041
     42        # Sort by Meta.fields_order if present.
     43        # Raises CommandError if any of the fields is missing.
     44        if attrs.has_key('Meta') and hasattr(attrs['Meta'], 'fields_order'):
     45            for f in fields:
     46                if not f[0] in attrs['Meta'].fields_order:
     47                    raise CommandError, "%s not present in %s.Meta.fields_order" % (f[0], name)
     48            fields.sort(lambda x, y: cmp(attrs['Meta'].fields_order.index(x[0]), attrs['Meta'].fields_order.index(y[0])))
     49
    4150        attrs['base_fields'] = SortedDict(fields)
    4251        return type.__new__(cls, name, bases, attrs)
    4352
  • tests/regressiontests/forms/fields_order.py

     
     1# -*- coding: utf-8 -*-
     2
     3tests = r"""
     4>>> from django.newforms import *
     5>>> class TestForm(Form):
     6...     f1=CharField(max_length=10)
     7...
     8>>> TestForm(auto_id=False).as_p()
     9u'<p>F1: <input type="text" name="f1" maxlength="10" /></p>'
     10>>> class TestForm(Form):
     11...     f1=CharField(max_length=10)
     12...     class Meta:
     13...         fields_order=['baz']
     14...
     15Traceback (most recent call last):
     16...
     17CommandError: f1 not present in TestForm.Meta.fields_order
     18>>> class TestForm(Form):
     19...     f1=CharField(max_length=10)
     20...     f2=CharField(max_length=10)
     21...     class Meta:
     22...         fields_order=['f1']
     23...
     24Traceback (most recent call last):
     25...
     26CommandError: f2 not present in TestForm.Meta.fields_order
     27>>> class TestForm(Form):
     28...     f1=CharField(max_length=10)
     29...     class Meta:
     30...         fields_order=['f1']
     31...
     32>>> TestForm(auto_id=False).as_p()
     33u'<p>F1: <input type="text" name="f1" maxlength="10" /></p>'
     34>>> class TestForm(Form):
     35...     f1=CharField(max_length=10)
     36...     f2=CharField(max_length=10)
     37...     class Meta:
     38...         fields_order=['f1','f2']
     39...
     40>>> TestForm(auto_id=False).as_p()
     41u'<p>F1: <input type="text" name="f1" maxlength="10" /></p>\n<p>F2: <input type="text" name="f2" maxlength="10" /></p>'
     42>>> class TestForm(Form):
     43...     f1=CharField(max_length=10)
     44...     f2=CharField(max_length=10)
     45...     class Meta:
     46...         fields_order=['f2','f1']
     47...
     48>>> TestForm(auto_id=False).as_p()
     49u'<p>F2: <input type="text" name="f2" maxlength="10" /></p>\n<p>F1: <input type="text" name="f1" maxlength="10" /></p>'
     50>>> class SomeForm(TestForm):
     51...     f3=CharField(max_length=10)
     52...
     53>>> TestForm(auto_id=False).as_p()
     54u'<p>F2: <input type="text" name="f2" maxlength="10" /></p>\n<p>F1: <input type="text" name="f1" maxlength="10" /></p>'
     55>>> SomeForm(auto_id=False).as_p()
     56u'<p>F2: <input type="text" name="f2" maxlength="10" /></p>\n<p>F1: <input type="text" name="f1" maxlength="10" /></p>\n<p>F3: <input type="text" name="f3" maxlength="10" /></p>'
     57>>> class SomeForm(TestForm):
     58...     f3=CharField(max_length=10)
     59...     class Meta:
     60...         fields_order=['f3']
     61...
     62Traceback (most recent call last):
     63...
     64CommandError: f2 not present in SomeForm.Meta.fields_order
     65>>> class SomeForm(TestForm):
     66...     f3=CharField(max_length=10)
     67...     class Meta:
     68...         fields_order=['f3','f2','f1']
     69...
     70>>> SomeForm(auto_id=False).as_p()
     71u'<p>F3: <input type="text" name="f3" maxlength="10" /></p>\n<p>F2: <input type="text" name="f2" maxlength="10" /></p>\n<p>F1: <input type="text" name="f1" maxlength="10" /></p>'
     72"""
  • tests/regressiontests/forms/tests.py

     
    22from extra import tests as extra_tests
    33from fields import tests as fields_tests
    44from forms import tests as form_tests
     5from fields_order import tests as fields_order_tests
    56from error_messages import tests as custom_error_message_tests
    67from localflavor.ar import tests as localflavor_ar_tests
    78from localflavor.au import tests as localflavor_au_tests
     
    2930__test__ = {
    3031    'extra_tests': extra_tests,
    3132    'fields_tests': fields_tests,
     33    'fields_order_test': fields_order_tests,
    3234    'form_tests': form_tests,
    3335    'custom_error_message_tests': custom_error_message_tests,
    3436    'localflavor_ar_tests': localflavor_ar_tests,
Back to Top