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.
 
 
 
 
 
 

152 lines
4.6 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