From ffcf24c9ce781a7c194ed8722b850e7873922f6b Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Wed, 19 Jun 2013 17:18:40 +0200 Subject: Removed several unused imports. --- django/forms/formsets.py | 2 +- django/forms/util.py | 1 - django/forms/widgets.py | 5 ++--- 3 files changed, 3 insertions(+), 5 deletions(-) (limited to 'django/forms') diff --git a/django/forms/formsets.py b/django/forms/formsets.py index edd362c595..9c2e88bee7 100644 --- a/django/forms/formsets.py +++ b/django/forms/formsets.py @@ -4,7 +4,7 @@ from django.core.exceptions import ValidationError from django.forms import Form from django.forms.fields import IntegerField, BooleanField from django.forms.util import ErrorList -from django.forms.widgets import Media, HiddenInput +from django.forms.widgets import HiddenInput from django.utils.encoding import python_2_unicode_compatible from django.utils.safestring import mark_safe from django.utils import six diff --git a/django/forms/util.py b/django/forms/util.py index 568cdd1086..0a73320f83 100644 --- a/django/forms/util.py +++ b/django/forms/util.py @@ -3,7 +3,6 @@ from __future__ import unicode_literals from django.conf import settings from django.utils.html import format_html, format_html_join from django.utils.encoding import force_text, python_2_unicode_compatible -from django.utils.safestring import mark_safe from django.utils import timezone from django.utils.translation import ugettext_lazy as _ from django.utils import six diff --git a/django/forms/widgets.py b/django/forms/widgets.py index aca4a457af..38d1b99b0d 100644 --- a/django/forms/widgets.py +++ b/django/forms/widgets.py @@ -5,7 +5,6 @@ HTML Widget classes from __future__ import absolute_import, unicode_literals import copy -import datetime from itertools import chain try: from urllib.parse import urljoin @@ -16,8 +15,8 @@ import warnings from django.conf import settings from django.forms.util import flatatt, to_current_timezone from django.utils.datastructures import MultiValueDict, MergeDict -from django.utils.html import conditional_escape, format_html, format_html_join -from django.utils.translation import ugettext, ugettext_lazy +from django.utils.html import conditional_escape, format_html +from django.utils.translation import ugettext_lazy from django.utils.encoding import force_text, python_2_unicode_compatible from django.utils.safestring import mark_safe from django.utils import datetime_safe, formats, six -- cgit v1.3 From 04628e2016641bfa657333d6ee1f5b371c17f62c Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Wed, 19 Jun 2013 23:42:23 -0400 Subject: Fixed #20630 -- Removed `maxlength` attribute from `NumberInput`. This attribute is only allowed on inputs of type "text", "search", "url", "tel", "email", or "password". Thanks to yoyoma for the report and @bmispelon for the review. --- django/forms/fields.py | 10 ++-------- tests/forms_tests/tests/test_fields.py | 4 ++-- tests/model_formsets/tests.py | 2 +- 3 files changed, 5 insertions(+), 11 deletions(-) (limited to 'django/forms') diff --git a/django/forms/fields.py b/django/forms/fields.py index 52bcf9485c..c4bc3fa88c 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -370,14 +370,8 @@ class DecimalField(IntegerField): def widget_attrs(self, widget): attrs = super(DecimalField, self).widget_attrs(widget) - if isinstance(widget, NumberInput): - if self.max_digits is not None: - max_length = self.max_digits + 1 # for the sign - if self.decimal_places is None or self.decimal_places > 0: - max_length += 1 # for the dot - attrs['maxlength'] = max_length - if self.decimal_places: - attrs['step'] = '0.%s1' % ('0' * (self.decimal_places-1)) + if isinstance(widget, NumberInput) and self.decimal_places: + attrs['step'] = '0.%s1' % ('0' * (self.decimal_places - 1)) return attrs diff --git a/tests/forms_tests/tests/test_fields.py b/tests/forms_tests/tests/test_fields.py index 47c637befa..f7d83503f4 100644 --- a/tests/forms_tests/tests/test_fields.py +++ b/tests/forms_tests/tests/test_fields.py @@ -296,7 +296,7 @@ class FieldsTests(SimpleTestCase): def test_decimalfield_1(self): f = DecimalField(max_digits=4, decimal_places=2) - self.assertWidgetRendersTo(f, '') + self.assertWidgetRendersTo(f, '') self.assertRaisesMessage(ValidationError, "'This field is required.'", f.clean, '') self.assertRaisesMessage(ValidationError, "'This field is required.'", f.clean, None) self.assertEqual(f.clean('1'), Decimal("1")) @@ -342,7 +342,7 @@ class FieldsTests(SimpleTestCase): def test_decimalfield_3(self): f = DecimalField(max_digits=4, decimal_places=2, max_value=Decimal('1.5'), min_value=Decimal('0.5')) - self.assertWidgetRendersTo(f, '') + self.assertWidgetRendersTo(f, '') self.assertRaisesMessage(ValidationError, "'Ensure this value is less than or equal to 1.5.'", f.clean, '1.6') self.assertRaisesMessage(ValidationError, "'Ensure this value is greater than or equal to 0.5.'", f.clean, '0.4') self.assertEqual(f.clean('1.5'), Decimal("1.5")) diff --git a/tests/model_formsets/tests.py b/tests/model_formsets/tests.py index 03cd3b0159..62870c7462 100644 --- a/tests/model_formsets/tests.py +++ b/tests/model_formsets/tests.py @@ -559,7 +559,7 @@ class ModelFormsetTest(TestCase): formset = AuthorBooksFormSet2(instance=author) self.assertEqual(len(formset.forms), 1) self.assertHTMLEqual(formset.forms[0].as_p(), - '

\n' + '

\n' '

') data = { -- cgit v1.3 From ef79582e8630cb3c119caed52130c9671188addd Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Sat, 22 Jun 2013 09:25:14 +0200 Subject: Fixed 17478 -- Allowed queryset overriding in BaseModelFormSet init BaseModelFormSet.forms is now a cached property instead of being populated in the __init__ method. This behaviour also matches an example in the documentation. Thanks Thomasz Swiderski for the report and Simon Charette for the review. --- django/forms/formsets.py | 15 ++++++++------- tests/forms_tests/tests/test_formsets.py | 3 ++- tests/model_formsets/tests.py | 19 ++++++++++++++++++- 3 files changed, 28 insertions(+), 9 deletions(-) (limited to 'django/forms') diff --git a/django/forms/formsets.py b/django/forms/formsets.py index 9c2e88bee7..cb3126e6d7 100644 --- a/django/forms/formsets.py +++ b/django/forms/formsets.py @@ -6,6 +6,7 @@ from django.forms.fields import IntegerField, BooleanField from django.forms.util import ErrorList from django.forms.widgets import HiddenInput from django.utils.encoding import python_2_unicode_compatible +from django.utils.functional import cached_property from django.utils.safestring import mark_safe from django.utils import six from django.utils.six.moves import xrange @@ -55,8 +56,6 @@ class BaseFormSet(object): self.error_class = error_class self._errors = None self._non_form_errors = None - # construct the forms in the formset - self._construct_forms() def __str__(self): return self.as_table() @@ -125,12 +124,14 @@ class BaseFormSet(object): initial_forms = len(self.initial) if self.initial else 0 return initial_forms - def _construct_forms(self): - # instantiate all the forms and put them in self.forms - self.forms = [] + @cached_property + def forms(self): + """ + Instantiate forms at first property access. + """ # DoS protection is included in total_form_count() - for i in xrange(self.total_form_count()): - self.forms.append(self._construct_form(i)) + forms = [self._construct_form(i) for i in xrange(self.total_form_count())] + return forms def _construct_form(self, i, **kwargs): """ diff --git a/tests/forms_tests/tests/test_formsets.py b/tests/forms_tests/tests/test_formsets.py index b26017bc78..41577e6049 100644 --- a/tests/forms_tests/tests/test_formsets.py +++ b/tests/forms_tests/tests/test_formsets.py @@ -1072,7 +1072,8 @@ ArticleFormSet = formset_factory(ArticleForm) class TestIsBoundBehavior(TestCase): def test_no_data_raises_validation_error(self): - self.assertRaises(ValidationError, ArticleFormSet, {}) + with self.assertRaises(ValidationError): + ArticleFormSet({}).is_valid() def test_with_management_data_attrs_work_fine(self): data = { diff --git a/tests/model_formsets/tests.py b/tests/model_formsets/tests.py index 62870c7462..43509c471f 100644 --- a/tests/model_formsets/tests.py +++ b/tests/model_formsets/tests.py @@ -8,7 +8,7 @@ from decimal import Decimal from django import forms from django.db import models from django.forms.models import (_get_foreign_key, inlineformset_factory, - modelformset_factory) + modelformset_factory, BaseModelFormSet) from django.test import TestCase, skipUnlessDBFeature from django.utils import six @@ -386,6 +386,23 @@ class ModelFormsetTest(TestCase): formset = PostFormSet() self.assertFalse("subtitle" in formset.forms[0].fields) + def test_custom_queryset_init(self): + """ + Test that a queryset can be overriden in the __init__ method. + https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#changing-the-queryset + """ + author1 = Author.objects.create(name='Charles Baudelaire') + author2 = Author.objects.create(name='Paul Verlaine') + + class BaseAuthorFormSet(BaseModelFormSet): + def __init__(self, *args, **kwargs): + super(BaseAuthorFormSet, self).__init__(*args, **kwargs) + self.queryset = Author.objects.filter(name__startswith='Charles') + + AuthorFormSet = modelformset_factory(Author, formset=BaseAuthorFormSet) + formset = AuthorFormSet() + self.assertEqual(len(formset.get_queryset()), 1) + def test_model_inheritance(self): BetterAuthorFormSet = modelformset_factory(BetterAuthor, fields="__all__") formset = BetterAuthorFormSet() -- cgit v1.3