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.
96 lines
3.6 KiB
96 lines
3.6 KiB
# -*- coding: utf-8 -*-
|
|
from django.db import models
|
|
from hvad.models import TranslatableModel, TranslatedFields, TranslationManager
|
|
from django.contrib.contenttypes.models import ContentType
|
|
from django.contrib.contenttypes import generic
|
|
import copy
|
|
#functions
|
|
from functions.custom_fields import EnumField
|
|
|
|
class NewsManager(TranslationManager):
|
|
def safe_get(self, **kwargs):
|
|
model = self.model
|
|
try:
|
|
return model.objects.get(**kwargs)
|
|
except:
|
|
return None
|
|
|
|
TYPES = ('announcement', 'news', 'overview')
|
|
class News(TranslatableModel):
|
|
#set manager of this model
|
|
objects = NewsManager()
|
|
|
|
content_type = models.ForeignKey(ContentType, null=True)
|
|
object_id = models.PositiveIntegerField(blank=True, null=True)
|
|
object = generic.GenericForeignKey(content_type, object_id)
|
|
|
|
url = models.SlugField(unique=True)
|
|
date = models.DateField(verbose_name='Дата')
|
|
type = EnumField(values=TYPES)
|
|
theme = models.ManyToManyField('theme.Theme', verbose_name='Тема')
|
|
tag = models.ManyToManyField('theme.Tag', verbose_name='Теги', blank=True, null=True)
|
|
user = models.ForeignKey('accounts.User', verbose_name='Организатор', blank=True, null=True,
|
|
on_delete=models.PROTECT)
|
|
paid = models.BooleanField(default=0)
|
|
|
|
translations = TranslatedFields(
|
|
main_title = models.CharField(verbose_name='Заголовок', max_length=255),
|
|
preview = models.TextField(verbose_name='Превью'),
|
|
description = models.TextField(verbose_name='Описание'),
|
|
#---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)
|
|
|
|
main_page = models.PositiveIntegerField(default=0, db_index=True)
|
|
views = models.PositiveIntegerField(default=0)
|
|
|
|
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 News.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)
|
|
|
|
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=News) |