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.
 
 
 
 
 
 

157 lines
4.9 KiB

# -*- coding: utf-8 -*-
from django.db import models
from django.utils.translation import ugettext_lazy as _
from hvad.models import TranslatableModel, TranslatedFields, TranslationManager
from bitfield import BitField
import copy
from exposition.models import Exposition
from conference.models import Conference
from seminar.models import Seminar
from webinar.models import Webinar
from functions.models_methods import ExpoManager
class Theme(TranslatableModel):
"""
Create Theme model
Uses hvad.TranslatableModel which is child of django.db.models class
"""
#set manager of this model
objects = ExpoManager()
FLAGS = (
('exposition', 'Выставка'),
('conference', 'Конференция'),
('seminar', 'Семинар'),
('webinar', 'Вебинар'),
)
url = models.SlugField(unique=True, max_length=255)
types = BitField([k for k, v in FLAGS])
#translated fields
translations = TranslatedFields(
name = models.CharField(max_length=100),
main_title = models.CharField(max_length=100),
description = models.TextField(blank=True),
#-----meta data
title = models.CharField(max_length=250, blank=True),
descriptions = models.CharField(max_length=250, blank=True),
keywords = models.CharField(max_length=250, blank=True),
)
class Meta:
ordering = ['translations__name']
# 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 expositions_number(self):
return len(Exposition.objects.filter(theme__id=self.id))
def conferences_number(self):
return len(Conference.objects.filter(theme__id=self.id))
def seminars_number(self):
return len(Seminar.objects.filter(theme__id=self.id))
def webinars_number(self):
return len(Webinar.objects.filter(theme__id=self.id))
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)
ignore_fields = ['id', 'master', 'language_code']
# Setting pk to None. Django thinking this is a new object.
duplicate.pk = None
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()
class Tag(TranslatableModel):
"""
Create Tag model
Uses hvad.TranslatableModel which is child of django.db.models class
"""
#set manager of this model
objects = ExpoManager()
url = models.SlugField(unique=True, max_length=255)
theme = models.ForeignKey(Theme, on_delete=models.PROTECT, related_name='tags')
#translated fields
translations = TranslatedFields(
name = models.CharField(max_length=100),
main_title = models.CharField(max_length=100),
description = models.TextField(blank=True),
#-----meta data
title = models.CharField(max_length=250, blank=True),
descriptions = models.CharField(max_length=250, blank=True),
keywords = models.CharField(max_length=250, blank=True),
)
#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)
ignore_fields = ['id', 'master', 'language_code']
# Setting pk to None. Django thinking this is a new object.
duplicate.pk = None
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()
return duplicate
from django.db.models.signals import post_save, pre_save
from functions.signal_handlers import post_save_handler, pre_save_handler
pre_save.connect(pre_save_handler, sender=Theme)
pre_save.connect(pre_save_handler, sender=Tag)
post_save.connect(post_save_handler, sender=Theme)
post_save.connect(post_save_handler, sender=Tag)