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.
190 lines
5.8 KiB
190 lines
5.8 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
|
|
|
|
|
|
class ThemeManager(TranslationManager):
|
|
def safe_get(self, **kwargs):
|
|
model = self.model
|
|
try:
|
|
return model.objects.get(**kwargs)
|
|
except:
|
|
return None
|
|
|
|
|
|
class Theme(TranslatableModel):
|
|
"""
|
|
Create Theme model
|
|
|
|
Uses hvad.TranslatableModel which is child of django.db.models class
|
|
|
|
"""
|
|
#set manager of this model
|
|
objects = ThemeManager()
|
|
|
|
FLAGS = (
|
|
('exposition', 'Выставка'),
|
|
('conference', 'Конференция'),
|
|
('seminar', 'Семинар'),
|
|
('webinar', 'Вебинар'),
|
|
)
|
|
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),
|
|
)
|
|
#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
|
|
duplicate.save()
|
|
# but lost all Translations.
|
|
|
|
# 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()
|
|
|
|
return duplicate
|
|
|
|
|
|
class TagManager(TranslationManager):
|
|
def safe_get(self, **kwargs):
|
|
model = self.model
|
|
try:
|
|
return model.objects.get(**kwargs)
|
|
except:
|
|
return None
|
|
|
|
|
|
class Tag(TranslatableModel):
|
|
"""
|
|
Create Tag model
|
|
|
|
Uses hvad.TranslatableModel which is child of django.db.models class
|
|
|
|
"""
|
|
#set manager of this model
|
|
objects = TagManager()
|
|
|
|
theme = models.ForeignKey(Theme, on_delete=models.PROTECT, related_name='themes')
|
|
#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)
|
|
# Setting pk to None. Django thinking this is a new object.
|
|
duplicate.pk = None
|
|
|
|
duplicate.save()
|
|
# but lost all Translations.
|
|
|
|
# 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()
|
|
|
|
return duplicate
|
|
|
|
|
|
from django.db.models.signals import post_save
|
|
from django.dispatch import receiver
|
|
from django.conf import settings
|
|
from functions.translate import get_translated_fields
|
|
|
|
@receiver(post_save)
|
|
def theme_post_save_handler(sender, **kwargs):
|
|
"""
|
|
objects saves two times:
|
|
- first time Exposition object
|
|
- second time ExpositionTranslation object
|
|
object must have at least one Translation
|
|
"""
|
|
|
|
obj = kwargs['instance']
|
|
if isinstance(obj, Theme) or isinstance(obj, Tag):
|
|
# object is Theme or Tag
|
|
# what languages must be
|
|
all_langs = [code for code, lang in settings.LANGUAGES]
|
|
# what languages are
|
|
obj_langs = obj.get_available_languages()
|
|
#
|
|
missing_languages = list(set(all_langs) - set(obj_langs))
|
|
# get first Translation object
|
|
translation = obj.translations.all()[0]
|
|
# translated fields
|
|
fields = get_translated_fields(obj)
|
|
|
|
for code in missing_languages:
|
|
# translate
|
|
obj.translate(code)
|
|
# go through all fields and set value
|
|
for field in fields:
|
|
setattr(obj, field, getattr(translation, field))
|
|
|
|
obj.save() |