From 4d52c80dd1b4c6aef9407b268ae720eeffd4d962 Mon Sep 17 00:00:00 2001 From: Joseph Kocherhans Date: Fri, 30 Nov 2007 04:32:25 +0000 Subject: newforms-admin: Merged from trunk up to [6670]. This is just before auto-escaping was checked in. git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@6761 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/newforms/forms.py | 18 +++--------------- django/newforms/models.py | 25 +++++++++++++++++-------- 2 files changed, 20 insertions(+), 23 deletions(-) (limited to 'django/newforms') diff --git a/django/newforms/forms.py b/django/newforms/forms.py index b502fb4de2..3e7ab957b0 100644 --- a/django/newforms/forms.py +++ b/django/newforms/forms.py @@ -2,7 +2,7 @@ Form classes """ -import copy +from copy import deepcopy from django.utils.datastructures import SortedDict from django.utils.html import escape @@ -21,18 +21,6 @@ def pretty_name(name): name = name[0].upper() + name[1:] return name.replace('_', ' ') -class SortedDictFromList(SortedDict): - "A dictionary that keeps its keys in the order in which they're inserted." - # This is different than django.utils.datastructures.SortedDict, because - # this takes a list/tuple as the argument to __init__(). - def __init__(self, data=None): - if data is None: data = [] - self.keyOrder = [d[0] for d in data] - dict.__init__(self, dict(data)) - - def copy(self): - return SortedDictFromList([(k, copy.deepcopy(v)) for k, v in self.items()]) - class DeclarativeFieldsMetaclass(type): """ Metaclass that converts Field attributes to a dictionary called @@ -50,7 +38,7 @@ class DeclarativeFieldsMetaclass(type): if hasattr(base, 'base_fields'): fields = base.base_fields.items() + fields - attrs['base_fields'] = SortedDictFromList(fields) + attrs['base_fields'] = SortedDict(fields) new_class = type.__new__(cls, name, bases, attrs) if 'media' not in attrs: @@ -79,7 +67,7 @@ class BaseForm(StrAndUnicode): # alter self.fields, we create self.fields here by copying base_fields. # Instances should always modify self.fields; they should not modify # self.base_fields. - self.fields = self.base_fields.copy() + self.fields = deepcopy(self.base_fields) def __unicode__(self): return self.as_table() diff --git a/django/newforms/models.py b/django/newforms/models.py index 3a80517816..82f794c02b 100644 --- a/django/newforms/models.py +++ b/django/newforms/models.py @@ -5,9 +5,10 @@ and database field objects. from django.utils.translation import ugettext from django.utils.encoding import smart_unicode +from django.utils.datastructures import SortedDict from util import ValidationError -from forms import BaseForm, SortedDictFromList +from forms import BaseForm from fields import Field, ChoiceField, IntegerField from formsets import BaseFormSet, formset_for_form, DELETION_FIELD_NAME from widgets import Select, SelectMultiple, HiddenInput, MultipleHiddenInput @@ -91,7 +92,7 @@ def form_for_model(model, form=BaseForm, fields=None, formfield = formfield_callback(f) if formfield: field_list.append((f.name, formfield)) - base_fields = SortedDictFromList(field_list) + base_fields = SortedDict(field_list) return type(opts.object_name + 'Form', (form,), {'base_fields': base_fields, '_model': model, 'save': make_model_save(model, fields, 'created')}) @@ -120,7 +121,7 @@ def form_for_instance(instance, form=BaseForm, fields=None, formfield = formfield_callback(f, initial=current_value) if formfield: field_list.append((f.name, formfield)) - base_fields = SortedDictFromList(field_list) + base_fields = SortedDict(field_list) return type(opts.object_name + 'InstanceForm', (form,), {'base_fields': base_fields, '_model': model, 'save': make_instance_save(instance, fields, 'changed')}) @@ -129,8 +130,8 @@ def form_for_fields(field_list): """ Returns a Form class for the given list of Django database field instances. """ - fields = SortedDictFromList([(f.name, f.formfield()) - for f in field_list if f.editable]) + fields = SortedDict([(f.name, f.formfield()) + for f in field_list if f.editable]) return type('FormForFields', (BaseForm,), {'base_fields': fields}) class QuerySetIterator(object): @@ -156,14 +157,22 @@ class ModelChoiceField(ChoiceField): def __init__(self, queryset, empty_label=u"---------", cache_choices=False, required=True, widget=Select, label=None, initial=None, help_text=None): - self.queryset = queryset 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) + self.queryset = queryset + + def _get_queryset(self): + return self._queryset + + def _set_queryset(self, queryset): + self._queryset = queryset self.widget.choices = self.choices + queryset = property(_get_queryset, _set_queryset) + def _get_choices(self): # If self._choices is set, then somebody must have manually set # the property self.choices. In this case, just return self._choices. @@ -191,7 +200,7 @@ class ModelChoiceField(ChoiceField): if value in ('', None): return None try: - value = self.queryset.model._default_manager.get(pk=value) + 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' @@ -218,7 +227,7 @@ class ModelMultipleChoiceField(ModelChoiceField): final_values = [] for val in value: try: - obj = self.queryset.model._default_manager.get(pk=val) + 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' -- cgit v1.3