Django

Code

Ticket #6302: pass_initial_through_with_tests.diff

File pass_initial_through_with_tests.diff, 5.7 kB (added by Øyvind Saltvik <oyvind@saltvik.no>, 8 months ago)

Same patch with tests

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

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

    old new  
    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

    old new  
    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

    old new  
    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 TextInput, Textarea 
    1414from util import flatatt, ErrorDict, ErrorList, ValidationError 
    1515 
     
    181181            # Each widget type knows how to retrieve its own data, because some 
    182182            # widgets split data over several HTML fields. 
    183183            value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name)) 
     184            if value in EMPTY_VALUES: 
     185                value = field.widget.value_from_initial(self.initial, name, value) 
    184186            try: 
    185187                value = field.clean(value) 
    186188                self.cleaned_data[name] = value 
  • django/newforms/widgets.py

    old new  
    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 
    1919 
    2020__all__ = ( 
    2121    'Widget', 'TextInput', 'PasswordInput', 
     
    7777        return id_ 
    7878    id_for_label = classmethod(id_for_label) 
    7979 
     80    def value_from_initial(self, initial, name, data): 
     81        return data 
     82 
    8083class Input(Widget): 
    8184    """ 
    8285    Base class for all <input> widgets (except type='checkbox' and 
     
    143146        "File widgets take data from FILES, not POST" 
    144147        return files.get(name, None) 
    145148 
     149    def value_from_initial(self, initial, name, data): 
     150        if initial.has_key(name): 
     151            return InitialData(initial.get(name)) 
     152        else: 
     153            return data 
     154 
    146155class Textarea(Widget): 
    147156    def __init__(self, attrs=None): 
    148157        # The 'rows' and 'cols' attributes are required for HTML correctness. 
  • tests/modeltests/model_forms/models.py

    old new  
    5555    def __unicode__(self): 
    5656        return self.phone 
    5757 
     58class TextFile(models.Model): 
     59    file = models.FileField(upload_to='textfiles') 
     60    description = models.CharField(max_length=20) 
     61 
     62    def __unicode__(self): 
     63        return self.description 
     64 
    5865__test__ = {'API_TESTS': """ 
    5966>>> from django import newforms as forms 
    6067>>> from django.newforms.models import ModelForm 
     
    701708True 
    702709>>> f.cleaned_data 
    703710{'phone': u'312-555-1212', 'description': u'Assistance'} 
     711 
     712>>> class TextFileForm(ModelForm): 
     713...     class Meta: 
     714...         model = TextFile 
     715>>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': {'filename':'test.txt', 'content': 'testtext'}}) 
     716>>> f.is_valid() 
     717True 
     718>>> f.cleaned_data['file'].__class__.__name__ 
     719'UploadedFile' 
     720>>> instance = f.save() 
     721>>> print instance.file 
     722textfiles/test.txt 
     723>>> f = TextFileForm(data={'description': u'Assistance'}, instance=instance) 
     724>>> f.is_valid() 
     725True 
     726>>> instance = f.save() 
     727>>> print instance.file 
     728textfiles/test.txt 
     729>>> instance.delete() 
    704730"""}