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.
257 lines
8.6 KiB
257 lines
8.6 KiB
# -*- coding: utf-8 -*-
|
|
import copy
|
|
from django.db import models
|
|
from django.utils.translation import ugettext_lazy as _
|
|
from django.utils.timezone import now
|
|
from hvad.models import TranslatableModel, TranslatedFields, TranslationManager
|
|
from django.utils.html import strip_tags
|
|
from sorl.thumbnail import ImageField
|
|
from functions.url_utils import slugify, unique_slug
|
|
from functions.model_utils import base_concrete_model
|
|
|
|
|
|
class ArticleManager(TranslationManager):
|
|
def safe_get(self, **kwargs):
|
|
model = self.model
|
|
try:
|
|
return model.objects.get(**kwargs)
|
|
except:
|
|
return None
|
|
|
|
def news(self):
|
|
"""
|
|
return queryset of news
|
|
"""
|
|
model = self.model
|
|
return model.objects.filter(type=model.news)
|
|
|
|
def blogs(self):
|
|
"""
|
|
return queryset of blogs
|
|
"""
|
|
model = self.model
|
|
return model.objects.filter(type=model.blog)
|
|
|
|
class Article(TranslatableModel):
|
|
"""
|
|
Create Article model
|
|
|
|
Uses hvad.TranslatableModel which is child of django.db.models class
|
|
|
|
"""
|
|
|
|
MAX_ON_MAIN_PAGE = 3
|
|
# types of article
|
|
blog = 1
|
|
news = 2
|
|
|
|
#set manager of this model
|
|
objects = ArticleManager()
|
|
slug = models.SlugField(unique=True, max_length=255)
|
|
logo = ImageField(upload_to='articles_preview', blank=True)
|
|
theme = models.ManyToManyField('theme.Theme')
|
|
tag = models.ManyToManyField('theme.Tag', related_name='tags',blank=True, null=True)
|
|
author = models.ForeignKey('accounts.User', verbose_name='Автор',
|
|
on_delete=models.PROTECT, related_name='articles')
|
|
exposition = models.ForeignKey('exposition.Exposition', blank=True, null=True)
|
|
conference = models.ForeignKey('conference.Conference', blank=True, null=True)
|
|
type = models.PositiveSmallIntegerField(default=1)
|
|
allow_comments = models.BooleanField(default=True)
|
|
|
|
# fields that provides features of a visible page on the website
|
|
publish_date = models.DateTimeField(blank=True, null=True)
|
|
expiry_date = models.DateTimeField(_("Expires on"),
|
|
help_text=_("With Published chosen, won't be shown after this time"),
|
|
blank=True, null=True)
|
|
in_sitemap = models.BooleanField(_("Show in sitemap"), default=False)
|
|
|
|
# fields that provides features of a visible on main page
|
|
main_page = models.BooleanField(default=False)
|
|
main_page_time = models.DateTimeField(blank=True, null=True)
|
|
|
|
# field that check if need generate description
|
|
gen_description = models.BooleanField(_("Generate description"),
|
|
help_text=_("If checked, the description will be automatically "
|
|
"generated from content. Uncheck if you want to manually "
|
|
"set a custom description."), default=True)
|
|
|
|
# published = models.
|
|
created = models.DateTimeField(auto_now_add=True)
|
|
modified = models.DateTimeField(auto_now=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 translation_model(self):
|
|
return self._meta.translations_model
|
|
|
|
|
|
def publish(self):
|
|
self.in_sitemap = True
|
|
if not self.publish_date:
|
|
# save time only first time
|
|
self.publish_date = now()
|
|
self.save()
|
|
|
|
return self
|
|
|
|
def get_event(self):
|
|
if self.exposition:
|
|
return self.exposition
|
|
elif self.conference:
|
|
return self.conference
|
|
return None
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
# If no slug is provided, generates one before saving.
|
|
if not self.slug:
|
|
self.slug = self.generate_unique_slug()
|
|
|
|
#Set the description field on save.
|
|
if self.gen_description:
|
|
self.description = strip_tags(self.description_from_content())
|
|
super(Article, self).save(*args, **kwargs)
|
|
|
|
def description_from_content(self):
|
|
"""
|
|
"""
|
|
# place for logic
|
|
return ''
|
|
|
|
def generate_unique_slug(self):
|
|
"""
|
|
Create a unique slug by passing the result of get_slug() to
|
|
utils.urls.unique_slug, which appends an index if necessary.
|
|
"""
|
|
# For custom content types, use the ``Page`` instance for
|
|
# slug lookup.
|
|
concrete_model = base_concrete_model(Article, self)
|
|
slug_qs = concrete_model.objects.exclude(id=self.id)
|
|
return unique_slug(slug_qs, "slug", self.get_slug())
|
|
|
|
def get_slug(self):
|
|
"""
|
|
Allows subclasses to implement their own slug creation logic.
|
|
"""
|
|
return slugify(self.get_available_title())
|
|
|
|
def get_available_title(self):
|
|
#print self.lazy_translation_getter('main_title', self.pk)
|
|
return u'%s'%self.lazy_translation_getter('main_title', self.pk)
|
|
|
|
|
|
def _get_next_or_previous_by_publish_date(self, is_next, **kwargs):
|
|
"""
|
|
Retrieves next or previous object by publish date. We implement
|
|
our own version instead of Django's so we can hook into the
|
|
published manager and concrete subclasses.
|
|
"""
|
|
arg = "publish_date__gt" if is_next else "publish_date__lt"
|
|
order = "publish_date" if is_next else "-publish_date"
|
|
lookup = {arg: self.publish_date}
|
|
concrete_model = base_concrete_model(Article, self)
|
|
try:
|
|
queryset = concrete_model.objects.published
|
|
except AttributeError:
|
|
queryset = concrete_model.objects.all
|
|
try:
|
|
return queryset(**kwargs).filter(**lookup).order_by(order)[0]
|
|
except IndexError:
|
|
pass
|
|
|
|
def get_next_by_publish_date(self, **kwargs):
|
|
"""
|
|
Retrieves next object by publish date.
|
|
"""
|
|
return self._get_next_or_previous_by_publish_date(True, **kwargs)
|
|
|
|
def get_previous_by_publish_date(self, **kwargs):
|
|
"""
|
|
Retrieves previous object by publish date.
|
|
"""
|
|
return self._get_next_or_previous_by_publish_date(False, **kwargs)
|
|
|
|
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
|
|
|
|
def admin_url(self):
|
|
if self.type == 1:
|
|
return '/admin/article/blog/%s'%self.slug
|
|
elif self.type == 2:
|
|
return '/admin/article/news/%s'%self.slug
|
|
|
|
def get_permanent_url(self):
|
|
if self.type == 1:
|
|
return '/blogs/%s'%self.slug
|
|
elif self.type == 2:
|
|
return '/news/%s'%self.slug
|
|
|
|
from django.db.models.signals import post_save
|
|
from functions.signal_handlers import post_save_handler
|
|
|
|
post_save.connect(post_save_handler, sender=Article) |