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.

blocks.py 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # -*- coding: utf-8 -*-
  2. from django.core.exceptions import ImproperlyConfigured
  3. def _collect(name, parser):
  4. collector = getattr(name, 'collect', None)
  5. if callable(collector):
  6. return collector(parser)
  7. return name
  8. class BlockDefinition(object):
  9. """
  10. Definition of 'parse-until-blocks' used by the parser.
  11. """
  12. def __init__(self, alias, *names):
  13. self.alias = alias
  14. self.names = names
  15. def validate(self, options):
  16. for name in self.names:
  17. validator = getattr(name, 'validate', None)
  18. if callable(validator):
  19. validator(options)
  20. def collect(self, parser):
  21. return [_collect(name, parser) for name in self.names]
  22. class VariableBlockName(object):
  23. def __init__(self, template, argname):
  24. self.template = template
  25. self.argname = argname
  26. def validate(self, options):
  27. if self.argname not in options.all_argument_names:
  28. raise ImproperlyConfigured(
  29. "Invalid block definition, %r not a valid argument name, "
  30. "available argument names: %r" % (self.argname,
  31. options.all_argument_names)
  32. )
  33. def collect(self, parser):
  34. value = parser.kwargs[self.argname]
  35. return self.template % {'value': value.literal}