import datetime from django.utils import translation from django.core.cache import cache from hvad.models import TranslationManager class ThemeActiveManager(TranslationManager): """ manager works only with active themes models where theme defines activity: - exposition """ cache_time = 600 def all(self): lang = translation.get_language() qs = super(ThemeActiveManager, self).select_related('exposition_themes').\ filter(exposition_themes__theme__isnull=False, translations__language_code=lang, )\ .order_by('translations__name').distinct() return qs def expo_main_page(self): Model = self.model return list(self.language().filter(types=Model.types.exposition)[:6]) def conference_main_page(self): Model = self.model return list(self.language().filter(types=Model.types.conference)[:6]) def expo_themes(self): lang = translation.get_language() key = 'used_expo_themes_%s'%lang cached_themes = cache.get(key) if cached_themes: return cached_themes else: from exposition.models import Exposition themes_id = [item['theme_id'] for item in Exposition.objects.values('theme').distinct()] themes = list(self.language().filter(id__in=themes_id)) cache.set(key, themes, 300) return themes """ def expo_themes_with_count(self): lang = translation.get_language() key = 'used_expo_themes_count_%s'%lang cached_themes = cache.get(key) if cached_themes: return cached_themes else: from exposition.models import Exposition sql = {'expo_count': 'SELECT COUNT(*) FROM exposition_exposition_theme WHERE exposition_exposition_theme.theme_id = theme_theme.id'} # id of unique cities themes_id = [item['theme'] for item in Exposition.objects.values('theme').distinct()] themes = list(self.language().filter(id__in=themes_id).extra(select=sql)) cache.set(key, themes, 300) return themes """ def expo_themes_with_count(self): lang = translation.get_language() key = 'used_expo_themes_count_%s'%lang cached_themes = cache.get(key) if cached_themes: return cached_themes else: from exposition.models import Exposition now = datetime.datetime.now().date() # id of unique cities themes_id = [item['theme'] for item in Exposition.objects.filter(is_published=True, data_end__gte=now).values('theme').distinct()] #themes = set(list(self.language().filter(id__in=themes_id).extra(select=sql))) themes = set(list(self.language().filter(id__in=themes_id))) themes = sorted(themes, key=lambda x: x.name) cache.set(key, themes, self.cache_time) return themes def conference_themes_with_count(self): lang = translation.get_language() key = 'used_conference_themes_count_%s'%lang cached_themes = cache.get(key) if cached_themes: return cached_themes else: from conference.models import Conference now = datetime.datetime.now().date() # id of unique cities themes_id = [item['theme'] for item in Conference.objects.filter(is_published=True, data_end__gte=now).values('theme').distinct()] #themes = set(list(self.language().filter(id__in=themes_id).extra(select=sql))) themes = set(list(self.language().filter(id__in=themes_id))) themes = sorted(themes, key=lambda x: x.name) cache.set(key, themes, self.cache_time) return themes class TagActiveManager(TranslationManager): """ manager works only with active themes models where theme defines activity: - exposition """ cache_time = 600 def all(self): lang = translation.get_language() qs = super(TagActiveManager, self).select_related('exposition_tags').\ filter(exposition_tags__tag__isnull=False, translations__language_code=lang, )\ .order_by('translations__name').distinct() return qs def expo_tag(self): lang = translation.get_language() key = 'used_expo_tags_%s'%lang cached_tags = cache.get(key) if cached_tags: return cached_tags else: from exposition.models import Exposition tag_id = [item['tag'] for item in Exposition.enable.upcoming().values('tag').distinct()] tags = list(self.language().filter(id__in=tag_id)) cache.set(key, tags, 300) return tags def conference_tags(self): lang = translation.get_language() key = 'used_conference_tags_count_%s'%lang cached_tags = cache.get(key) if cached_tags: return cached_tags else: from conference.models import Conference now = datetime.datetime.now().date() # id of unique cities tag_id = [item['tag'] for item in Conference.objects.filter(is_published=True, data_end__gte=now).values('tag').distinct()] #themes = set(list(self.language().filter(id__in=themes_id).extra(select=sql))) tags = set(list(self.language().filter(id__in=tag_id))) tags = sorted(tags, key=lambda x: x.name) cache.set(key, tags, self.cache_time) return tags