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.
 
 
 
 
 
 

179 lines
6.5 KiB

# -*- coding: utf-8 -*-
from datetime import date
from django.db import models
from django.utils import translation
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 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
# 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):
translations = TranslatedFields(
name = models.CharField(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):
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):
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()]
return objects
def get_parent(self):
parent = {}
return parent
def get_index_text(self):
translation.activate('ru')
translations = self.translations.all()
names = ' '.join([tr.name for tr in translations])
return names
class Country(TranslatableModel):
"""
Create Country model
Uses hvad.TranslatableModel which is child of django.db.models class
"""
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')
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)
time_delivery = models.PositiveSmallIntegerField(blank=True, null=True)
latitude = models.FloatField(blank=True, null=True)
longitude = models.FloatField(blank=True, null=True)
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
country_code = models.CharField(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),
#-----meta data
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']
def __unicode__(self):
return self.lazy_translation_getter('name', unicode(self.pk))
def get_events(self):
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))
def conferences_number(self):
return Conference.objects.filter(country=self.id).count()
def seminars_number(self):
return Seminar.objects.filter(country=self.id).count()
def webinars_number(self):
return Webinar.objects.filter(country=self.id).count()
def active_cities(self):
result = list(set(City.used.active_qs().filter(country=self)))
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()]
return objects
def get_parent(self):
parent = {'text' : self.area.name, 'id': self.area.id, 'name': 'area'}
return parent
def get_index_text(self):
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)