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.
138 lines
4.4 KiB
138 lines
4.4 KiB
# -*- coding: utf-8 -*-
|
|
from datetime import date
|
|
from django.db import models
|
|
from django.db.models.signals import post_save, pre_save
|
|
from hvad.models import TranslatableModel, TranslatedFields, TranslationManager
|
|
from bitfield import BitField
|
|
from sorl.thumbnail import ImageField
|
|
# models
|
|
from directories.models import Iata
|
|
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
|
|
# custom functions
|
|
from functions.db import db_table_exists
|
|
from functions.signal_handlers import post_save_handler
|
|
from functions.models_methods import ExpoManager, CityManager
|
|
|
|
#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 City(TranslatableModel):
|
|
"""
|
|
Create City model
|
|
|
|
Uses hvad.TranslatableModel which is child of django.db.models class
|
|
|
|
"""
|
|
objects = ExpoManager()
|
|
used = CityManager()
|
|
catalog = '/city/'
|
|
|
|
services = BitField(flags=flags)
|
|
|
|
url = models.SlugField(unique=True)
|
|
#
|
|
old_url = models.CharField(max_length=55)
|
|
inflect = models.CharField(max_length=255, blank=True)
|
|
#relations
|
|
country = models.ForeignKey('country.Country', null=True, on_delete=models.PROTECT, related_name='cities')
|
|
code_IATA = models.ForeignKey(Iata, blank=True, null=True)
|
|
|
|
population = models.PositiveIntegerField(blank=True, null=True)
|
|
phone_code = models.PositiveIntegerField(blank=True, null=True)
|
|
#translated fields
|
|
translations = TranslatedFields(
|
|
name = models.CharField(max_length=50),
|
|
region = models.CharField(max_length=255),
|
|
transport = models.TextField(blank=True),
|
|
description = models.TextField(blank=True),
|
|
famous_places = models.TextField(blank=True),
|
|
shoping = models.TextField(blank=True),
|
|
#-----meta
|
|
title = models.CharField(max_length=255),
|
|
descriptions = models.CharField(max_length=255),
|
|
keywords = models.CharField(max_length=255),
|
|
)
|
|
# fields saves information about creating and changing model
|
|
created = models.DateTimeField(auto_now_add=True)
|
|
modified = models.DateTimeField(auto_now=True)
|
|
|
|
class Meta:
|
|
ordering = ['translations__name']
|
|
|
|
def __unicode__(self):
|
|
return self.lazy_translation_getter('name', self.pk)
|
|
|
|
def get_hotels(self):
|
|
return self.hotels.all()[:4]
|
|
|
|
def get_events(self):
|
|
now = date.today()
|
|
return Exposition.objects.filter(data_begin__gte=now, city=self).order_by('data_begin')[:3]
|
|
|
|
def get_places(self):
|
|
return PlaceExposition.objects.filter(city=self)[:3]
|
|
|
|
def get_organisers(self):
|
|
return Organiser.objects.filter(city=self)
|
|
|
|
def get_permanent_url(self):
|
|
return self.catalog+self.url
|
|
|
|
def events_catalog(self):
|
|
return Exposition.catalog+'city-%s'%self.url
|
|
|
|
def places_catalog(self):
|
|
return PlaceExposition.catalog+'city-%s'%self.url
|
|
|
|
def expositions_number(self):
|
|
|
|
return Exposition.objects.filter(city=self.id).count()
|
|
|
|
def conferences_number(self):
|
|
|
|
return Conference.objects.filter(city=self.id).count()
|
|
|
|
def seminars_number(self):
|
|
|
|
return Seminar.objects.filter(city=self.id).count()
|
|
|
|
def webinars_number(self):
|
|
|
|
return Webinar.objects.filter(city=self.id).count()
|
|
|
|
def get_parent(self):
|
|
parent = {'text' : self.country.name, 'id': self.country.id, 'name': 'co',
|
|
'parent':{'text' : self.country.area.name, 'id': self.country.area.id, 'name': 'area'}}
|
|
return parent
|
|
|
|
def get_sub_categories(self):
|
|
return []
|
|
|
|
|
|
|
|
|
|
|
|
class Hotel(TranslatableModel):
|
|
url = models.URLField(max_length=255)
|
|
city = models.ForeignKey(City, related_name='hotels')
|
|
|
|
ranking = models.FloatField(blank=True, null=True)
|
|
hotel_class = models.CharField(max_length=10, blank=True)
|
|
latitude = models.FloatField(blank=True, null=True)
|
|
longitude = models.FloatField(blank=True, null=True)
|
|
photo = ImageField(upload_to='hotels')
|
|
|
|
translations = TranslatedFields(
|
|
name = models.CharField(max_length=255, blank=True),
|
|
address = models.CharField(max_length=255, blank=True)
|
|
)
|
|
|
|
post_save.connect(post_save_handler, sender=City)
|
|
post_save.connect(post_save_handler, sender=Hotel) |