theme.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. from django.conf import settings
  2. from misago.template.middlewares import process_templates
  3. from misago.thread import local
  4. __all__ = ('activate_theme', 'active_theme', 'prefix_templates')
  5. _thread_local = local()
  6. def activate_theme(theme):
  7. """
  8. Activate theme in current thread
  9. """
  10. if theme not in settings.INSTALLED_THEMES:
  11. raise ValueError('"%s" is not correct theme name.' % theme)
  12. if theme[0] == '_':
  13. raise ValueError('"%s" is a template package, not a theme.' % theme[1:])
  14. _thread_local.theme = theme;
  15. _thread_local.template_mutex = False
  16. def reset_theme():
  17. _thread_local.theme = settings.INSTALLED_THEMES[0];
  18. def active_theme():
  19. try:
  20. return _thread_local.theme
  21. except AttributeError:
  22. return None
  23. def prefix_templates(templates, dictionary=None):
  24. templates = process_templates(templates, dictionary)
  25. if isinstance(templates, str):
  26. return ('%s/%s' % (_thread_local.theme, templates), templates)
  27. else:
  28. prefixed = []
  29. for template in templates:
  30. prefixed.append('%s/%s' % (_thread_local.theme, template))
  31. prefixed += templates
  32. return tuple(prefixed)