mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	newforms-admin: Fixed #5828. You can leave inline add forms empty again. Thanks brosner.
git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@6837 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
		| @@ -182,12 +182,11 @@ class BaseForm(StrAndUnicode): | ||||
|         for name, field in self.fields.items(): | ||||
|             if name in exceptions: | ||||
|                 continue | ||||
|             # value_from_datadict() gets the data from the dictionary. | ||||
|             # value_from_datadict() gets the data from the data dictionaries. | ||||
|             # Each widget type knows how to retrieve its own data, because some | ||||
|             # widgets split data over several HTML fields. | ||||
|             value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name)) | ||||
|             # HACK: ['', ''] and [None, None] deal with SplitDateTimeWidget. This should be more robust. | ||||
|             if value not in (None, '', ['', ''], [None, None]): | ||||
|             if not field.widget.is_empty(value): | ||||
|                 return False | ||||
|         return True | ||||
|  | ||||
|   | ||||
| @@ -166,6 +166,15 @@ class Widget(object): | ||||
|         """ | ||||
|         return data.get(name, None) | ||||
|      | ||||
|     def is_empty(self, value): | ||||
|         """ | ||||
|         Given a dictionary of data and this widget's name, return True if the | ||||
|         widget data is empty or False when not empty. | ||||
|         """ | ||||
|         if value not in (None, ''): | ||||
|             return False | ||||
|         return True | ||||
|  | ||||
|     def id_for_label(self, id_): | ||||
|         """ | ||||
|         Returns the HTML ID attribute of this Widget for use by a <label>, | ||||
| @@ -302,6 +311,11 @@ class CheckboxInput(Widget): | ||||
|             return False | ||||
|         return super(CheckboxInput, self).value_from_datadict(data, files, name) | ||||
|      | ||||
|     def is_empty(self, value): | ||||
|         # this widget will always either be True or False, so always return the | ||||
|         # opposite value so False values will make the form empty | ||||
|         return not value | ||||
|  | ||||
| class Select(Widget): | ||||
|     def __init__(self, attrs=None, choices=()): | ||||
|         super(Select, self).__init__(attrs) | ||||
| @@ -344,6 +358,12 @@ class NullBooleanSelect(Select): | ||||
|         value = data.get(name, None) | ||||
|         return {u'2': True, u'3': False, True: True, False: False}.get(value, None) | ||||
|      | ||||
|     def is_empty(self, value): | ||||
|         # this widget will always either be True, False or None, so always | ||||
|         # return the opposite value so False and None values will make the | ||||
|         # form empty. | ||||
|         return not value | ||||
|  | ||||
| class SelectMultiple(Widget): | ||||
|     def __init__(self, attrs=None, choices=()): | ||||
|         super(SelectMultiple, self).__init__(attrs) | ||||
| @@ -540,6 +560,12 @@ class MultiWidget(Widget): | ||||
|     def value_from_datadict(self, data, files, name): | ||||
|         return [widget.value_from_datadict(data, files, name + '_%s' % i) for i, widget in enumerate(self.widgets)] | ||||
|      | ||||
|     def is_empty(self, value): | ||||
|         for widget, val in zip(self.widgets, value): | ||||
|             if not widget.is_empty(val): | ||||
|                 return False | ||||
|         return True | ||||
|  | ||||
|     def format_output(self, rendered_widgets): | ||||
|         """ | ||||
|         Given a list of rendered widgets (as strings), returns a Unicode string | ||||
|   | ||||
| @@ -292,6 +292,12 @@ checkboxes). | ||||
| >>> w.value_from_datadict({}, {}, 'testing') | ||||
| False | ||||
|  | ||||
| The CheckboxInput widget will always be empty when there is a False value | ||||
| >>> w.is_empty(False) | ||||
| True | ||||
| >>> w.is_empty(True) | ||||
| False | ||||
|  | ||||
| # Select Widget ############################################################### | ||||
|  | ||||
| >>> w = Select() | ||||
| @@ -453,6 +459,15 @@ over multiple times without getting consumed: | ||||
| <option value="3" selected="selected">No</option> | ||||
| </select> | ||||
|  | ||||
| The NullBooleanSelect widget will always be empty when Unknown or No is selected | ||||
| as its value.  This is to stay compliant with the CheckboxInput behavior | ||||
| >>> w.is_empty(False) | ||||
| True | ||||
| >>> w.is_empty(None) | ||||
| True | ||||
| >>> w.is_empty(True) | ||||
| False | ||||
|  | ||||
| """ + \ | ||||
| r""" # [This concatenation is to keep the string below the jython's 32K limit]. | ||||
| # SelectMultiple Widget ####################################################### | ||||
| @@ -895,6 +910,16 @@ u'<input id="foo_0" type="text" class="big" value="john" name="name_0" /><br />< | ||||
| >>> w.render('name', ['john', 'lennon']) | ||||
| u'<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" />' | ||||
|  | ||||
| The MultiWidget will be empty only when all widgets are considered empty. | ||||
| >>> w.is_empty(['john', 'lennon']) | ||||
| False | ||||
| >>> w.is_empty(['john', '']) | ||||
| False | ||||
| >>> w.is_empty(['', '']) | ||||
| True | ||||
| >>> w.is_empty([None, None]) | ||||
| True | ||||
|  | ||||
| # SplitDateTimeWidget ######################################################### | ||||
|  | ||||
| >>> w = SplitDateTimeWidget() | ||||
|   | ||||
		Reference in New Issue
	
	Block a user