theme.py 1.3 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. print _thread_local.__dict__.keys()
  15. _thread_local.misago_theme = theme;
  16. _thread_local.misago_template_mutex = False
  17. def reset_theme():
  18. _thread_local.misago_theme = settings.INSTALLED_THEMES[0];
  19. def active_theme():
  20. try:
  21. return _thread_local.misago_theme
  22. except AttributeError:
  23. return None
  24. def prefix_templates(templates, dictionary=None):
  25. templates = process_templates(templates, dictionary)
  26. if isinstance(templates, str):
  27. return ('%s/%s' % (_thread_local.misago_theme, templates), templates)
  28. else:
  29. prefixed = []
  30. for template in templates:
  31. prefixed.append('%s/%s' % (_thread_local.misago_theme, template))
  32. prefixed += templates
  33. return tuple(prefixed)