Ticket #2856: 2856.diff

File 2856.diff, 3.2 KB (added by ionut bizau, 17 years ago)

Added a new field (object_exists) to LogEntry, which becomes False after the object that that entry is referring to is deleted. I think this approach is better than just looking for the object by id, both for performance and for the situation where an object with the same id is added later. This field is then checked in the template.

  • django/contrib/admin/models.py

     
    1111
    1212class LogEntryManager(models.Manager):
    1313    def log_action(self, user_id, content_type_id, object_id, object_repr, action_flag, change_message=''):
    14         e = self.model(None, None, user_id, content_type_id, smart_unicode(object_id), object_repr[:200], action_flag, change_message)
     14        deletion = action_flag == DELETION
     15
     16        if deletion:
     17            for log in self.filter(object_id__exact=object_id):
     18                log.object_exists = False
     19                log.save()
     20
     21        e = self.model(None, None, user_id, content_type_id, smart_unicode(object_id), not deletion, object_repr[:200], action_flag, change_message)
    1522        e.save()
    1623
    1724class LogEntry(models.Model):
     
    1926    user = models.ForeignKey(User)
    2027    content_type = models.ForeignKey(ContentType, blank=True, null=True)
    2128    object_id = models.TextField(_('object id'), blank=True, null=True)
     29    object_exists = models.BooleanField(_('object exists'))
    2230    object_repr = models.CharField(_('object repr'), max_length=200)
    2331    action_flag = models.PositiveSmallIntegerField(_('action flag'))
    2432    change_message = models.TextField(_('change message'), blank=True)
     
    4957        """
    5058        Returns the admin URL to edit the object represented by this log entry.
    5159        This is relative to the Django admin index page.
     60        Returns None if the object has been deleted.
    5261        """
    53         return mark_safe(u"%s/%s/%s/" % (self.content_type.app_label, self.content_type.model, self.object_id))
     62        if self.object_exists:
     63            return mark_safe(u"%s/%s/%s/" % (self.content_type.app_label, self.content_type.model, self.object_id))
     64        else:
     65            return None
     66
  • django/contrib/admin/templates/admin/index.html

     
    5858            {% else %}
    5959            <ul class="actionlist">
    6060            {% for entry in admin_log %}
    61             <li class="{% if entry.is_addition %}addlink{% endif %}{% if entry.is_change %}changelink{% endif %}{% if entry.is_deletion %}deletelink{% endif %}">{% if not entry.is_deletion %}<a href="{{ entry.get_admin_url }}">{% endif %}{{ entry.object_repr|escape }}{% if not entry.is_deletion %}</a>{% endif %}<br /><span class="mini quiet">{% filter capfirst %}{% trans entry.content_type.name %}{% endfilter %}</span></li>
     61            <li class="{% if entry.is_addition %}addlink{% endif %}{% if entry.is_change %}changelink{% endif %}{% if entry.is_deletion %}deletelink{% endif %}">{% if entry.object_exists %}<a href="{{ entry.get_admin_url }}">{% endif %}{{ entry.object_repr|escape }}{% if not entry.is_deletion %}</a>{% endif %}<br /><span class="mini quiet">{% filter capfirst %}{% trans entry.content_type.name %}{% endfilter %}</span></li>
    6262            {% endfor %}
    6363            </ul>
    6464            {% endif %}
Back to Top