# -*- coding: utf-8 -*- from django.db import models from django.contrib.contenttypes import generic from django.db.models.signals import post_save, pre_save from django.utils import timezone from hvad.models import TranslatableModel, TranslatedFields, TranslationManager from functions.custom_fields import EnumField from functions.custom_fields import LocationField from functions.signal_handlers import post_save_handler, pre_save_handler from functions.models_methods import ExpoManager import copy from django.utils.translation import ugettext as _ from conference.models import Conference from functions.model_mixin import ExpoMixin CONFERENCE_TYPE = (('Convention centre', 'Конгресс-центр'), ('Exposition centre', 'Конференц зал'),) def logo_name(instance, filename): url = instance.url return '/'.join(['place_conference', url, url+'_logo.jpg']) class PlaceConference(TranslatableModel, ExpoMixin): """ Create PlaceConference model Uses hvad.TranslatableModel which is child of django.db.models class """ catalog = '/places/' place = 'place_conference' catalog_name = _(u'Места:') search_name = None objects = ExpoManager() url = models.SlugField(unique=True) country = models.ForeignKey('country.Country', on_delete=models.PROTECT, verbose_name=_(u'Страна')) city = models.ForeignKey('city.City', on_delete=models.PROTECT, related_name='place_conferences', verbose_name=_(u'Город')) #type uses EnumField for creating Enum type field in Mysql database type = EnumField(values = [item1 for item1, item2 in CONFERENCE_TYPE], default=CONFERENCE_TYPE[0][0], verbose_name=_(u'Тип')) #information address = LocationField(verbose_name=_(u'Адресс')) # phone = models.BigIntegerField(blank=True, null=True, verbose_name=_(u'Телефон')) fax = models.BigIntegerField(blank=True, null=True, verbose_name=_(u'Факс')) web_page = models.URLField(blank=True, verbose_name=_(u'Веб сайт')) email = models.EmailField(blank=True, verbose_name=_(u'Email')) foundation_year = models.PositiveIntegerField(blank=True, null=True, verbose_name=_(u'Год основания')) total_capacity = models.PositiveIntegerField(blank=True, null=True, verbose_name=_(u'Общая вместимость')) amount_halls = models.PositiveIntegerField(blank=True, null=True, verbose_name=_(u'Количество залов')) exposition_hall = models.NullBooleanField(verbose_name=_(u'Выставочный зал')) exp_hall_area = models.PositiveIntegerField(blank=True, null=True, verbose_name=_(u'Площадь выст. зала')) video_link = models.CharField(max_length=255, blank=True) virtual_tour = models.URLField(blank=True, verbose_name=_(u'Виртуальный тур')) # wifi = models.NullBooleanField() multimedia_equipment = models.NullBooleanField() conference_call = models.NullBooleanField() translate_equipment = models.NullBooleanField() banquet_hall = models.NullBooleanField() catering = models.NullBooleanField() hotel = models.NullBooleanField() # logo = models.ImageField(verbose_name='Logo', upload_to=logo_name, blank=True, max_length=255) files = generic.GenericRelation('file.FileModel',content_type_field='content_type', object_id_field='object_id') top = models.ForeignKey('expobanner.Top', blank=True, null=True, on_delete=models.SET_NULL) #translations is translated fields translations = TranslatedFields( name = models.CharField(max_length=100), main_title = models.TextField(blank=True), description = models.TextField(blank=True), adress = models.TextField(blank=True), hall_capacity = models.CharField(max_length=255, 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) def __unicode__(self): return self.lazy_translation_getter('name', unicode(self.pk)) def get_logo(self): logo = self.files.filter(purpose='logo') if logo: return logo[0] return logo def get_type(self): type = {'Convention centre': _(u'Конгресс-центр'), 'Exposition centre': _(u'Конференц зал')} return type.get(self.type) def get_object_type(self): return _(u'Места') def get_object_catalog(self): return '/places' def get_events_number(self): return Conference.objects.filter(place=self, data_begin__gt=timezone.now()).count() def events(self): events = Conference.objects.filter(place=self)[:6] return events def get_nearest_places(self): return PlaceConference.objects.filter(city=self.city).exclude(id=self.id)[:6] def get_catalog_url(self): return self.catalog def get_permanent_url(self): url = '%s%s/'%(self.get_catalog_url(), self.url) return url 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 PlaceConference.objects.safe_get(url=duplicate.url): #already has copy this instance return # duplicate should not be published duplicate.is_published = False duplicate.cancel_by_administrator = False 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_conference=getattr(self, 'id')) for hall in halls: duplicate_hall = copy.copy(hall) duplicate_hall.place_conference = duplicate duplicate_hall.save() return duplicate class Hall(models.Model): """ Create Hall model which saves information about halls in PlaceConference """ place_conference = models.ForeignKey(PlaceConference, related_name='halls') name = models.CharField(max_length=100, blank=True) number = models.PositiveIntegerField(blank=True, null=True) capacity = models.PositiveIntegerField(blank=True, null=True) pre_save.connect(pre_save_handler, sender=PlaceConference) post_save.connect(post_save_handler, sender=PlaceConference)