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.

utils.py 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import re
  2. from copy import copy
  3. from distutils.version import LooseVersion
  4. from classytags.compat import compat_basestring
  5. from django import get_version
  6. from django.template import Context, RequestContext
  7. from django.template.context import BaseContext
  8. DJANGO_1_9_OR_HIGHER = (
  9. LooseVersion(get_version()) >= LooseVersion('1.9')
  10. )
  11. class NULL:
  12. """
  13. Internal type to differentiate between None and No-Input
  14. """
  15. class TemplateConstant(object):
  16. """
  17. A 'constant' internal template variable which basically allows 'resolving'
  18. returning it's initial value
  19. """
  20. def __init__(self, value):
  21. self.literal = value
  22. if isinstance(value, compat_basestring):
  23. self.value = value.strip('"\'')
  24. else:
  25. self.value = value
  26. def __repr__(self): # pragma: no cover
  27. return '<TemplateConstant: %s>' % repr(self.value)
  28. def resolve(self, context):
  29. return self.value
  30. class StructuredOptions(object):
  31. """
  32. Bootstrapped options
  33. """
  34. def __init__(self, options, breakpoints, blocks, combind_breakpoints):
  35. self.options = options
  36. self.breakpoints = copy(breakpoints)
  37. self.blocks = copy(blocks)
  38. self.combined_breakpoints = dict(combind_breakpoints.items())
  39. self.reversed_combined_breakpoints = dict(
  40. (v, k) for k, v in combind_breakpoints.items()
  41. )
  42. self.current_breakpoint = None
  43. if self.breakpoints:
  44. self.next_breakpoint = self.breakpoints.pop(0)
  45. else:
  46. self.next_breakpoint = None
  47. def shift_breakpoint(self):
  48. """
  49. Shift to the next breakpoint
  50. """
  51. self.current_breakpoint = self.next_breakpoint
  52. if self.breakpoints:
  53. self.next_breakpoint = self.breakpoints.pop(0)
  54. else:
  55. self.next_breakpoint = None
  56. def get_arguments(self):
  57. """
  58. Get the current arguments
  59. """
  60. return copy(self.options[self.current_breakpoint])
  61. _re1 = re.compile('(.)([A-Z][a-z]+)')
  62. _re2 = re.compile('([a-z0-9])([A-Z])')
  63. def get_default_name(name):
  64. """
  65. Turns "CamelCase" into "camel_case"
  66. """
  67. return _re2.sub(r'\1_\2', _re1.sub(r'\1_\2', name)).lower()
  68. def mixin(parent, child, attrs=None):
  69. attrs = attrs or {}
  70. return type(
  71. '%sx%s' % (parent.__name__, child.__name__),
  72. (child, parent),
  73. attrs
  74. )
  75. def flatten_context(context):
  76. def do_flatten(context):
  77. flat = {}
  78. for d in context.dicts:
  79. if isinstance(d, (Context, RequestContext)):
  80. flat.update(do_flatten(d))
  81. else:
  82. flat.update(d)
  83. return flat
  84. if callable(getattr(context, 'flatten', None)) and DJANGO_1_9_OR_HIGHER:
  85. return context.flatten()
  86. elif isinstance(context, BaseContext):
  87. return do_flatten(context)
  88. return context