Ticket #6117: admin_changed.2.diff

File admin_changed.2.diff, 8.0 KB (added by Alex Gaynor, 17 years ago)

Added some tests, there may be some test failures, I can't tell right now

  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    index d0cca0a..d6ee1ee 100644
    a b class ModelAdmin(BaseModelAdmin):  
    385385            for formset in formsets:
    386386                formset.save()
    387387
    388         # Construct the change message. TODO: Temporarily commented-out,
    389         # as manipulator object doesn't exist anymore, and we don't yet
    390         # have a way to get fields_added, fields_changed, fields_deleted.
     388        # Construct the change message.                 
    391389        change_message = []
    392         #if manipulator.fields_added:
    393             #change_message.append(_('Added %s.') % get_text_list(manipulator.fields_added, _('and')))
    394         #if manipulator.fields_changed:
    395             #change_message.append(_('Changed %s.') % get_text_list(manipulator.fields_changed, _('and')))
    396         #if manipulator.fields_deleted:
    397             #change_message.append(_('Deleted %s.') % get_text_list(manipulator.fields_deleted, _('and')))
    398         #change_message = ' '.join(change_message)
     390        if form.changed_data:
     391            change_message.append(_('Changed %s.') % get_text_list(form.changed_data, _('and')))
     392           
     393        if formsets:
     394            for formset in formsets:
     395                for added_object in formset.new_objects:
     396                    change_message.append(_('Added %s "%s".')
     397                                          % (added_object._meta.verbose_name, added_object))
     398                for changed_object, changed_fields in formset.changed_objects:
     399                    change_message.append(_('Changed %s for %s "%s".')
     400                                          % (get_text_list(changed_fields, _('and')),
     401                                             changed_object._meta.verbose_name,
     402                                             changed_object))
     403                for deleted_object in formset.deleted_objects:
     404                    change_message.append(_('Deleted %s "%s".')
     405                                          % (deleted_object._meta.verbose_name, deleted_object))
     406           
     407        change_message = ' '.join(change_message)
    399408        if not change_message:
    400409            change_message = _('No fields changed.')
    401410        LogEntry.objects.log_action(request.user.id, ContentType.objects.get_for_model(model).id, pk_value, force_unicode(new_object), CHANGE, change_message)
  • django/newforms/forms.py

    diff --git a/django/newforms/forms.py b/django/newforms/forms.py
    index 42dacec..4c4c0e0 100644
    a b class BaseForm(StrAndUnicode):  
    8181        self.label_suffix = label_suffix
    8282        self.empty_permitted = empty_permitted
    8383        self._errors = None # Stores the errors after clean() has been called.
     84        self._changed_data = None
    8485
    8586        # The base_fields class attribute is the *class-wide* definition of
    8687        # fields. Because a particular *instance* of the class might want to
    class BaseForm(StrAndUnicode):  
    256257            if field.widget._has_changed(initial_value, data_value):
    257258                return True
    258259        return False
     260   
     261    def _get_changed_data(self):
     262        if self._changed_data is None:
     263            self._changed_data = []
     264            for name, field in self.fields.items():
     265                prefixed_name = self.add_prefix(name)
     266                data_value = field.widget.value_from_datadict(self.data, self.files, prefixed_name)
     267                initial_value = self.initial.get(name, field.initial)
     268                if field.widget._has_changed(initial_value, data_value):
     269                    self._changed_data.append(name)
     270        return self._changed_data
     271    changed_data = property(_get_changed_data)
    259272
    260273    def _get_media(self):
    261274        """
  • django/newforms/models.py

    diff --git a/django/newforms/models.py b/django/newforms/models.py
    index f12b53d..b1332a7 100644
    a b class BaseModelFormSet(BaseFormSet):  
    328328        return self.save_existing_objects(commit) + self.save_new_objects(commit)
    329329
    330330    def save_existing_objects(self, commit=True):
     331        self.changed_objects = []
     332        self.deleted_objects = []
    331333        if not self.get_queryset():
    332334            return []
     335
    333336        # Put the objects from self.get_queryset into a dict so they are easy to lookup by pk
    334337        existing_objects = {}
    335338        for obj in self.get_queryset():
    class BaseModelFormSet(BaseFormSet):  
    338341        for form in self.initial_forms:
    339342            obj = existing_objects[form.cleaned_data[self.model._meta.pk.attname]]
    340343            if self.can_delete and form.cleaned_data[DELETION_FIELD_NAME]:
     344                self.deleted_objects.append(obj)
    341345                obj.delete()
    342346            else:
    343                 saved_instances.append(self.save_existing(form, obj, commit=commit))
     347                if form.changed_data:
     348                    self.changed_objects.append((obj, form.changed_data))
     349                    saved_instances.append(self.save_existing(form, obj, commit=commit))
    344350        return saved_instances
    345351
    346352    def save_new_objects(self, commit=True):
    347         new_objects = []
     353        self.new_objects = []
    348354        for form in self.extra_forms:
    349355            if not form.has_changed():
    350356                continue
    class BaseModelFormSet(BaseFormSet):  
    353359            # the deletion widget for add forms.
    354360            if self.can_delete and form.cleaned_data[DELETION_FIELD_NAME]:
    355361                continue
    356             new_objects.append(self.save_new(form, commit=commit))
    357         return new_objects
     362            self.new_objects.append(self.save_new(form, commit=commit))
     363        return self.new_objects
    358364
    359365    def add_fields(self, form, index):
    360366        """Add a hidden field for the object's primary key."""
  • tests/regressiontests/forms/forms.py

    diff --git a/tests/regressiontests/forms/forms.py b/tests/regressiontests/forms/forms.py
    index a865e91..96c871e 100644
    a b initial that returns False on a boolean call needs to be treated literally.  
    17381738>>> form.is_valid()
    17391739True
    17401740
     1741>>> from django import newforms as forms
     1742>>> class Person(forms.Form):
     1743...     first_name = forms.CharField()
     1744...     last_name = forms.CharField()
     1745...     age = forms.IntegerField()
     1746...     happy = forms.BooleanField()
     1747...     resume = forms.FileField()
     1748
     1749>>> p = Person()
     1750>>> p.changed_data
     1751[]
     1752
     1753>>> p = Person({'first_name': 'John'})
     1754>>> p.changed_data
     1755['first_name']
     1756
     1757>>> p = Person({'first_name': 'John', 'last_name': 'Boothe'})
     1758>>> p.changed_data
     1759['first_name', 'last_name']
     1760
     1761>>> p = Person({'first_name': 'John', 'last_name': 'Boothe'}, initial={'last_name': 'Boothe'})
     1762>>> p.changed_data
     1763['first_name']
     1764
     1765>>> p = Person({'age': '20'}, initial={'age': 20})
     1766>>> p.changed_data
     1767[]
     1768
     1769>>> p = Person({'happy': True}, initial={'happy': False})
     1770>>> p.changed_data
     1771['happy']
     1772
     1773>>> p = Person({'happy': 1}, initial={'happy': True})
     1774>>> p.changed_data
     1775[]
     1776
     1777>>> p = Person({'happy': '1'}, initial={'happy': True})
     1778>>> p.changed_data
     1779[]
    17411780"""
  • tests/regressiontests/forms/widgets.py

    diff --git a/tests/regressiontests/forms/widgets.py b/tests/regressiontests/forms/widgets.py
    index 0e69602..849224d 100644
    a b u'<input type="text" name="date" value="2007-09-17 12:51:34" />'  
    929929u'<input type="text" name="date" value="2007-09-17 12:51:34" />'
    930930>>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51))
    931931u'<input type="text" name="date" value="2007-09-17 12:51:00" />'
     932
     933>>> from django import newforms as forms
     934>>> w =  forms.TextInput()
     935>>> w._has_changed('John', 'John')
     936False
     937
     938>>> w._has_changed('John', '')
     939True
     940
     941>>> w._has_changed('', 'John')
     942True
     943
     944>>> w._has_changed('', '')
     945False
     946
     947# This test is for Integer Fields, which uses the TextInput for a widget.
     948>>> w._has_changed(20, '20')
     949False
     950
     951>>> w =  forms.CheckboxInput()
     952>>> w._has_changed(True, '1')
     953False
     954
     955>>> w._has_changed(True, '0')
     956False
     957
     958>>> w._has_changed(True, 0)
     959True
     960
    932961"""
Back to Top