parent
09e78a6d38
commit
a5c19b3549
21 changed files with 526 additions and 256 deletions
@ -0,0 +1,105 @@ |
|||||||
|
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 |
||||||
@ -0,0 +1,106 @@ |
|||||||
|
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 |
||||||
|
""" |
||||||
|
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 |
||||||
|
|
||||||
Loading…
Reference in new issue