from django.conf import settings from misago.template.middlewares import process_templates from misago.thread import local __all__ = ('activate_theme', 'active_theme', 'prefix_templates') _thread_local = local() def activate_theme(theme): """ Activate theme in current thread """ if theme not in settings.INSTALLED_THEMES: raise ValueError('"%s" is not correct theme name.' % theme) if theme[0] == '_': raise ValueError('"%s" is a template package, not a theme.' % theme[1:]) _thread_local.theme = theme _thread_local.template_mutex = False def reset_theme(): _thread_local.theme = settings.INSTALLED_THEMES[0] def active_theme(): try: return _thread_local.theme except AttributeError:
from django.core.cache import cache from django.db import models from django.utils.datastructures import SortedDict from misago.thread import local _thread_local = local() class WarnLevelManager(models.Manager): def get_levels(self): try: return _thread_local._misago_warning_levels except AttributeError: _thread_local._misago_warning_levels = self.fetch_levels() return _thread_local._misago_warning_levels def get_level(self, level): return self.get_levels().get(level) def fetch_levels(self): from_cache = cache.get('warning_levels', 'nada') if from_cache != 'nada': return from_cache from_db = self.fetch_levels_from_db() cache.set('warning_levels', from_db) return from_db def fetch_levels_from_db(self): fetched_levels = SortedDict() for level in self.order_by('warning_level').iterator(): fetched_levels[level.warning_level] = level