Django

Code

Changeset 6837

Show
Ignore:
Timestamp:
12/02/07 12:14:11 (9 months ago)
Author:
jkocherhans
Message:

newforms-admin: Fixed #5828. You can leave inline add forms empty again. Thanks brosner.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/newforms-admin/django/newforms/forms.py

    r6776 r6837  
    183183            if name in exceptions: 
    184184                continue 
    185             # value_from_datadict() gets the data from the dictionary
     185            # value_from_datadict() gets the data from the data dictionaries
    186186            # Each widget type knows how to retrieve its own data, because some 
    187187            # widgets split data over several HTML fields. 
    188188            value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name)) 
    189             # HACK: ['', ''] and [None, None] deal with SplitDateTimeWidget. This should be more robust. 
    190             if value not in (None, '', ['', ''], [None, None]): 
     189            if not field.widget.is_empty(value): 
    191190                return False 
    192191        return True 
  • django/branches/newforms-admin/django/newforms/widgets.py

    r6777 r6837  
    166166        """ 
    167167        return data.get(name, None) 
     168     
     169    def is_empty(self, value): 
     170        """ 
     171        Given a dictionary of data and this widget's name, return True if the 
     172        widget data is empty or False when not empty. 
     173        """ 
     174        if value not in (None, ''): 
     175            return False 
     176        return True 
    168177 
    169178    def id_for_label(self, id_): 
     
    302311            return False 
    303312        return super(CheckboxInput, self).value_from_datadict(data, files, name) 
     313     
     314    def is_empty(self, value): 
     315        # this widget will always either be True or False, so always return the 
     316        # opposite value so False values will make the form empty 
     317        return not value 
    304318 
    305319class Select(Widget): 
     
    344358        value = data.get(name, None) 
    345359        return {u'2': True, u'3': False, True: True, False: False}.get(value, None) 
     360     
     361    def is_empty(self, value): 
     362        # this widget will always either be True, False or None, so always 
     363        # return the opposite value so False and None values will make the 
     364        # form empty. 
     365        return not value 
    346366 
    347367class SelectMultiple(Widget): 
     
    540560    def value_from_datadict(self, data, files, name): 
    541561        return [widget.value_from_datadict(data, files, name + '_%s' % i) for i, widget in enumerate(self.widgets)] 
     562     
     563    def is_empty(self, value): 
     564        for widget, val in zip(self.widgets, value): 
     565            if not widget.is_empty(val): 
     566                return False 
     567        return True 
    542568 
    543569    def format_output(self, rendered_widgets): 
  • django/branches/newforms-admin/tests/regressiontests/forms/widgets.py

    r6777 r6837  
    293293False 
    294294 
     295The CheckboxInput widget will always be empty when there is a False value 
     296>>> w.is_empty(False) 
     297True 
     298>>> w.is_empty(True) 
     299False 
     300 
    295301# Select Widget ############################################################### 
    296302 
     
    453459<option value="3" selected="selected">No</option> 
    454460</select> 
     461 
     462The NullBooleanSelect widget will always be empty when Unknown or No is selected 
     463as its value.  This is to stay compliant with the CheckboxInput behavior 
     464>>> w.is_empty(False) 
     465True 
     466>>> w.is_empty(None) 
     467True 
     468>>> w.is_empty(True) 
     469False 
    455470 
    456471""" + \ 
     
    896911u'<input id="bar_0" type="text" class="big" value="john" name="name_0" /><br /><input id="bar_1" type="text" class="small" value="lennon" name="name_1" />' 
    897912 
     913The MultiWidget will be empty only when all widgets are considered empty. 
     914>>> w.is_empty(['john', 'lennon']) 
     915False 
     916>>> w.is_empty(['john', '']) 
     917False 
     918>>> w.is_empty(['', '']) 
     919True 
     920>>> w.is_empty([None, None]) 
     921True 
     922 
    898923# SplitDateTimeWidget ######################################################### 
    899924