You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
139 lines
5.8 KiB
139 lines
5.8 KiB
import datetime
|
|
from django.utils import translation
|
|
from django.core.cache import cache
|
|
from hvad.models import TranslationManager
|
|
|
|
|
|
class CountryManager(TranslationManager):
|
|
cache_time = 600
|
|
|
|
def safe_get(self, **kwargs):
|
|
model = self.model
|
|
try:
|
|
return model.objects.get(**kwargs)
|
|
except:
|
|
return None
|
|
|
|
def expo_countries(self, cached=True):
|
|
lang = translation.get_language()
|
|
key = 'used_expo_countries_%s'%lang
|
|
cached_countries = cache.get(key)
|
|
if cached and 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, cached=True):
|
|
lang = translation.get_language()
|
|
key = 'used_expo_countries_count_%s'%lang
|
|
cached_countries = cache.get(key)
|
|
if cached and 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 place_countries_with_count(self, cached=True):
|
|
lang = translation.get_language()
|
|
key = 'used_place_countries_count_%s'%lang
|
|
cached_countries = cache.get(key)
|
|
if cached and cached_countries:
|
|
return cached_countries
|
|
else:
|
|
|
|
from place_exposition.models import PlaceExposition
|
|
sql = {'place_count':
|
|
"""SELECT COUNT(*)
|
|
FROM place_exposition_placeexposition
|
|
WHERE place_exposition_placeexposition.country_id = country_country.id
|
|
AND place_exposition_placeexposition.is_published = 1"""}
|
|
now = datetime.datetime.now().date()
|
|
# id of unique countries
|
|
countries_id = set(
|
|
PlaceExposition.objects\
|
|
.filter(is_published=True)\
|
|
.values_list('country_id', flat=True)
|
|
)
|
|
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, cached=True):
|
|
lang = translation.get_language()
|
|
key = 'used_conference_countries_count_%s'%lang
|
|
cached_countries = cache.get(key)
|
|
if cached and 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, cached=True):
|
|
lang = translation.get_language()
|
|
key = 'used_conference_countries_%s'%lang
|
|
cached_countries = cache.get(key)
|
|
if cached and 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
|
|
|
|
def countries_for_search(self):
|
|
lang = translation.get_language()
|
|
qs = super(CountryManager, self).select_related('exposition_country').\
|
|
filter(exposition_country__country__isnull=False, translations__language_code=lang, )\
|
|
.order_by('translations__name').distinct()
|
|
return qs
|
|
|
|
|
|
class AreaManager(TranslationManager):
|
|
cache_time = 55
|
|
|
|
def all_sorted(self, cached=True):
|
|
"""
|
|
return list, not queryset
|
|
"""
|
|
lang = translation.get_language()
|
|
key = 'country_area_all_%s' % lang
|
|
if cached and key in cache:
|
|
result = cache.get(key)
|
|
else:
|
|
model = self.model
|
|
result = list(model.objects.language().filter())
|
|
#result.sort(key=lambda x: len(x.expos()), reverse=True)
|
|
cache.set(key, result, self.cache_time)
|
|
return result
|
|
|