Ticket #8472: recent_action_on_appindex.diff

File recent_action_on_appindex.diff, 4.9 KB (added by Dario Ocles, 14 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, current_app):
     9        self.limit, self.varname, self.user, self.current_app = limit, varname, user, current_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.current_app and len(context['app_list']) == 1: # If there are more than one app don't use the filter
     24                current_app = context['app_list'][0]['name'].lower()
     25                contenttypes = ContentType.objects.filter(app_label=current_app)
     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_current_app %}
    3039
    3140    Examples::
    3241
     42        {% get_admin_log 10 as admin_log for_user 23 for_current_app %}
    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 %}
    3646
     47    The option ``for_current_app`` should be used only inside of an
     48    application index. It will look up for the current app inside of app_list.
     49    If this option is used on any other place, it just won't make any filter.
    3750    Note that ``context_var_containing_user_obj`` can be a hard-coded integer
    3851    (user ID) or the name of a template context variable containing the user
    3952    object whose ID you want.
     
    5265        if len(tokens) > 4:
    5366            if tokens[4] != 'for_user':
    5467                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))
     68        if len(tokens) > 6:
     69            if tokens[6] != 'for_current_app':
     70                raise template.TemplateSyntaxError("Fifth argument in '%s' must be 'for_current_app'" % self.tag_name)
     71        return AdminLogNode(limit=tokens[1], varname=tokens[3], user=(len(tokens) > 5 and tokens[5] or None), current_app=(len(tokens) > 6))
    5672
    5773register.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_current_app %}
     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