PageRenderTime 36ms CodeModel.GetById 21ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/django/contrib/admin/templatetags/log.py

https://code.google.com/p/mango-py/
Python | 57 lines | 53 code | 4 blank | 0 comment | 3 complexity | 39344b80a579c6fdc79f31b38052dc27 MD5 | raw file
 1from django import template
 2from django.contrib.admin.models import LogEntry
 3
 4register = template.Library()
 5
 6class AdminLogNode(template.Node):
 7    def __init__(self, limit, varname, user):
 8        self.limit, self.varname, self.user = limit, varname, user
 9
10    def __repr__(self):
11        return "<GetAdminLog Node>"
12
13    def render(self, context):
14        if self.user is None:
15            context[self.varname] = LogEntry.objects.all().select_related('content_type', 'user')[:self.limit]
16        else:
17            user_id = self.user
18            if not user_id.isdigit():
19                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        return ''
22
23class DoGetAdminLog:
24    """
25    Populates a template variable with the admin log for the given criteria.
26
27    Usage::
28
29        {% get_admin_log [limit] as [varname] for_user [context_var_containing_user_obj] %}
30
31    Examples::
32
33        {% get_admin_log 10 as admin_log for_user 23 %}
34        {% get_admin_log 10 as admin_log for_user user %}
35        {% get_admin_log 10 as admin_log %}
36
37    Note that ``context_var_containing_user_obj`` can be a hard-coded integer
38    (user ID) or the name of a template context variable containing the user
39    object whose ID you want.
40    """
41    def __init__(self, tag_name):
42        self.tag_name = tag_name
43
44    def __call__(self, parser, token):
45        tokens = token.contents.split()
46        if len(tokens) < 4:
47            raise template.TemplateSyntaxError("'%s' statements require two arguments" % self.tag_name)
48        if not tokens[1].isdigit():
49            raise template.TemplateSyntaxError("First argument in '%s' must be an integer" % self.tag_name)
50        if tokens[2] != 'as':
51            raise template.TemplateSyntaxError("Second argument in '%s' must be 'as'" % self.tag_name)
52        if len(tokens) > 4:
53            if tokens[4] != 'for_user':
54                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))
56
57register.tag('get_admin_log', DoGetAdminLog('get_admin_log'))