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.
 
 
 
 
 
 

130 lines
4.8 KiB

# -*- coding: utf-8 -*-
from django.db import models
from django.contrib.contenttypes import generic
from hvad.models import TranslatableModel, TranslatedFields, TranslationManager
from functions.custom_fields import EnumField
from functions.custom_fields import LocationField
import copy
class PlaceConferenceManager(TranslationManager):
def safe_get(self, **kwargs):
model = self.model
try:
return model.objects.get(**kwargs)
except:
return None
CONFERENCE_TYPE = (('Convention centre', 'Конгресс-центр'), ('Exposition centre', 'Конференц зал'),)
class PlaceConference(TranslatableModel):
"""
Create PlaceConference model
Uses hvad.TranslatableModel which is child of django.db.models class
"""
#set manager of this model
objects = PlaceConferenceManager()
url = models.SlugField(unique=True)
country = models.ForeignKey('country.Country', on_delete=models.PROTECT)
city = models.ForeignKey('city.City', on_delete=models.PROTECT)
#type uses EnumField for creating Enum type field in Mysql database
type = EnumField(values = [item1 for item1, item2 in CONFERENCE_TYPE])
#information
#
address= LocationField(verbose_name='Адресс')
#
phone = models.PositiveIntegerField(blank=True, null=True)
fax = models.PositiveIntegerField(blank=True, null=True)
web_page = models.URLField(blank=True)
email = models.EmailField(blank=True)
total_capacity = models.PositiveIntegerField(blank=True, null=True)
amount_halls = models.PositiveIntegerField(blank=True, null=True)
exposition_hall = models.NullBooleanField()
exp_hall_area = models.PositiveIntegerField(blank=True, null=True)
#
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()
#
files = generic.GenericRelation('file.FileModel',content_type_field='content_type', object_id_field='object_id')
#translations is translated fields
translations = TranslatedFields(
name = models.CharField(max_length=100),
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),
)
#fields saves information about creating and changing model
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
def __unicode__(self):
return self.lazy_translation_getter('name', unicode(self.pk))
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
duplicate.save()
# but lost all Translations and Halls.
# copy translations
languages = self.get_available_languages()
ignore_fields = ['id', 'master', 'language_code']
for code in languages:
duplicate.translate(code)
tr = self._meta.translations_model.objects.get(language_code = code,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()
# 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)
name = models.CharField(max_length=100, blank=True)
number = models.PositiveIntegerField(blank=True, null=True)
capacity = models.PositiveIntegerField(blank=True, null=True)