import datetime from django.utils import translation from django.core.cache import cache from django.utils.translation import get_language as lang from hvad.models import TranslationManager class CountryManager(TranslationManager): cache_time = 600 def all(self): """ hack """ return super(TranslationManager, self).all().filter(translations__language_code=lang()).order_by('translations__name') def safe_get(self, **kwargs): model = self.model try: return model.objects.get(**kwargs) except: return None def expo_countries(self): lang = translation.get_language() key = 'used_expo_countries_%s'%lang cached_countries = cache.get(key) if cached_countries: return cached_countries else: from exposition.models import Exposition countries_id = [item['country_id'] for item in Exposition.objects.values('country_id').distinct()] countries = list(self.language().filter(id__in=countries_id)) cache.set(key, countries, self.cache_time) return countries def expo_countries_with_count(self): lang = translation.get_language() key = 'used_expo_countries_count_%s'%lang cached_countries = cache.get(key) if cached_countries: return cached_countries else: from exposition.models import Exposition sql = {'expo_count': """SELECT COUNT(*) FROM exposition_exposition WHERE exposition_exposition.country_id = country_country.id AND exposition_exposition.data_end >= CURDATE() AND exposition_exposition.is_published = 1"""} now = datetime.datetime.now().date() # id of unique countries countries_id = [item['country_id'] for item in Exposition.objects.filter(is_published=True, data_end__gte=now).values('country_id').distinct()] countries = set(list(self.language().filter(id__in=countries_id).extra(select=sql))) countries = sorted(countries, key=lambda x: x.name) cache.set(key, countries, self.cache_time) return countries def conference_countries_with_count(self): lang = translation.get_language() key = 'used_conference_countries_count_%s'%lang cached_countries = cache.get(key) if cached_countries: return cached_countries else: from conference.models import Conference sql = {'conference_count': """SELECT COUNT(*) FROM conference_conference WHERE conference_conference.country_id = country_country.id AND conference_conference.data_end >= CURDATE() AND conference_conference.is_published = 1"""} now = datetime.datetime.now().date() # id of unique countries countries_id = [item['country_id'] for item in Conference.objects.filter(is_published=True, data_end__gte=now).values('country_id').distinct()] countries = set(list(self.language().filter(id__in=countries_id).extra(select=sql))) countries = sorted(countries, key=lambda x: x.name) cache.set(key, countries, self.cache_time) return countries def conference_countries(self): lang = translation.get_language() key = 'used_conference_countries_%s'%lang cached_countries = cache.get(key) if cached_countries: return cached_countries else: from conference.models import Conference countries_id = [item['country_id'] for item in Conference.objects.values('country_id').distinct()] countries = list(self.language().filter(id__in=countries_id)) cache.set(key, countries, self.cache_time) return countries class AreaManager(TranslationManager): def all_sorted(self): """ return list, not queryset """ model = self.model result = list(model.objects.filter()) result.sort(key=lambda x: len(x.expos()), reverse=True) return result