Commit 9d8f41ba authored by Brian Rosner's avatar Brian Rosner
Browse files

Made the semi-private _max_form_count live on the public API of formsets by...

Made the semi-private _max_form_count live on the public API of formsets by renaming it to max_num. This also removes the ManagementForm use of MAX_COUNT since that usage should just be referenced to the formset's max_num property. Refs #7899. Thanks Peter of the Norse for straightening me out.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8058 bcc190cf-cafb-0310-a4f2-bffc1f526a37
parent 62d9e278
Loading
Loading
Loading
Loading
+6 −10
Original line number Diff line number Diff line
@@ -10,7 +10,6 @@ __all__ = ('BaseFormSet', 'all_valid')
# special field names
TOTAL_FORM_COUNT = 'TOTAL_FORMS'
INITIAL_FORM_COUNT = 'INITIAL_FORMS'
MAX_FORM_COUNT = 'MAX_FORMS'
ORDERING_FIELD_NAME = 'ORDER'
DELETION_FIELD_NAME = 'DELETE'

@@ -23,7 +22,6 @@ class ManagementForm(Form):
    def __init__(self, *args, **kwargs):
        self.base_fields[TOTAL_FORM_COUNT] = IntegerField(widget=HiddenInput)
        self.base_fields[INITIAL_FORM_COUNT] = IntegerField(widget=HiddenInput)
        self.base_fields[MAX_FORM_COUNT] = IntegerField(widget=HiddenInput)
        super(ManagementForm, self).__init__(*args, **kwargs)

class BaseFormSet(StrAndUnicode):
@@ -47,23 +45,21 @@ class BaseFormSet(StrAndUnicode):
            if self.management_form.is_valid():
                self._total_form_count = self.management_form.cleaned_data[TOTAL_FORM_COUNT]
                self._initial_form_count = self.management_form.cleaned_data[INITIAL_FORM_COUNT]
                self._max_form_count = self.management_form.cleaned_data[MAX_FORM_COUNT]
            else:
                raise ValidationError('ManagementForm data is missing or has been tampered with')
        else:
            if initial:
                self._initial_form_count = len(initial)
                if self._initial_form_count > self._max_form_count and self._max_form_count > 0:
                    self._initial_form_count = self._max_form_count
                if self._initial_form_count > self.max_num and self.max_num > 0:
                    self._initial_form_count = self.max_num
                self._total_form_count = self._initial_form_count + self.extra
            else:
                self._initial_form_count = 0
                self._total_form_count = self.extra
            if self._total_form_count > self._max_form_count and self._max_form_count > 0:
                self._total_form_count = self._max_form_count
            if self._total_form_count > self.max_num and self.max_num > 0:
                self._total_form_count = self.max_num
            initial = {TOTAL_FORM_COUNT: self._total_form_count,
                       INITIAL_FORM_COUNT: self._initial_form_count,
                       MAX_FORM_COUNT: self._max_form_count}
                       INITIAL_FORM_COUNT: self._initial_form_count}
            self.management_form = ManagementForm(initial=initial, auto_id=self.auto_id, prefix=self.prefix)
        
        # construct the forms in the formset
@@ -280,7 +276,7 @@ def formset_factory(form, formset=BaseFormSet, extra=1, can_order=False,
    """Return a FormSet for the given form class."""
    attrs = {'form': form, 'extra': extra,
             'can_order': can_order, 'can_delete': can_delete,
             '_max_form_count': max_num}
             'max_num': max_num}
    return type(form.__name__ + 'FormSet', (formset,), attrs)

def all_valid(formsets):
+2 −2
Original line number Diff line number Diff line
@@ -305,8 +305,8 @@ class BaseModelFormSet(BaseFormSet):
                 queryset=None, **kwargs):
        self.queryset = queryset
        defaults = {'data': data, 'files': files, 'auto_id': auto_id, 'prefix': prefix}
        if self._max_form_count > 0:
            qs = self.get_queryset()[:self._max_form_count]
        if self.max_num > 0:
            qs = self.get_queryset()[:self.max_num]
        else:
            qs = self.get_queryset()
        defaults['initial'] = [model_to_dict(obj) for obj in qs]
+0 −8
Original line number Diff line number Diff line
@@ -41,7 +41,6 @@ __test__ = {'API_TESTS': """
>>> data = {
...     'form-TOTAL_FORMS': '3', # the number of forms rendered
...     'form-INITIAL_FORMS': '0', # the number of forms with initial data
...     'form-MAX_FORMS': '0', # the max number of forms
...     'form-0-name': 'Charles Baudelaire',
...     'form-1-name': 'Arthur Rimbaud',
...     'form-2-name': '',
@@ -79,7 +78,6 @@ them in alphabetical order by name.
>>> data = {
...     'form-TOTAL_FORMS': '3', # the number of forms rendered
...     'form-INITIAL_FORMS': '2', # the number of forms with initial data
...     'form-MAX_FORMS': '0', # the max number of forms
...     'form-0-id': '2',
...     'form-0-name': 'Arthur Rimbaud',
...     'form-1-id': '1',
@@ -123,7 +121,6 @@ deltetion, make sure we don't save that form.
>>> data = {
...     'form-TOTAL_FORMS': '4', # the number of forms rendered
...     'form-INITIAL_FORMS': '3', # the number of forms with initial data
...     'form-MAX_FORMS': '0', # the max number of forms
...     'form-0-id': '2',
...     'form-0-name': 'Arthur Rimbaud',
...     'form-1-id': '1',
@@ -153,7 +150,6 @@ Let's edit a record to ensure save only returns that one record.
>>> data = {
...     'form-TOTAL_FORMS': '4', # the number of forms rendered
...     'form-INITIAL_FORMS': '3', # the number of forms with initial data
...     'form-MAX_FORMS': '0', # the max number of forms
...     'form-0-id': '2',
...     'form-0-name': 'Walt Whitman',
...     'form-1-id': '1',
@@ -184,7 +180,6 @@ Test the behavior of commit=False and save_m2m
>>> data = {
...     'form-TOTAL_FORMS': '2', # the number of forms rendered
...     'form-INITIAL_FORMS': '1', # the number of forms with initial data
...     'form-MAX_FORMS': '0', # the max number of forms
...     'form-0-id': '1',
...     'form-0-name': '2nd Tuesday of the Week Meeting',
...     'form-0-authors': [2, 1, 3, 4],
@@ -242,7 +237,6 @@ admin system's edit inline functionality works.
>>> data = {
...     'book_set-TOTAL_FORMS': '3', # the number of forms rendered
...     'book_set-INITIAL_FORMS': '0', # the number of forms with initial data
...     'book_set-MAX_FORMS': '0', # the max number of forms
...     'book_set-0-title': 'Les Fleurs du Mal',
...     'book_set-1-title': '',
...     'book_set-2-title': '',
@@ -277,7 +271,6 @@ book.
>>> data = {
...     'book_set-TOTAL_FORMS': '3', # the number of forms rendered
...     'book_set-INITIAL_FORMS': '1', # the number of forms with initial data
...     'book_set-MAX_FORMS': '0', # the max number of forms
...     'book_set-0-id': '1',
...     'book_set-0-title': 'Les Fleurs du Mal',
...     'book_set-1-title': 'Le Spleen de Paris',
@@ -304,7 +297,6 @@ This is used in the admin for save_as functionality.
>>> data = {
...     'book_set-TOTAL_FORMS': '3', # the number of forms rendered
...     'book_set-INITIAL_FORMS': '2', # the number of forms with initial data
...     'book_set-MAX_FORMS': '0', # the max number of forms
...     'book_set-0-id': '1',
...     'book_set-0-title': 'Les Fleurs du Mal',
...     'book_set-1-id': '2',
+1 −15
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ but we'll look at how to do so later.

>>> formset = ChoiceFormSet(auto_id=False, prefix='choices')
>>> print formset
<input type="hidden" name="choices-TOTAL_FORMS" value="1" /><input type="hidden" name="choices-INITIAL_FORMS" value="0" /><input type="hidden" name="choices-MAX_FORMS" value="0" />
<input type="hidden" name="choices-TOTAL_FORMS" value="1" /><input type="hidden" name="choices-INITIAL_FORMS" value="0" />
<tr><th>Choice:</th><td><input type="text" name="choices-0-choice" /></td></tr>
<tr><th>Votes:</th><td><input type="text" name="choices-0-votes" /></td></tr>

@@ -34,7 +34,6 @@ the TOTAL_FORMS field appropriately.
>>> data = {
...     'choices-TOTAL_FORMS': '1', # the number of forms rendered
...     'choices-INITIAL_FORMS': '0', # the number of forms with initial data
...     'choices-MAX_FORMS': '0', # the max number of forms
...     'choices-0-choice': 'Calexico',
...     'choices-0-votes': '100',
... }
@@ -61,7 +60,6 @@ any of the forms.
>>> data = {
...     'choices-TOTAL_FORMS': '1', # the number of forms rendered
...     'choices-INITIAL_FORMS': '0', # the number of forms with initial data
...     'choices-MAX_FORMS': '0', # the max number of forms
...     'choices-0-choice': 'Calexico',
...     'choices-0-votes': '',
... }
@@ -92,7 +90,6 @@ Let's simulate what would happen if we submitted this form.
>>> data = {
...     'choices-TOTAL_FORMS': '2', # the number of forms rendered
...     'choices-INITIAL_FORMS': '1', # the number of forms with initial data
...     'choices-MAX_FORMS': '0', # the max number of forms
...     'choices-0-choice': 'Calexico',
...     'choices-0-votes': '100',
...     'choices-1-choice': '',
@@ -114,7 +111,6 @@ handle that later.
>>> data = {
...     'choices-TOTAL_FORMS': '2', # the number of forms rendered
...     'choices-INITIAL_FORMS': '1', # the number of forms with initial data
...     'choices-MAX_FORMS': '0', # the max number of forms
...     'choices-0-choice': 'Calexico',
...     'choices-0-votes': '100',
...     'choices-1-choice': 'The Decemberists',
@@ -134,7 +130,6 @@ handle that case later.
>>> data = {
...     'choices-TOTAL_FORMS': '2', # the number of forms rendered
...     'choices-INITIAL_FORMS': '1', # the number of forms with initial data
...     'choices-MAX_FORMS': '0', # the max number of forms
...     'choices-0-choice': '', # deleted value
...     'choices-0-votes': '', # deleted value
...     'choices-1-choice': '',
@@ -172,7 +167,6 @@ number of forms to be completed.
>>> data = {
...     'choices-TOTAL_FORMS': '3', # the number of forms rendered
...     'choices-INITIAL_FORMS': '0', # the number of forms with initial data
...     'choices-MAX_FORMS': '0', # the max number of forms
...     'choices-0-choice': '',
...     'choices-0-votes': '',
...     'choices-1-choice': '',
@@ -193,7 +187,6 @@ We can just fill out one of the forms.
>>> data = {
...     'choices-TOTAL_FORMS': '3', # the number of forms rendered
...     'choices-INITIAL_FORMS': '0', # the number of forms with initial data
...     'choices-MAX_FORMS': '0', # the max number of forms
...     'choices-0-choice': 'Calexico',
...     'choices-0-votes': '100',
...     'choices-1-choice': '',
@@ -214,7 +207,6 @@ And once again, if we try to partially complete a form, validation will fail.
>>> data = {
...     'choices-TOTAL_FORMS': '3', # the number of forms rendered
...     'choices-INITIAL_FORMS': '0', # the number of forms with initial data
...     'choices-MAX_FORMS': '0', # the max number of forms
...     'choices-0-choice': 'Calexico',
...     'choices-0-votes': '100',
...     'choices-1-choice': 'The Decemberists',
@@ -275,7 +267,6 @@ To delete something, we just need to set that form's special delete field to
>>> data = {
...     'choices-TOTAL_FORMS': '3', # the number of forms rendered
...     'choices-INITIAL_FORMS': '2', # the number of forms with initial data
...     'choices-MAX_FORMS': '0', # the max number of forms
...     'choices-0-choice': 'Calexico',
...     'choices-0-votes': '100',
...     'choices-0-DELETE': '',
@@ -325,7 +316,6 @@ something at the front of the list, you'd need to set it's order to 0.
>>> data = {
...     'choices-TOTAL_FORMS': '3', # the number of forms rendered
...     'choices-INITIAL_FORMS': '2', # the number of forms with initial data
...     'choices-MAX_FORMS': '0', # the max number of forms
...     'choices-0-choice': 'Calexico',
...     'choices-0-votes': '100',
...     'choices-0-ORDER': '1',
@@ -352,7 +342,6 @@ they will be sorted below everything else.
>>> data = {
...     'choices-TOTAL_FORMS': '4', # the number of forms rendered
...     'choices-INITIAL_FORMS': '3', # the number of forms with initial data
...     'choices-MAX_FORMS': '0', # the max number of forms
...     'choices-0-choice': 'Calexico',
...     'choices-0-votes': '100',
...     'choices-0-ORDER': '1',
@@ -414,7 +403,6 @@ Let's delete Fergie, and put The Decemberists ahead of Calexico.
>>> data = {
...     'choices-TOTAL_FORMS': '4', # the number of forms rendered
...     'choices-INITIAL_FORMS': '3', # the number of forms with initial data
...     'choices-MAX_FORMS': '0', # the max number of forms
...     'choices-0-choice': 'Calexico',
...     'choices-0-votes': '100',
...     'choices-0-ORDER': '1',
@@ -473,7 +461,6 @@ We start out with a some duplicate data.
>>> data = {
...     'drinks-TOTAL_FORMS': '2', # the number of forms rendered
...     'drinks-INITIAL_FORMS': '0', # the number of forms with initial data
...     'drinks-MAX_FORMS': '0', # the max number of forms
...     'drinks-0-name': 'Gin and Tonic',
...     'drinks-1-name': 'Gin and Tonic',
... }
@@ -495,7 +482,6 @@ Make sure we didn't break the valid case.
>>> data = {
...     'drinks-TOTAL_FORMS': '2', # the number of forms rendered
...     'drinks-INITIAL_FORMS': '0', # the number of forms with initial data
...     'drinks-MAX_FORMS': '0', # the max number of forms
...     'drinks-0-name': 'Gin and Tonic',
...     'drinks-1-name': 'Bloody Mary',
... }