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.
 
 
 
 
 
 

291 lines
11 KiB

# -*- coding: utf-8 -*-
from django.db import models
from django.db.models import Q
from django.contrib.contenttypes import generic
from django.db.models.signals import post_save, pre_save
from django.utils.translation import ugettext as _
from functools import partial
from django.conf import settings
from functions.translate import fill_with_signal
#
from hvad.models import TranslatableModel, TranslatedFields, TranslationManager
#
from functions.custom_fields import EnumField, LocationField
from functions.signal_handlers import post_save_handler, pre_save_handler
from functions.models_methods import ExpoManager
import copy
from functions.model_mixin import ExpoMixin
from exposition.models import Exposition
from place_conference.models import PlaceConference
from photologue.models import Gallery
EXPOSITION_TYPE = (('Exposition complex', u'Выставочный комплекс'), ('Convention centre', u'Конгрессно-выставочный центр'),
('Exposition centre', u'Выставочный центр'),)
dist=lambda s,d: (s[0]-d[0])**2+(s[1]-d[1])**2
def logo_name(instance, filename):
url = instance.url
return '/'.join(['place_exposition', url, url+'_logo.jpg'])
class PlaceExposition(TranslatableModel, ExpoMixin):
"""
Create PlaceConference model
Uses hvad.TranslatableModel which is child of django.db.models class
"""
catalog = '/places/'
place = 'place_exposition'
catalog_name = _(u'Места:')
search_name = None
#set manager of this model
objects = ExpoManager()
url = models.SlugField(unique=True, max_length=255)
country = models.ForeignKey('country.Country', on_delete=models.PROTECT)
city = models.ForeignKey('city.City', on_delete=models.PROTECT, related_name='place_expositions')
photogallery = models.ForeignKey('photologue.Gallery', null=True, blank=True, on_delete=models.SET_NULL)
#type uses EnumField for creating Enum type field in Mysql database
type = EnumField(values = [item1 for item1, item2 in EXPOSITION_TYPE])
#information
address = LocationField(verbose_name='Адресс')
phone = models.BigIntegerField(blank=True, null=True)
fax = models.BigIntegerField(blank=True, null=True)
web_page = models.URLField(blank=True)
email = models.EmailField(blank=True)
foundation_year = models.PositiveIntegerField(blank=True, null=True)
event_in_year = models.PositiveIntegerField(blank=True, null=True)
total_area = models.PositiveIntegerField(blank=True, null=True)
closed_area = models.PositiveIntegerField(blank=True, null=True)
open_area = models.PositiveIntegerField(blank=True, null=True)
total_pavilions = models.PositiveIntegerField(blank=True, null=True)
total_halls = models.PositiveIntegerField(blank=True, null=True)
virtual_tour = models.URLField(blank=True)
#
wifi = models.NullBooleanField()
bank = models.NullBooleanField()
children_room = models.NullBooleanField()
disabled_service = models.NullBooleanField()
conference_centre = models.NullBooleanField() #change name here, in form, in template
business_centre = models.NullBooleanField()
online_registration = models.NullBooleanField()
cafe = models.NullBooleanField()
terminals = models.NullBooleanField()
parking = models.NullBooleanField()
press_centre = models.NullBooleanField()
mobile_application = models.NullBooleanField()
#
logo = models.ImageField(verbose_name='Logo', upload_to=logo_name, blank=True, max_length=255)
rating = models.IntegerField(default=0)
partner = models.NullBooleanField(default=0)
# delete after profiling
files = generic.GenericRelation('file.FileModel', content_type_field='content_type', object_id_field='object_id')
#photos = generic.GenericRelation('file.Photo', content_type_field='content_type', object_id_field='object_id')
#translations is translated fields
translations = TranslatedFields(
name = models.CharField(max_length=100),
main_title = models.TextField(blank=True),
description = models.TextField(blank=True),
# text information about address
adress = models.TextField(blank=True),
#-----meta data
title = models.CharField(max_length=250),
descriptions = models.CharField(max_length=250),
keywords = models.CharField(max_length=250),
#
total_year_action = models.TextField(blank=True),
)
#fields saves information about creating and changing model
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
views = models.PositiveIntegerField(default=0)
is_published = models.BooleanField(default=1)
class Meta:
ordering = ['-rating', 'id']
def get_gallery(self):
if self.photogallery:
return self.photogallery
data = {}
model = type(self)
for code, name in settings.LANGUAGES:
obj = model._meta.translations_model.objects.get(language_code = code,master__id=self.id) #access to translated fields
data['title_%s'%code] = obj.name
data['description_%s'%code] = obj.description
gallery = Gallery()
fill_with_signal(Gallery, gallery, data)
self.photogallery = gallery
self.save()
return gallery
def upload_photo(self, photo ,gallery=None):
"""
uploading photo to gallery
"""
if gallery is None:
gallery = self.get_gallery()
gallery.photos.add(photo)
def upload_photo_url(self):
return '/admin/place_exposition/upload-photo/%s/'%self.id
def get_index_text(self):
translations = self.translations.all()
names = ' '.join([tr.name for tr in translations])
titles = ' '.join([tr.main_title for tr in translations])
return names + ' ' + titles
def __unicode__(self):
return self.lazy_translation_getter('name', unicode(self.pk))
def get_object_type(self):
return _(u'Места')
def get_catalog_url(self):
return self.catalog
def get_permanent_url(self):
url = '%s%s/'%(self.catalog, self.url)
return url
def get_logo(self):
return self.logo
def get_photos(self):
photos = self.photos.all()
ph = self.files.filter(purpose='photo')
return list(photos)+list(ph)
def get_scheme(self):
scheme = self.files.filter(purpose='scheme teritory')
return scheme
def events(self):
events = Exposition.objects.filter(place=self)[:6]
return events
def get_nearest_places(self):
pl_exp = PlaceExposition.objects.language().filter(city=self.city).exclude(id=self.id)[:3]
pl_conf = PlaceConference.objects.language().filter(city=self.city)[:3]
#return pl_exp[]
return list(pl_exp)+ list(pl_conf)#PlaceExposition.objects.filter(city=self.city).exclude(id=self.id)
def get_nearest_hotels(self):
if not self.address:
return []
# get coordinates of all hotels in current city
qs_hotels_all = self.city.hotels.filter(city=self.city)
hotels_coord = [(hotel.latitude, hotel.longitude) for hotel in qs_hotels_all]
# coordinate current place
place_coord = (float(self.address['lat']), float(self.address['lng']))
# 4 coordinates of 4 nearest hotels
hotels_coord = sorted(hotels_coord, key=partial(dist, place_coord))[:4]
# start generating filter for queryset
qs = [Q(latitude=item[0]) & Q(longitude=item[1]) for item in hotels_coord]
res = reduce(lambda a,b: a|b, qs)
return list(qs_hotels_all.filter(res))
def get_type(self):
type = {'Convention centre': _(u'Конгрессно-выставочный центр'), 'Exposition centre': _(u'Выставочный центр'),
'Exposition complex': _(u'Выставочный комплекс')}
return type.get(self.type)
def clone(self):
"""
Return an identical copy of the instance with a new ID.
"""
if not self.pk:
raise ValueError('Instance must be saved before it can be cloned.')
duplicate = copy.copy(self)
# Setting pk to None. Django thinking this is a new object.
duplicate.pk = None
# url must be unique
duplicate.url += '_copy'
if PlaceExposition.objects.safe_get(url=duplicate.url):
#already has copy this instance
return
ignore_fields = ['id', 'master', 'language_code']
duplicate.translate('ru')
tr = self._meta.translations_model.objects.get(language_code = 'ru',master__id=self.pk)
for field in duplicate._translated_field_names:
if field in ignore_fields:
continue
setattr(duplicate, field, getattr(tr, field))
duplicate.save()
# but lost all Halls.
# copy halls
halls = Hall.objects.filter(place_exposition=getattr(self, 'id'))
for hall in halls:
duplicate_hall = copy.copy(hall)
duplicate_hall.exposition = duplicate
duplicate_hall.save()
return duplicate
def get_events_number(self):
exp = Exposition.objects.filter(place=self)
return len(exp)
class Hall(TranslatableModel):
"""
Create Hall model which saves information about halls in PlaceExposition
"""
place_exposition = models.ForeignKey(PlaceExposition, related_name='halls')
number = models.CharField(blank=True, max_length=6)
capacity = models.PositiveIntegerField(blank=True, null=True)
translations = TranslatedFields(
name = models.CharField(max_length=255, blank=True)
)
def calculate_rating(place):
rating_simple = {'address': 20, 'phone': 5, 'fax': 5, 'email': 5,
'web_page': 5, 'logo': 20, 'event_in_year': 5, 'total_area': 5, 'closed_area': 5,
'total_pavilions': 5, 'description':15, 'foundation_year': 5, 'total_halls': 5, 'virtual_tour': 5 }
rating_methods = {'theme': 10, 'tag': 5, 'photo':5}
# base rating
rating = 100
for key, value in rating_simple.iteritems():
if getattr(place, key):
rating += value
place.rating = rating
# call to prevent recursion
post_save.disconnect(create_place, sender=PlaceExposition)
place.save()
post_save.connect(create_place, sender=PlaceExposition)
def create_place(sender, instance, created, **kwargs):
post_save_handler(sender, instance=instance, **kwargs)
calculate_rating(instance)
#test
pre_save.connect(pre_save_handler, sender=PlaceExposition)
post_save.connect(create_place, sender=PlaceExposition)
post_save.connect(post_save_handler, sender=Hall)