Ticket #8472: recent_action_on_appindex_v2.diff

File recent_action_on_appindex_v2.diff, 4.5 KB (added by Dario Ocles, 13 years ago)
  • django/contrib/admin/templatetags/log.py

     
    11from django import template
    22from django.contrib.admin.models import LogEntry
     3from django.contrib.contenttypes.models import ContentType
    34
    45register = template.Library()
    56
    67class AdminLogNode(template.Node):
    7     def __init__(self, limit, varname, user):
    8         self.limit, self.varname, self.user = limit, varname, user
     8    def __init__(self, limit, varname, user, for_app):
     9        self.limit, self.varname, self.user, self.for_app = limit, varname, user, for_app
    910
    1011    def __repr__(self):
    1112        return "<GetAdminLog Node>"
     
    1718            user_id = self.user
    1819            if not user_id.isdigit():
    1920                user_id = context[self.user].id
    20             context[self.varname] = LogEntry.objects.filter(user__id__exact=user_id).select_related('content_type', 'user')[:self.limit]
     21            filters = {'user__id__exact': user_id}
     22
     23            if self.for_app:
     24                app_name = template.FilterExpression(self.for_app, template.Parser('')).resolve(context)
     25                contenttypes = ContentType.objects.filter(app_label=app_name.lower())
     26                ids = [contenttype.id for contenttype in contenttypes]
     27                filters['content_type__in'] = ids
     28
     29            context[self.varname] = LogEntry.objects.filter(**filters).select_related('content_type', 'user')[:self.limit]
    2130        return ''
    2231
    2332class DoGetAdminLog:
     
    2635
    2736    Usage::
    2837
    29         {% get_admin_log [limit] as [varname] for_user [context_var_containing_user_obj] %}
     38        {% get_admin_log [limit] as [varname] for_user [context_var_containing_user_obj] for_app [appname] %}
    3039
    3140    Examples::
    3241
     42        {% get_admin_log 10 as admin_log for_user 23 for_app "appname" %}
    3343        {% get_admin_log 10 as admin_log for_user 23 %}
    3444        {% get_admin_log 10 as admin_log for_user user %}
    3545        {% get_admin_log 10 as admin_log %}
     
    5262        if len(tokens) > 4:
    5363            if tokens[4] != 'for_user':
    5464                raise template.TemplateSyntaxError("Fourth argument in '%s' must be 'for_user'" % self.tag_name)
    55         return AdminLogNode(limit=tokens[1], varname=tokens[3], user=(len(tokens) > 5 and tokens[5] or None))
     65        if len(tokens) > 6:
     66            if tokens[6] != 'for_app':
     67                raise template.TemplateSyntaxError("Fifth argument in '%s' must be 'for_app'" % self.tag_name)
     68        return AdminLogNode(limit=tokens[1], varname=tokens[3], user=(len(tokens) > 5 and tokens[5] or None), for_app=(len(tokens) > 7 and tokens[7] or None))
    5669
    5770register.tag('get_admin_log', DoGetAdminLog('get_admin_log'))
  • django/contrib/admin/templates/admin/app_index.html

     
    1212
    1313{% endif %}
    1414
    15 {% block sidebar %}{% endblock %}
    16  No newline at end of file
     15{% block sidebar %}
     16<div id="content-related">
     17    <div class="module" id="recent-actions-module">
     18        <h2>{% trans 'Recent Actions' %}</h2>
     19        <h3>{% trans 'My Actions' %}</h3>
     20            {% load log %}
     21            {% get_admin_log 10 as admin_log for_user user for_app app_list.0.name %}
     22            {% if not admin_log %}
     23            <p>{% trans 'None available' %}</p>
     24            {% else %}
     25            <ul class="actionlist">
     26            {% for entry in admin_log %}
     27            <li class="{% if entry.is_addition %}addlink{% endif %}{% if entry.is_change %}changelink{% endif %}{% if entry.is_deletion %}deletelink{% endif %}">
     28                {% if entry.is_deletion or not entry.get_admin_url %}
     29                    {{ entry.object_repr }}
     30                {% else %}
     31                    <a href="{{ entry.get_admin_url }}">{{ entry.object_repr }}</a>
     32                {% endif %}
     33                <br/>
     34                {% if entry.content_type %}
     35                    <span class="mini quiet">{% filter capfirst %}{% trans entry.content_type.name %}{% endfilter %}</span>
     36                {% else %}
     37                    <span class="mini quiet">{% trans 'Unknown content' %}</span>
     38                {% endif %}
     39            </li>
     40            {% endfor %}
     41            </ul>
     42            {% endif %}
     43    </div>
     44</div>
     45{% endblock %}
Back to Top