PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms 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
Possible License(s): BSD-3-Clause
  1. from django import template
  2. from django.contrib.admin.models import LogEntry
  3. register = template.Library()
  4. class AdminLogNode(template.Node):
  5. def __init__(self, limit, varname, user):
  6. self.limit, self.varname, self.user = limit, varname, user
  7. def __repr__(self):
  8. return "<GetAdminLog Node>"
  9. def render(self, context):
  10. if self.user is None:
  11. context[self.varname] = LogEntry.objects.all().select_related('content_type', 'user')[:self.limit]
  12. else:
  13. user_id = self.user
  14. if not user_id.isdigit():
  15. user_id = context[self.user].id
  16. context[self.varname] = LogEntry.objects.filter(user__id__exact=user_id).select_related('content_type', 'user')[:self.limit]
  17. return ''
  18. class DoGetAdminLog:
  19. """
  20. Populates a template variable with the admin log for the given criteria.
  21. Usage::
  22. {% get_admin_log [limit] as [varname] for_user [context_var_containing_user_obj] %}
  23. Examples::
  24. {% get_admin_log 10 as admin_log for_user 23 %}
  25. {% get_admin_log 10 as admin_log for_user user %}
  26. {% get_admin_log 10 as admin_log %}
  27. Note that ``context_var_containing_user_obj`` can be a hard-coded integer
  28. (user ID) or the name of a template context variable containing the user
  29. object whose ID you want.
  30. """
  31. def __init__(self, tag_name):
  32. self.tag_name = tag_name
  33. def __call__(self, parser, token):
  34. tokens = token.contents.split()
  35. if len(tokens) < 4:
  36. raise template.TemplateSyntaxError("'%s' statements require two arguments" % self.tag_name)
  37. if not tokens[1].isdigit():
  38. raise template.TemplateSyntaxError("First argument in '%s' must be an integer" % self.tag_name)
  39. if tokens[2] != 'as':
  40. raise template.TemplateSyntaxError("Second argument in '%s' must be 'as'" % self.tag_name)
  41. if len(tokens) > 4:
  42. if tokens[4] != 'for_user':
  43. raise template.TemplateSyntaxError("Fourth argument in '%s' must be 'for_user'" % self.tag_name)
  44. return AdminLogNode(limit=tokens[1], varname=tokens[3], user=(len(tokens) > 5 and tokens[5] or None))
  45. register.tag('get_admin_log', DoGetAdminLog('get_admin_log'))