diff options
| author | Brian Rosner <brosner@gmail.com> | 2008-04-29 05:45:46 +0000 |
|---|---|---|
| committer | Brian Rosner <brosner@gmail.com> | 2008-04-29 05:45:46 +0000 |
| commit | abb7a7ff0fa9440d833434a52328f56ab0c425e3 (patch) | |
| tree | 9a796eee544772de87d15f645c2573e7a18c78b2 /django/newforms | |
| parent | 95dcdf473ed08f03040334d716c4e60c2a41e57c (diff) | |
newforms-admin: Fixed #6117 -- Implemented change history for the admin. This includes the ability to track changes on a newform. Model formsets now only return the changed/new objects saved. A big thanks to Karen Tracey and Alex Gaynor.
git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@7507 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/newforms')
| -rw-r--r-- | django/newforms/forms.py | 33 | ||||
| -rw-r--r-- | django/newforms/models.py | 17 |
2 files changed, 31 insertions, 19 deletions
diff --git a/django/newforms/forms.py b/django/newforms/forms.py index 42dacec6ce..300afb2a5b 100644 --- a/django/newforms/forms.py +++ b/django/newforms/forms.py @@ -81,6 +81,7 @@ class BaseForm(StrAndUnicode): self.label_suffix = label_suffix self.empty_permitted = empty_permitted self._errors = None # Stores the errors after clean() has been called. + self._changed_data = None # The base_fields class attribute is the *class-wide* definition of # fields. Because a particular *instance* of the class might want to @@ -243,19 +244,25 @@ class BaseForm(StrAndUnicode): """ Returns True if data differs from initial. """ - # XXX: For now we're asking the individual widgets whether or not the - # data has changed. It would probably be more efficient to hash the - # initial data, store it in a hidden field, and compare a hash of the - # submitted data, but we'd need a way to easily get the string value - # for a given field. Right now, that logic is embedded in the render - # method of each widget. - for name, field in self.fields.items(): - prefixed_name = self.add_prefix(name) - data_value = field.widget.value_from_datadict(self.data, self.files, prefixed_name) - initial_value = self.initial.get(name, field.initial) - if field.widget._has_changed(initial_value, data_value): - return True - return False + return bool(self.changed_data) + + def _get_changed_data(self): + if self._changed_data is None: + self._changed_data = [] + # XXX: For now we're asking the individual widgets whether or not the + # data has changed. It would probably be more efficient to hash the + # initial data, store it in a hidden field, and compare a hash of the + # submitted data, but we'd need a way to easily get the string value + # for a given field. Right now, that logic is embedded in the render + # method of each widget. + for name, field in self.fields.items(): + prefixed_name = self.add_prefix(name) + data_value = field.widget.value_from_datadict(self.data, self.files, prefixed_name) + initial_value = self.initial.get(name, field.initial) + if field.widget._has_changed(initial_value, data_value): + self._changed_data.append(name) + return self._changed_data + changed_data = property(_get_changed_data) def _get_media(self): """ diff --git a/django/newforms/models.py b/django/newforms/models.py index f12b53dacb..79a9e1a457 100644 --- a/django/newforms/models.py +++ b/django/newforms/models.py @@ -328,8 +328,11 @@ class BaseModelFormSet(BaseFormSet): return self.save_existing_objects(commit) + self.save_new_objects(commit) def save_existing_objects(self, commit=True): + self.changed_objects = [] + self.deleted_objects = [] if not self.get_queryset(): return [] + # Put the objects from self.get_queryset into a dict so they are easy to lookup by pk existing_objects = {} for obj in self.get_queryset(): @@ -338,23 +341,25 @@ class BaseModelFormSet(BaseFormSet): for form in self.initial_forms: obj = existing_objects[form.cleaned_data[self.model._meta.pk.attname]] if self.can_delete and form.cleaned_data[DELETION_FIELD_NAME]: + self.deleted_objects.append(obj) obj.delete() else: - saved_instances.append(self.save_existing(form, obj, commit=commit)) + if form.changed_data: + self.changed_objects.append((obj, form.changed_data)) + saved_instances.append(self.save_existing(form, obj, commit=commit)) return saved_instances def save_new_objects(self, commit=True): - new_objects = [] + self.new_objects = [] for form in self.extra_forms: if not form.has_changed(): continue # If someone has marked an add form for deletion, don't save the - # object. At some point it would be nice if we didn't display - # the deletion widget for add forms. + # object. if self.can_delete and form.cleaned_data[DELETION_FIELD_NAME]: continue - new_objects.append(self.save_new(form, commit=commit)) - return new_objects + self.new_objects.append(self.save_new(form, commit=commit)) + return self.new_objects def add_fields(self, form, index): """Add a hidden field for the object's primary key.""" |
