Ticket #8472: recent_action_on_appindex_v3.diff

File recent_action_on_appindex_v3.diff, 8.4 KB (added by Preston Timmons, 14 years ago)

Added tests to patch

  • django/contrib/admin/templates/admin/app_index.html

    diff --git a/django/contrib/admin/templates/admin/app_index.html b/django/contrib/admin/templates/admin/app_index.html
    index 120433d..89d5e66 100644
    a b  
    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 %}
  • django/contrib/admin/templatetags/log.py

    diff --git a/django/contrib/admin/templatetags/log.py b/django/contrib/admin/templatetags/log.py
    index 663830b..20db05a 100644
    a b  
    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>"
    class AdminLogNode(template.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:
    class 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 %}
    class DoGetAdminLog:  
    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'))
  • new file tests/regressiontests/admin_log/models.py

    diff --git a/tests/regressiontests/admin_log/__init__.py b/tests/regressiontests/admin_log/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/admin_log/models.py b/tests/regressiontests/admin_log/models.py
    new file mode 100644
    index 0000000..855dcee
    - +  
     1from django.db import models
     2
     3class Person(models.Model):
     4    name = models.CharField(max_length=128)
     5
     6class Building(models.Model):
     7    name = models.CharField(max_length=128)
  • new file tests/regressiontests/admin_log/tests.py

    diff --git a/tests/regressiontests/admin_log/tests.py b/tests/regressiontests/admin_log/tests.py
    new file mode 100644
    index 0000000..e6855f0
    - +  
     1from django.contrib.admin.models import LogEntry, ADDITION
     2from django.contrib.auth.models import User
     3from django.contrib.contenttypes.models import ContentType
     4from django.template import Context, Template
     5from django.test import TransactionTestCase
     6
     7from models import Building, Person
     8
     9
     10class GetAdminLogTemplateTagTests(TransactionTestCase):
     11
     12    def test_get_admin_log(self):
     13        user = User.objects.create(
     14            username="testuser",
     15        )
     16
     17        b1 = Building.objects.create(name="The Colosseum")
     18        b2 = Building.objects.create(name="Eiffel Tower")
     19        p1 = Person.objects.create(name="Caesar")
     20
     21        LogEntry.objects.log_action(
     22            user_id = user.pk,
     23            content_type_id = ContentType.objects.get_for_model(User).pk,
     24            object_id = user.pk,
     25            object_repr = "testuser",
     26            action_flag = ADDITION,
     27        )
     28
     29        LogEntry.objects.log_action(
     30            user_id = user.pk,
     31            content_type_id = ContentType.objects.get_for_model(Building).pk,
     32            object_id = b1.pk,
     33            object_repr = "colosseum",
     34            action_flag = ADDITION,
     35        )
     36
     37        LogEntry.objects.log_action(
     38            user_id = user.pk,
     39            content_type_id = ContentType.objects.get_for_model(Building).pk,
     40            object_id = b2.pk,
     41            object_repr = "eiffeltower",
     42            action_flag = ADDITION,
     43        )
     44
     45        LogEntry.objects.log_action(
     46            user_id = user.pk,
     47            content_type_id = ContentType.objects.get_for_model(Person).pk,
     48            object_id = p1.pk,
     49            object_repr = "caesar",
     50            action_flag = ADDITION,
     51        )
     52
     53        template = Template('{% load log %}{% spaceless %}{% get_admin_log 10 as admin_log for_user ' + str(user.pk) + ' %}{% for entry in admin_log %}{{ entry.object_repr }}{% endfor %}{% endspaceless %}')
     54        context = Context()
     55        output = template.render(context)
     56        self.assertEqual(output, "caesareiffeltowercolosseumtestuser")
     57
     58        template = Template('{% load log %}{% spaceless %}{% get_admin_log 10 as admin_log for_user ' + str(user.pk) + ' for_app "auth" %}{% for entry in admin_log %}{{ entry.object_repr }}{% endfor %}{% endspaceless %}')
     59        context = Context()
     60        output = template.render(context)
     61        self.assertEqual(output, "testuser")
     62
     63        template = Template('{% load log %}{% spaceless %}{% get_admin_log 10 as admin_log for_user ' + str(user.pk) + ' for_app app_name %}{% for entry in admin_log %}{{ entry.object_repr }}{% endfor %}{% endspaceless %}')
     64        context = Context(dict(app_name="admin_log"))
     65        output = template.render(context)
     66        self.assertEqual(output, "caesareiffeltowercolosseum")
     67
     68    def test_get_empty_admin_log(self):
     69        template = Template('{% load log %}{% spaceless %}{% get_admin_log 10 as admin_log for_user 1 %}{% for entry in admin_log %}{{ entry.object_repr }}{% endfor %}{% endspaceless %}')
     70        context = Context()
     71        self.assertEqual(template.render(context), "")
Back to Top