theme.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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 active_theme():
  18. try:
  19. return _thread_local.misago_theme
  20. except AttributeError:
  21. return None
  22. def prefix_templates(templates, dictionary=None):
  23. templates = process_templates(templates, dictionary)
  24. if isinstance(templates, str):
  25. return ('%s/%s' % (_thread_local.misago_theme, templates), templates)
  26. else:
  27. prefixed = []
  28. for template in templates:
  29. prefixed.append('%s/%s' % (_thread_local.misago_theme, template))
  30. prefixed += templates
  31. return tuple(prefixed)