Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

base.py 1.3KB

123456789101112131415161718192021222324252627282930313233
  1. from inspect import getfullargspec
  2. from django.template.library import InclusionNode, parse_bits
  3. class InclusionAdminNode(InclusionNode):
  4. """
  5. Template tag that allows its template to be overridden per model, per app,
  6. or globally.
  7. """
  8. def __init__(self, parser, token, func, template_name, takes_context=True):
  9. self.template_name = template_name
  10. params, varargs, varkw, defaults, kwonly, kwonly_defaults, _ = getfullargspec(func)
  11. bits = token.split_contents()
  12. args, kwargs = parse_bits(
  13. parser, bits[1:], params, varargs, varkw, defaults, kwonly,
  14. kwonly_defaults, takes_context, bits[0],
  15. )
  16. super().__init__(func, takes_context, args, kwargs, filename=None)
  17. def render(self, context):
  18. opts = context['opts']
  19. app_label = opts.app_label.lower()
  20. object_name = opts.object_name.lower()
  21. # Load template for this render call. (Setting self.filename isn't
  22. # thread-safe.)
  23. context.render_context[self] = context.template.engine.select_template([
  24. 'admin/%s/%s/%s' % (app_label, object_name, self.template_name),
  25. 'admin/%s/%s' % (app_label, self.template_name),
  26. 'admin/%s' % (self.template_name,),
  27. ])
  28. return super().render(context)