# -*- coding: utf-8 -*- 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 from bitfield import BitField from manager import CountryManager, AreaManager from directories.models import Language, Currency from city.models import City 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(verbose_name=_(u'Название'), max_length=255), ) objects = AreaManager() class Meta: ordering = ['translations__name'] def __unicode__(self): 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') def expos(self): """ return expos that occur in current area """ from exposition.models import Exposition countries = self.countries() return Exposition.objects.filter(country__in=countries) def get_sub_categories(self): """ 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): """ 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]) return names class Country(TranslatableModel): """ Stores information about countries area- parent, city - child in search """ objects = CountryManager() catalog = '/country/' services = BitField(flags=flags) 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=_(u'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, 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(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(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') translations = TranslatedFields( 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), ) class Meta: ordering = ['translations__name'] def __unicode__(self): return self.lazy_translation_getter('name', unicode(self.pk)) def get_events(self): """ returns nearest expos in this country """ now = date.today() from exposition.models import Exposition return Exposition.objects.filter(data_begin__gte=now, country=self).order_by('data_begin')[:3] def get_places(self): from place_exposition.models import PlaceExposition return PlaceExposition.objects.filter(country=self)[:3] def get_permanent_url(self): return self.catalog+self.url def expositions_number(self): from exposition.models import Exposition return Exposition.objects.filter(country=self).count() def conferences_number(self): from conference.models import Conference return Conference.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) return result def get_sub_categories(self): 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'} 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)