Ticket #6117: admin_log.2.diff

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

Made it a bit more efficient

  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    index 7f95801..abd9582 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            fields = [f.name for f in obj._meta.fields]
     414            for key in dict_.keys():
     415                if key in fields:
     416                    if dict_[key] is None and getattr(obj, key) is not None:
     417                        removed.append(key)
     418                    elif dict_[key] is not None and getattr(obj, key) is None:
     419                        added.append(key)
     420                    elif dict_[key] != getattr(obj, key):
     421                        changed.append(key)
     422            return added, changed, removed
     423
    411424        from django.contrib.admin.models import LogEntry, CHANGE
    412425        from django.contrib.contenttypes.models import ContentType
    413426        opts = model._meta
    class ModelAdmin(BaseModelAdmin): 
    417430        if formsets:
    418431            for formset in formsets:
    419432                formset.save()
    420 
     433        added, changed, removed = diff(new_object, form.cleaned_data)
    421434        # Construct the change message. TODO: Temporarily commented-out,
    422435        # as manipulator object doesn't exist anymore, and we don't yet
    423436        # have a way to get fields_added, fields_changed, fields_deleted.
    424437        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)
     438        if added:
     439            change_message.append(_('Added %s.') % ' and '.join(added))
     440        if changed:
     441            change_message.append(_('Changed %s.') % ' and '.join(changed))
     442        if removed:
     443            change_message.append(_('Deleted %s.') % ' and '.join(removed))
     444        change_message = ' '.join(change_message)
    432445        if not change_message:
    433446            change_message = _('No fields changed.')
    434447        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