Ticket #6117: admin_log.diff

File admin_log.diff, 2.6 KB (added by Alex Gaynor, 10 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.

  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    index 7f95801..f3dbd0f 100644
    a b class ModelAdmin(BaseModelAdmin): 
    408408       
    409409        `formsets` is a sequence of InlineFormSet instances that are verified to be valid.
    410410        """
     411        def diff(obj, dict_):
     412            added, changed, removed = [], [], []
     413            for key in dict_.keys():
     414                if key in [f.name for f in obj._meta.fields]:
     415                    if dict_[key] is None and getattr(obj, key) is not None:
     416                        removed.append(key)
     417                    elif dict_[key] is not None and getattr(obj, key) is None:
     418                        added.append(key)
     419                    elif dict_[key] != getattr(obj, key):
     420                        changed.append(key)
     421            return added, changed, removed
     422
    411423        from django.contrib.admin.models import LogEntry, CHANGE
    412424        from django.contrib.contenttypes.models import ContentType
    413425        opts = model._meta
    class ModelAdmin(BaseModelAdmin): 
    417429        if formsets:
    418430            for formset in formsets:
    419431                formset.save()
    420 
     432        added, changed, removed = diff(new_object, form.cleaned_data)
    421433        # Construct the change message. TODO: Temporarily commented-out,
    422434        # as manipulator object doesn't exist anymore, and we don't yet
    423435        # have a way to get fields_added, fields_changed, fields_deleted.
    424436        change_message = []
    425         #if manipulator.fields_added:
    426             #change_message.append(_('Added %s.') % get_text_list(manipulator.fields_added, _('and')))
    427         #if manipulator.fields_changed:
    428             #change_message.append(_('Changed %s.') % get_text_list(manipulator.fields_changed, _('and')))
    429         #if manipulator.fields_deleted:
    430             #change_message.append(_('Deleted %s.') % get_text_list(manipulator.fields_deleted, _('and')))
    431         #change_message = ' '.join(change_message)
     437        if added:
     438            change_message.append(_('Added %s.') % ' and '.join(added))
     439        if changed:
     440            change_message.append(_('Changed %s.') % ' and '.join(changed))
     441        if removed:
     442            change_message.append(_('Deleted %s.') % ' and '.join(removed))
     443        change_message = ' '.join(change_message)
    432444        if not change_message:
    433445            change_message = _('No fields changed.')
    434446        LogEntry.objects.log_action(request.user.id, ContentType.objects.get_for_model(model).id, pk_value, force_unicode(new_object), CHANGE, change_message)
Back to Top