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.
170 lines
5.8 KiB
170 lines
5.8 KiB
# -*- coding: utf-8 -*-
|
|
from datetime import date
|
|
from django.db import models
|
|
from django.utils.translation import ugettext as _
|
|
from django.contrib.contenttypes import generic
|
|
from django.db.models.signals import post_save, pre_save
|
|
from hvad.models import TranslatableModel, TranslatedFields, TranslationManager
|
|
from bitfield import BitField
|
|
# models
|
|
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
|
|
# func
|
|
from functions.custom_fields import EnumField
|
|
from functions.db import db_table_exists
|
|
from functions.signal_handlers import post_save_handler, pre_save_handler
|
|
from django.utils.translation import get_language as lang
|
|
from django.utils import translation
|
|
|
|
# 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 []
|
|
|
|
class CountryManager(TranslationManager):
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
class Area(TranslatableModel):
|
|
translations = TranslatedFields(
|
|
name = models.CharField(max_length=255),
|
|
)
|
|
objects = AreaManager()
|
|
|
|
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()
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
# 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)
|
|
|
|
# fields saves information about creating and changing model
|
|
created = models.DateTimeField(auto_now_add=True)
|
|
modified = models.DateTimeField(auto_now=True)
|
|
# 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),
|
|
|
|
)
|
|
country_code = models.CharField(max_length=2)
|
|
|
|
# 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 active_cities(self):
|
|
lang = translation.get_language()
|
|
return City.objects.select_related('exposition_city')\
|
|
.filter(exposition_city__city__isnull=False, translations__language_code=lang, country=self).distinct()
|
|
|
|
def get_sub_categories(self):
|
|
objects = [{'text':item.name, 'id':item.id, 'name':'ci', 'sub': False} for item in self.active_cities()]
|
|
return objects
|
|
|
|
|
|
|
|
post_save.connect(post_save_handler, sender=Country) |