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.
138 lines
4.8 KiB
138 lines
4.8 KiB
# -*- coding: utf-8 -*-
|
|
import copy
|
|
from django.db import models
|
|
from django.utils.translation import ugettext_lazy as _
|
|
from hvad.models import TranslatableModel, TranslatedFields, TranslationManager
|
|
from sorl.thumbnail import ImageField
|
|
|
|
|
|
class ArticleManager(TranslationManager):
|
|
def safe_get(self, **kwargs):
|
|
model = self.model
|
|
try:
|
|
return model.objects.get(**kwargs)
|
|
except:
|
|
return None
|
|
|
|
class Article(TranslatableModel):
|
|
"""
|
|
Create Article model
|
|
|
|
Uses hvad.TranslatableModel which is child of django.db.models class
|
|
|
|
"""
|
|
#set manager of this model
|
|
objects = ArticleManager()
|
|
|
|
url = models.SlugField(unique=True)
|
|
theme = models.ManyToManyField('theme.Theme')
|
|
tag = models.ManyToManyField('theme.Tag', related_name='tags',blank=True, null=True)
|
|
user = models.ForeignKey('accounts.User', verbose_name='Автор',
|
|
on_delete=models.PROTECT, related_name='articles')
|
|
main_page = models.PositiveIntegerField(default=0, db_index=True)
|
|
|
|
#translated fields
|
|
translations = TranslatedFields(
|
|
main_title = models.CharField(max_length=100),
|
|
preview = models.TextField(),
|
|
description = models.TextField(),
|
|
#-----meta
|
|
title = models.CharField(max_length=255, blank=True),
|
|
descriptions = models.CharField(max_length=255, blank=True),
|
|
keywords = models.CharField(max_length=255, blank=True),
|
|
)
|
|
|
|
def __unicode__(self):
|
|
return self.lazy_translation_getter('main_title', 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 Article.objects.safe_get(url=duplicate.url):
|
|
#already has copy this instance
|
|
return
|
|
|
|
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 ManyToMany relations and Translations.
|
|
'''
|
|
# copy relations
|
|
for field in self._meta.many_to_many:
|
|
source = getattr(self, field.attname)
|
|
destination = getattr(duplicate, field.attname)
|
|
for item in source.all():
|
|
destination.add(item)
|
|
|
|
# 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 functions.signal_handlers import post_save_handler
|
|
|
|
post_save.connect(post_save_handler, sender=Article)
|
|
|
|
|
|
#--------------------------------
|
|
class Blog(TranslatableModel):
|
|
url = models.SlugField(unique=True)
|
|
theme = models.ManyToManyField('theme.Theme', related_name='blog_theme')
|
|
tag = models.ManyToManyField('theme.Tag', related_name='blog_tag',blank=True, null=True)
|
|
creator = models.ForeignKey('accounts.User', verbose_name='Автор',
|
|
on_delete=models.PROTECT, related_name='blog')
|
|
date = models.DateField(verbose_name=_(u'Дата'))
|
|
preview_img = ImageField(upload_to='article', blank=True)
|
|
|
|
#
|
|
main_page = models.PositiveIntegerField(default=0, db_index=True)
|
|
translations = TranslatedFields(
|
|
main_title = models.CharField(max_length=255),
|
|
preview = models.TextField(),
|
|
description = models.TextField(),
|
|
#-----meta
|
|
title = models.CharField(max_length=255, blank=True),
|
|
descriptions = models.CharField(max_length=255, blank=True),
|
|
keywords = models.CharField(max_length=255, 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('main_title', self.pk)
|
|
|
|
|
|
#post_save.connect(post_save_handler, sender=Blog) |