Code

Ticket #6117: admin_log.diff

File admin_log.diff, 2.6 KB (added by Alex, 6 years ago)

This should work, brosner has said this should probably be in modelforms, I'm not familliar enough to work on those, however someone can use this work.

Line 
1diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
2index 7f95801..f3dbd0f 100644
3--- a/django/contrib/admin/options.py
4+++ b/django/contrib/admin/options.py
5@@ -408,6 +408,18 @@ class ModelAdmin(BaseModelAdmin):
6         
7         `formsets` is a sequence of InlineFormSet instances that are verified to be valid.
8         """
9+        def diff(obj, dict_):
10+            added, changed, removed = [], [], []
11+            for key in dict_.keys():
12+                if key in [f.name for f in obj._meta.fields]:
13+                    if dict_[key] is None and getattr(obj, key) is not None:
14+                        removed.append(key)
15+                    elif dict_[key] is not None and getattr(obj, key) is None:
16+                        added.append(key)
17+                    elif dict_[key] != getattr(obj, key):
18+                        changed.append(key)
19+            return added, changed, removed
20+
21         from django.contrib.admin.models import LogEntry, CHANGE
22         from django.contrib.contenttypes.models import ContentType
23         opts = model._meta
24@@ -417,18 +429,18 @@ class ModelAdmin(BaseModelAdmin):
25         if formsets:
26             for formset in formsets:
27                 formset.save()
28-
29+        added, changed, removed = diff(new_object, form.cleaned_data)
30         # Construct the change message. TODO: Temporarily commented-out,
31         # as manipulator object doesn't exist anymore, and we don't yet
32         # have a way to get fields_added, fields_changed, fields_deleted.
33         change_message = []
34-        #if manipulator.fields_added:
35-            #change_message.append(_('Added %s.') % get_text_list(manipulator.fields_added, _('and')))
36-        #if manipulator.fields_changed:
37-            #change_message.append(_('Changed %s.') % get_text_list(manipulator.fields_changed, _('and')))
38-        #if manipulator.fields_deleted:
39-            #change_message.append(_('Deleted %s.') % get_text_list(manipulator.fields_deleted, _('and')))
40-        #change_message = ' '.join(change_message)
41+        if added:
42+            change_message.append(_('Added %s.') % ' and '.join(added))
43+        if changed:
44+            change_message.append(_('Changed %s.') % ' and '.join(changed))
45+        if removed:
46+            change_message.append(_('Deleted %s.') % ' and '.join(removed))
47+        change_message = ' '.join(change_message)
48         if not change_message:
49             change_message = _('No fields changed.')
50         LogEntry.objects.log_action(request.user.id, ContentType.objects.get_for_model(model).id, pk_value, force_unicode(new_object), CHANGE, change_message)