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.

helpers.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. from django.core.exceptions import ImproperlyConfigured
  2. from django.template.loader import render_to_string
  3. from classytags.core import Tag
  4. from classytags.utils import flatten_context
  5. class AsTag(Tag):
  6. """
  7. Same as tag but allows for an optional 'as varname'. The 'as varname'
  8. options must be added 'manually' to the options class.
  9. """
  10. def __init__(self, parser, tokens):
  11. super(AsTag, self).__init__(parser, tokens)
  12. if len(self.options.breakpoints) < 1:
  13. raise ImproperlyConfigured(
  14. "AsTag subclasses require at least one breakpoint."
  15. )
  16. last_breakpoint = self.options.options[self.options.breakpoints[-1]]
  17. optscount = len(last_breakpoint)
  18. if optscount != 1:
  19. raise ImproperlyConfigured(
  20. "The last breakpoint of AsTag subclasses require exactly one "
  21. "argument, got %s instead." % optscount
  22. )
  23. self.varname_name = last_breakpoint[-1].name
  24. def render_tag(self, context, **kwargs):
  25. """
  26. INTERNAL!
  27. Get's the value for the current context and arguments and puts it into
  28. the context if needed or returns it.
  29. """
  30. varname = kwargs.pop(self.varname_name)
  31. if varname:
  32. value = self.get_value_for_context(context, **kwargs)
  33. context[varname] = value
  34. return ''
  35. else:
  36. value = self.get_value(context, **kwargs)
  37. return value
  38. def get_value_for_context(self, context, **kwargs):
  39. """
  40. Called when a value for a varname (in the "as varname" case) should is
  41. requested. This can be used to for example suppress exceptions in this
  42. case.
  43. Returns the value to be set.
  44. """
  45. return self.get_value(context, **kwargs)
  46. def get_value(self, context, **kwargs):
  47. """
  48. Returns the value for the current context and arguments.
  49. """
  50. raise NotImplementedError
  51. class InclusionTag(Tag):
  52. """
  53. A helper Tag class which allows easy inclusion tags.
  54. The template attribute must be set.
  55. Instead of render_tag, override get_context in your subclasses.
  56. Optionally override get_template in your subclasses.
  57. """
  58. template = None
  59. push_context = False
  60. def render_tag(self, context, **kwargs):
  61. """
  62. INTERNAL!
  63. Gets the context and data to render.
  64. """
  65. template = self.get_template(context, **kwargs)
  66. if self.push_context:
  67. safe_context = flatten_context(context)
  68. data = self.get_context(safe_context, **kwargs)
  69. safe_context.update(**data)
  70. output = render_to_string(template, safe_context)
  71. else:
  72. new_context = context.new(
  73. flatten_context(self.get_context(context, **kwargs))
  74. )
  75. data = flatten_context(new_context)
  76. output = render_to_string(template, data)
  77. return output
  78. def get_template(self, context, **kwargs):
  79. """
  80. Returns the template to be used for the current context and arguments.
  81. """
  82. return self.template
  83. def get_context(self, context, **kwargs):
  84. """
  85. Returns the context to render the template with.
  86. """
  87. return {}