root / env / lib / python2.7 / site-packages / django / contrib / admin / templatetags / log.py @ 1a305335
History | View | Annotate | Download (2.07 KB)
1 | 1a305335 | officers | from django import template |
---|---|---|---|
2 | from django.contrib.admin.models import LogEntry |
||
3 | |||
4 | register = template.Library() |
||
5 | |||
6 | class 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 | |||
23 | @register.tag
|
||
24 | def get_admin_log(parser, token): |
||
25 | """
|
||
26 | Populates a template variable with the admin log for the given criteria.
|
||
27 |
|
||
28 | Usage::
|
||
29 |
|
||
30 | {% get_admin_log [limit] as [varname] for_user [context_var_containing_user_obj] %}
|
||
31 |
|
||
32 | Examples::
|
||
33 |
|
||
34 | {% get_admin_log 10 as admin_log for_user 23 %}
|
||
35 | {% get_admin_log 10 as admin_log for_user user %}
|
||
36 | {% get_admin_log 10 as admin_log %}
|
||
37 |
|
||
38 | Note that ``context_var_containing_user_obj`` can be a hard-coded integer
|
||
39 | (user ID) or the name of a template context variable containing the user
|
||
40 | object whose ID you want.
|
||
41 | """
|
||
42 | tokens = token.contents.split() |
||
43 | if len(tokens) < 4: |
||
44 | raise template.TemplateSyntaxError(
|
||
45 | "'get_admin_log' statements require two arguments")
|
||
46 | if not tokens[1].isdigit(): |
||
47 | raise template.TemplateSyntaxError(
|
||
48 | "First argument to 'get_admin_log' must be an integer")
|
||
49 | if tokens[2] != 'as': |
||
50 | raise template.TemplateSyntaxError(
|
||
51 | "Second argument to 'get_admin_log' must be 'as'")
|
||
52 | if len(tokens) > 4: |
||
53 | if tokens[4] != 'for_user': |
||
54 | raise template.TemplateSyntaxError(
|
||
55 | "Fourth argument to 'get_admin_log' must be 'for_user'")
|
||
56 | return AdminLogNode(limit=tokens[1], varname=tokens[3], user=(len(tokens) > 5 and tokens[5] or None)) |