diff options
Diffstat (limited to 'django/newforms')
| -rw-r--r-- | django/newforms/extras/widgets.py | 3 | ||||
| -rw-r--r-- | django/newforms/models.py | 37 | ||||
| -rw-r--r-- | django/newforms/widgets.py | 25 |
3 files changed, 40 insertions, 25 deletions
diff --git a/django/newforms/extras/widgets.py b/django/newforms/extras/widgets.py index 60936a6bd6..0097ba3f54 100644 --- a/django/newforms/extras/widgets.py +++ b/django/newforms/extras/widgets.py @@ -6,6 +6,7 @@ import datetime from django.newforms.widgets import Widget, Select from django.utils.dates import MONTHS +from django.utils.safestring import mark_safe __all__ = ('SelectDateWidget',) @@ -51,7 +52,7 @@ class SelectDateWidget(Widget): select_html = Select(choices=year_choices).render(self.year_field % name, year_val) output.append(select_html) - return u'\n'.join(output) + return mark_safe(u'\n'.join(output)) def value_from_datadict(self, data, files, name): y, m, d = data.get(self.year_field % name), data.get(self.month_field % name), data.get(self.day_field % name) diff --git a/django/newforms/models.py b/django/newforms/models.py index c86b9b3a42..51ed16ff7f 100644 --- a/django/newforms/models.py +++ b/django/newforms/models.py @@ -3,13 +3,13 @@ Helper functions for creating Form classes from Django models and database field objects. """ -from django.utils.translation import ugettext +from django.utils.translation import ugettext_lazy as _ from django.utils.encoding import smart_unicode from django.utils.datastructures import SortedDict from util import ValidationError from forms import BaseForm -from fields import Field, ChoiceField +from fields import Field, ChoiceField, EMPTY_VALUES from widgets import Select, SelectMultiple, MultipleHiddenInput __all__ = ( @@ -151,15 +151,20 @@ class ModelChoiceField(ChoiceField): """A ChoiceField whose choices are a model QuerySet.""" # This class is a subclass of ChoiceField for purity, but it doesn't # actually use any of ChoiceField's implementation. + default_error_messages = { + 'invalid_choice': _(u'Select a valid choice. That choice is not one of' + u' the available choices.'), + } def __init__(self, queryset, empty_label=u"---------", cache_choices=False, required=True, widget=Select, label=None, initial=None, - help_text=None): + help_text=None, *args, **kwargs): self.empty_label = empty_label self.cache_choices = cache_choices # Call Field instead of ChoiceField __init__() because we don't need # ChoiceField.__init__(). - Field.__init__(self, required, widget, label, initial, help_text) + Field.__init__(self, required, widget, label, initial, help_text, + *args, **kwargs) self.queryset = queryset def _get_queryset(self): @@ -195,41 +200,43 @@ class ModelChoiceField(ChoiceField): def clean(self, value): Field.clean(self, value) - if value in ('', None): + if value in EMPTY_VALUES: return None try: value = self.queryset.get(pk=value) except self.queryset.model.DoesNotExist: - raise ValidationError(ugettext(u'Select a valid choice. That' - u' choice is not one of the' - u' available choices.')) + raise ValidationError(self.error_messages['invalid_choice']) return value class ModelMultipleChoiceField(ModelChoiceField): """A MultipleChoiceField whose choices are a model QuerySet.""" hidden_widget = MultipleHiddenInput + default_error_messages = { + 'list': _(u'Enter a list of values.'), + 'invalid_choice': _(u'Select a valid choice. %s is not one of the' + u' available choices.'), + } def __init__(self, queryset, cache_choices=False, required=True, widget=SelectMultiple, label=None, initial=None, - help_text=None): + help_text=None, *args, **kwargs): super(ModelMultipleChoiceField, self).__init__(queryset, None, - cache_choices, required, widget, label, initial, help_text) + cache_choices, required, widget, label, initial, help_text, + *args, **kwargs) def clean(self, value): if self.required and not value: - raise ValidationError(ugettext(u'This field is required.')) + raise ValidationError(self.error_messages['required']) elif not self.required and not value: return [] if not isinstance(value, (list, tuple)): - raise ValidationError(ugettext(u'Enter a list of values.')) + raise ValidationError(self.error_messages['list']) final_values = [] for val in value: try: obj = self.queryset.get(pk=val) except self.queryset.model.DoesNotExist: - raise ValidationError(ugettext(u'Select a valid choice. %s is' - u' not one of the available' - u' choices.') % val) + raise ValidationError(self.error_messages['invalid_choice'] % val) else: final_values.append(obj) return final_values diff --git a/django/newforms/widgets.py b/django/newforms/widgets.py index 350b878af9..580834857e 100644 --- a/django/newforms/widgets.py +++ b/django/newforms/widgets.py @@ -11,7 +11,7 @@ import copy from itertools import chain from django.utils.datastructures import MultiValueDict -from django.utils.html import escape +from django.utils.html import escape, conditional_escape from django.utils.translation import ugettext from django.utils.encoding import StrAndUnicode, force_unicode from django.utils.safestring import mark_safe @@ -155,7 +155,7 @@ class Textarea(Widget): value = force_unicode(value) final_attrs = self.build_attrs(attrs, name=name) return mark_safe(u'<textarea%s>%s</textarea>' % (flatatt(final_attrs), - escape(value))) + conditional_escape(force_unicode(value)))) class DateTimeInput(Input): input_type = 'text' @@ -217,7 +217,9 @@ class Select(Widget): for option_value, option_label in chain(self.choices, choices): option_value = force_unicode(option_value) selected_html = (option_value == str_value) and u' selected="selected"' or '' - output.append(u'<option value="%s"%s>%s</option>' % (escape(option_value), selected_html, escape(force_unicode(option_label)))) + output.append(u'<option value="%s"%s>%s</option>' % ( + escape(option_value), selected_html, + conditional_escape(force_unicode(option_label)))) output.append(u'</select>') return mark_safe(u'\n'.join(output)) @@ -254,7 +256,9 @@ class SelectMultiple(Widget): for option_value, option_label in chain(self.choices, choices): option_value = force_unicode(option_value) selected_html = (option_value in str_values) and ' selected="selected"' or '' - output.append(u'<option value="%s"%s>%s</option>' % (escape(option_value), selected_html, escape(force_unicode(option_label)))) + output.append(u'<option value="%s"%s>%s</option>' % ( + escape(option_value), selected_html, + conditional_escape(force_unicode(option_label)))) output.append(u'</select>') return mark_safe(u'\n'.join(output)) @@ -278,7 +282,7 @@ class RadioInput(StrAndUnicode): def __unicode__(self): return mark_safe(u'<label>%s %s</label>' % (self.tag(), - self.choice_label)) + conditional_escape(force_unicode(self.choice_label)))) def is_checked(self): return self.value == self.choice_value @@ -317,11 +321,13 @@ class RadioFieldRenderer(StrAndUnicode): % force_unicode(w) for w in self])) class RadioSelect(Select): + renderer = RadioFieldRenderer def __init__(self, *args, **kwargs): - self.renderer = kwargs.pop('renderer', None) - if not self.renderer: - self.renderer = RadioFieldRenderer + # Override the default renderer if we were passed one. + renderer = kwargs.pop('renderer', None) + if renderer: + self.renderer = renderer super(RadioSelect, self).__init__(*args, **kwargs) def get_renderer(self, name, value, attrs=None, choices=()): @@ -361,7 +367,8 @@ class CheckboxSelectMultiple(SelectMultiple): cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_unicode(option_value) rendered_cb = cb.render(name, option_value) - output.append(u'<li><label>%s %s</label></li>' % (rendered_cb, escape(force_unicode(option_label)))) + output.append(u'<li><label>%s %s</label></li>' % (rendered_cb, + conditional_escape(force_unicode(option_label)))) output.append(u'</ul>') return mark_safe(u'\n'.join(output)) |
