Ticket #6302: pass_initial_through_messy_example.diff

File pass_initial_through_messy_example.diff, 4.4 KB (added by oyvind@…, 13 years ago)

Patch against newforms-admin, don't like all parts of it

  • django/db/models/fields/__init__.py

     
    1313from django.core import validators
    1414from django import oldforms
    1515from django import newforms as forms
     16from django.newforms.utils import InitialData
    1617from django.core.exceptions import ObjectDoesNotExist
    1718from django.utils.functional import curry
    1819from django.utils.itercompat import tee
     
    795796        return os.path.normpath(f)
    796797
    797798    def save_form_data(self, instance, data):
    798         if data:
     799        if data and not isinstance(data, InitialData):
    799800            getattr(instance, "save_%s_file" % self.name)(data.filename, data.content, save=False)
    800801
    801802    def formfield(self, **kwargs):
  • django/newforms/util.py

     
    6767        # AttributeError: ValidationError instance has no attribute 'args'
    6868        # See http://www.python.org/doc/current/tut/node10.html#handling
    6969        return repr(self.messages)
     70
     71class InitialData(object):
     72
     73    value = None
     74
     75    def __init__(self, value):
     76        self.value = value
  • django/newforms/fields.py

     
    1919from django.utils.translation import ugettext_lazy as _
    2020from django.utils.encoding import StrAndUnicode, smart_unicode, smart_str
    2121
    22 from util import ErrorList, ValidationError
     22from util import ErrorList, ValidationError, InitialData
    2323from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple, DateTimeInput
    2424
    2525
     
    441441        super(FileField, self).clean(data)
    442442        if not self.required and data in EMPTY_VALUES:
    443443            return None
     444        elif self.required and isinstance(data, InitialData):
     445            return data
    444446        try:
    445447            f = UploadedFile(data['filename'], data['content'])
    446448        except TypeError:
  • django/newforms/forms.py

     
    99from django.utils.encoding import StrAndUnicode, smart_unicode, force_unicode
    1010from django.utils.safestring import mark_safe
    1111
    12 from fields import Field
     12from fields import Field, EMPTY_VALUES
    1313from widgets import Media, media_property, TextInput, Textarea
    1414from util import flatatt, ErrorDict, ErrorList, ValidationError
    1515
     
    204204            # Each widget type knows how to retrieve its own data, because some
    205205            # widgets split data over several HTML fields.
    206206            value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name))
     207            if value in EMPTY_VALUES:
     208                value = field.widget.value_from_initial(self.initial, name, value)
    207209            try:
    208210                value = field.clean(value)
    209211                self.cleaned_data[name] = value
  • django/newforms/widgets.py

     
    1515from django.utils.translation import ugettext
    1616from django.utils.encoding import StrAndUnicode, force_unicode
    1717from django.utils.safestring import mark_safe
    18 from util import flatatt
     18from util import flatatt, InitialData
    1919from urlparse import urljoin
    2020
    2121__all__ = (
     
    202202            final_attrs['value'] = force_unicode(value)
    203203        return mark_safe(u'<input%s />' % flatatt(final_attrs))
    204204
     205    def value_from_initial(self, initial, name, data):
     206        return data
     207
    205208class TextInput(Input):
    206209    input_type = 'text'
    207210
     
    253256        "File widgets take data from FILES, not POST"
    254257        return files.get(name, None)
    255258
     259    def value_from_initial(self, initial, name, data):
     260        if initial.has_key(name):
     261            return InitialData(initial.get(name))
     262        else:
     263            return data
     264
    256265class Textarea(Widget):
    257266    def __init__(self, attrs=None):
    258267        # The 'rows' and 'cols' attributes are required for HTML correctness.
Back to Top