|
|
|
|
@ -2,6 +2,7 @@ |
|
|
|
|
from datetime import date |
|
|
|
|
from django.db import models |
|
|
|
|
from django.utils import translation |
|
|
|
|
from django.utils.translation import ugettext_lazy as _ |
|
|
|
|
from django.contrib.contenttypes import generic |
|
|
|
|
from django.db.models.signals import post_save, pre_save |
|
|
|
|
from hvad.models import TranslatableModel, TranslatedFields |
|
|
|
|
@ -9,26 +10,28 @@ from bitfield import BitField |
|
|
|
|
from manager import CountryManager, AreaManager |
|
|
|
|
from directories.models import Language, Currency |
|
|
|
|
from city.models import City |
|
|
|
|
from service.models import Service |
|
|
|
|
from exposition.models import Exposition |
|
|
|
|
from place_exposition.models import PlaceExposition |
|
|
|
|
from organiser.models import Organiser |
|
|
|
|
from conference.models import Conference |
|
|
|
|
from seminar.models import Seminar |
|
|
|
|
from webinar.models import Webinar |
|
|
|
|
from functions.db import db_table_exists |
|
|
|
|
from functions.signal_handlers import post_save_handler, pre_save_handler |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from service.models import Service |
|
|
|
|
from functions.db import db_table_exists |
|
|
|
|
# check if table exist and create flags if true |
|
|
|
|
#flags = [str(item.url) for item in Service.objects.all()] if db_table_exists('service_service') else [] |
|
|
|
|
flags = ['catalog', 'translator', 'participation', 'remote', 'tickets', 'visit', 'buildstand'] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Area(TranslatableModel): |
|
|
|
|
""" |
|
|
|
|
Store information about geographical zones |
|
|
|
|
""" |
|
|
|
|
translations = TranslatedFields( |
|
|
|
|
name = models.CharField(max_length=255), |
|
|
|
|
name=models.CharField(verbose_name=_(u'Название'), max_length=255), |
|
|
|
|
) |
|
|
|
|
objects = AreaManager() |
|
|
|
|
|
|
|
|
|
@ -39,23 +42,40 @@ class Area(TranslatableModel): |
|
|
|
|
return self.lazy_translation_getter('name', unicode(self.pk)) |
|
|
|
|
|
|
|
|
|
def countries(self): |
|
|
|
|
""" |
|
|
|
|
returns countries of current area |
|
|
|
|
""" |
|
|
|
|
lang = translation.get_language() |
|
|
|
|
return Country.objects.select_related('exposition_country')\ |
|
|
|
|
.filter(exposition_country__country__isnull=False, translations__language_code=lang, area=self).distinct().order_by('translations__name') |
|
|
|
|
.filter(exposition_country__country__isnull=False, translations__language_code=lang, area=self)\ |
|
|
|
|
.distinct().order_by('translations__name') |
|
|
|
|
|
|
|
|
|
def expos(self): |
|
|
|
|
""" |
|
|
|
|
return expos that occur in current area |
|
|
|
|
""" |
|
|
|
|
countries = self.countries() |
|
|
|
|
return Exposition.objects.filter(country__in=countries) |
|
|
|
|
|
|
|
|
|
def get_sub_categories(self): |
|
|
|
|
objects = [{'text':item.name, 'id':item.id, 'name':'co', 'sub':True} for item in self.countries()] |
|
|
|
|
""" |
|
|
|
|
returns list with countries data that connected to current area |
|
|
|
|
uses in search |
|
|
|
|
""" |
|
|
|
|
objects = [{'text': item.name, 'id': item.id, 'name': 'co', 'sub': True} for item in self.countries()] |
|
|
|
|
return objects |
|
|
|
|
|
|
|
|
|
def get_parent(self): |
|
|
|
|
parent = {} |
|
|
|
|
return parent |
|
|
|
|
""" |
|
|
|
|
returns empty dict, cause area has no parents |
|
|
|
|
uses in search |
|
|
|
|
""" |
|
|
|
|
return {} |
|
|
|
|
|
|
|
|
|
def get_index_text(self): |
|
|
|
|
""" |
|
|
|
|
returns string of names in all languages for indexing it in search engine |
|
|
|
|
""" |
|
|
|
|
translation.activate('ru') |
|
|
|
|
translations = self.translations.all() |
|
|
|
|
names = ' '.join([tr.name for tr in translations]) |
|
|
|
|
@ -64,51 +84,46 @@ class Area(TranslatableModel): |
|
|
|
|
|
|
|
|
|
class Country(TranslatableModel): |
|
|
|
|
""" |
|
|
|
|
Create Country model |
|
|
|
|
|
|
|
|
|
Uses hvad.TranslatableModel which is child of django.db.models class |
|
|
|
|
|
|
|
|
|
Stores information about countries |
|
|
|
|
area- parent, city - child in search |
|
|
|
|
""" |
|
|
|
|
objects = CountryManager() |
|
|
|
|
catalog = '/country/' |
|
|
|
|
services = BitField(flags=flags) |
|
|
|
|
url = models.SlugField(unique=True) |
|
|
|
|
old_url = models.CharField(unique=True, max_length=55) |
|
|
|
|
inflect = models.CharField(max_length=255, blank=True) |
|
|
|
|
# relations |
|
|
|
|
area = models.ForeignKey(Area) |
|
|
|
|
big_cities = models.ManyToManyField(City, blank=True, null=True, related_name='cities') |
|
|
|
|
capital = models.ForeignKey(City,blank=True, null=True, on_delete=models.PROTECT, related_name='capital') |
|
|
|
|
url = models.SlugField(verbose_name=_(u'Url'), unique=True) |
|
|
|
|
old_url = models.CharField(verbose_name=_(u'Старый урл'), unique=True, max_length=55) |
|
|
|
|
# inflect name for russian language. example- в Росии |
|
|
|
|
inflect = models.CharField(verbose_name=_(u'Склонение'), max_length=255, blank=True) |
|
|
|
|
area = models.ForeignKey(Area, verbose_name=_(u'Географическая зона')) |
|
|
|
|
logo = models.ImageField(verbose_name='Logo', upload_to='country/logo/', blank=True, max_length=255) |
|
|
|
|
big_cities = models.ManyToManyField(City, verbose_name=_(u'Большые города'), blank=True, null=True, related_name='cities') |
|
|
|
|
capital = models.ForeignKey(City, verbose_name=_(u'Столица'), blank=True, null=True, on_delete=models.PROTECT, related_name='capital') |
|
|
|
|
language = models.ManyToManyField(Language, blank=True, null=True) |
|
|
|
|
currency = models.ManyToManyField(Currency, blank=True, null=True) |
|
|
|
|
|
|
|
|
|
population = models.PositiveIntegerField(blank=True, null=True) |
|
|
|
|
teritory = models.PositiveIntegerField(blank=True, null=True) |
|
|
|
|
timezone = models.FloatField(blank=True, null=True) |
|
|
|
|
phone_code = models.PositiveIntegerField(blank=True, null=True) |
|
|
|
|
currency = models.ManyToManyField(Currency, verbose_name=_(u'Валюта'), blank=True, null=True) |
|
|
|
|
population = models.PositiveIntegerField(verbose_name=_(u'Население'), blank=True, null=True) |
|
|
|
|
teritory = models.PositiveIntegerField(verbose_name=_(u'Територия'), blank=True, null=True) |
|
|
|
|
timezone = models.FloatField(verbose_name=_(u'Часовой пояс'), blank=True, null=True) |
|
|
|
|
phone_code = models.PositiveIntegerField(verbose_name=_(u'Тел. Код страны'), blank=True, null=True) |
|
|
|
|
time_delivery = models.PositiveSmallIntegerField(blank=True, null=True) |
|
|
|
|
latitude = models.FloatField(blank=True, null=True) |
|
|
|
|
longitude = models.FloatField(blank=True, null=True) |
|
|
|
|
latitude = models.FloatField(verbose_name=_(u'Широта'), blank=True, null=True) |
|
|
|
|
longitude = models.FloatField(verbose_name=_(u'Долгота'), blank=True, null=True) |
|
|
|
|
created = models.DateTimeField(auto_now_add=True) |
|
|
|
|
modified = models.DateTimeField(auto_now=True) |
|
|
|
|
country_code = models.CharField(max_length=2) |
|
|
|
|
country_code = models.CharField(verbose_name=_(u'Код страны(Alpha2)'), max_length=2) |
|
|
|
|
# connection with FileModel by ContentType |
|
|
|
|
files = generic.GenericRelation('file.FileModel',content_type_field='content_type', object_id_field='object_id') |
|
|
|
|
#translated fields |
|
|
|
|
translations = TranslatedFields( |
|
|
|
|
name = models.CharField(max_length=255), |
|
|
|
|
description = models.TextField(blank=True), |
|
|
|
|
transport = models.TextField(blank=True), |
|
|
|
|
#------visa inf |
|
|
|
|
rules = models.TextField(blank=True), |
|
|
|
|
documents = models.TextField(blank=True),#pdf? |
|
|
|
|
consulate = models.TextField(blank=True), |
|
|
|
|
name=models.CharField(max_length=255), |
|
|
|
|
description=models.TextField(blank=True), |
|
|
|
|
transport=models.TextField(blank=True), |
|
|
|
|
rules=models.TextField(blank=True), |
|
|
|
|
documents=models.TextField(blank=True), |
|
|
|
|
consulate=models.TextField(blank=True), |
|
|
|
|
#-----meta data |
|
|
|
|
title = models.CharField(max_length=255), |
|
|
|
|
descriptions = models.CharField(max_length=255), |
|
|
|
|
keywords = models.CharField(max_length=255), |
|
|
|
|
title=models.CharField(max_length=255), |
|
|
|
|
descriptions=models.CharField(max_length=255), |
|
|
|
|
keywords=models.CharField(max_length=255), |
|
|
|
|
) |
|
|
|
|
logo = models.ImageField(verbose_name='Logo', upload_to='country/logo/', blank=True, max_length=255) |
|
|
|
|
|
|
|
|
|
class Meta: |
|
|
|
|
ordering = ['translations__name'] |
|
|
|
|
@ -117,63 +132,52 @@ class Country(TranslatableModel): |
|
|
|
|
return self.lazy_translation_getter('name', unicode(self.pk)) |
|
|
|
|
|
|
|
|
|
def get_events(self): |
|
|
|
|
""" |
|
|
|
|
returns nearest expos in this country |
|
|
|
|
""" |
|
|
|
|
now = date.today() |
|
|
|
|
return Exposition.objects.filter(data_begin__gte=now, country=self).order_by('data_begin')[:3] |
|
|
|
|
|
|
|
|
|
def get_places(self): |
|
|
|
|
return PlaceExposition.objects.filter(country=self)[:3] |
|
|
|
|
|
|
|
|
|
def get_organisers(self): |
|
|
|
|
return Organiser.objects.filter(country=self) |
|
|
|
|
|
|
|
|
|
def get_permanent_url(self): |
|
|
|
|
return self.catalog+self.url |
|
|
|
|
|
|
|
|
|
def events_catalog(self): |
|
|
|
|
return Exposition.catalog+'country-%s'%self.url |
|
|
|
|
|
|
|
|
|
def places_catalog(self): |
|
|
|
|
return PlaceExposition.catalog+'country-%s'%self.url |
|
|
|
|
|
|
|
|
|
def expositions_number(self): |
|
|
|
|
|
|
|
|
|
return len(Exposition.objects.filter(country=self.id)) |
|
|
|
|
return Exposition.objects.filter(country=self).count() |
|
|
|
|
|
|
|
|
|
def conferences_number(self): |
|
|
|
|
|
|
|
|
|
return Conference.objects.filter(country=self.id).count() |
|
|
|
|
return Conference.objects.filter(country=self).count() |
|
|
|
|
|
|
|
|
|
def seminars_number(self): |
|
|
|
|
|
|
|
|
|
return Seminar.objects.filter(country=self.id).count() |
|
|
|
|
return Seminar.objects.filter(country=self).count() |
|
|
|
|
|
|
|
|
|
def webinars_number(self): |
|
|
|
|
|
|
|
|
|
return Webinar.objects.filter(country=self.id).count() |
|
|
|
|
return Webinar.objects.filter(country=self).count() |
|
|
|
|
|
|
|
|
|
def active_cities(self): |
|
|
|
|
result = list(set(City.used.active_qs().filter(country=self))) |
|
|
|
|
result.sort(key=lambda x:x.name) |
|
|
|
|
result.sort(key=lambda x: x.name) |
|
|
|
|
return result |
|
|
|
|
lang = translation.get_language() |
|
|
|
|
#return City.objects.select_related('exposition_city')\ |
|
|
|
|
# .filter(exposition_city__city__isnull=False, translations__language_code=lang, country=self).distinct().order_by('translations__name') |
|
|
|
|
|
|
|
|
|
def get_sub_categories(self): |
|
|
|
|
objects = [{'text':item.name, 'id':item.id, 'name':'ci', 'sub': False} for item in self.active_cities()] |
|
|
|
|
objects = [{'text': item.name, 'id': item.id, 'name': 'ci', 'sub': False} for item in self.active_cities()] |
|
|
|
|
return objects |
|
|
|
|
|
|
|
|
|
def get_parent(self): |
|
|
|
|
parent = {'text' : self.area.name, 'id': self.area.id, 'name': 'area'} |
|
|
|
|
parent = {'text': self.area.name, 'id': self.area.id, 'name': 'area'} |
|
|
|
|
return parent |
|
|
|
|
|
|
|
|
|
def get_index_text(self): |
|
|
|
|
""" |
|
|
|
|
returns string of names in all languages for indexing it in search engine |
|
|
|
|
""" |
|
|
|
|
translation.activate('ru') |
|
|
|
|
translations = self.translations.all() |
|
|
|
|
names = ' '.join([tr.name for tr in translations]) |
|
|
|
|
return names |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pre_save.connect(pre_save_handler, sender=Country) |
|
|
|
|
post_save.connect(post_save_handler, sender=Country) |