2014-01-04 16:07:09 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from cms.models import PlaceholderField, CMSPlugin
|
|
|
|
from cmsplugin_filer_image.models import ThumbnailOption
|
|
|
|
from django.contrib.auth.models import User
|
2014-01-08 21:33:20 +00:00
|
|
|
from django.contrib.sites.models import Site
|
2014-01-04 16:07:09 +00:00
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
from django.db import models
|
|
|
|
from django.utils import timezone
|
2014-06-08 22:06:12 +00:00
|
|
|
from django.utils.encoding import force_text
|
2014-01-04 16:07:09 +00:00
|
|
|
from django.utils.text import slugify
|
2014-03-07 17:36:41 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _, get_language
|
2014-01-04 16:07:09 +00:00
|
|
|
from djangocms_text_ckeditor.fields import HTMLField
|
|
|
|
from filer.fields.image import FilerImageField
|
2014-01-09 07:51:03 +00:00
|
|
|
from parler.models import TranslatableModel, TranslatedFields
|
2014-01-10 05:37:55 +00:00
|
|
|
from parler.managers import TranslationManager
|
2014-01-04 16:07:09 +00:00
|
|
|
from taggit_autosuggest.managers import TaggableManager
|
|
|
|
|
|
|
|
from . import settings
|
2014-04-19 17:18:11 +00:00
|
|
|
from meta_mixin.models import ModelMeta
|
2014-01-10 05:37:55 +00:00
|
|
|
from .managers import GenericDateTaggedManager
|
2014-01-04 16:07:09 +00:00
|
|
|
|
2014-03-13 21:16:40 +00:00
|
|
|
BLOG_CURRENT_POST_IDENTIFIER = 'djangocms_post_current'
|
2014-01-04 16:07:09 +00:00
|
|
|
|
2014-04-01 14:32:15 +00:00
|
|
|
|
2014-01-04 16:07:09 +00:00
|
|
|
class BlogCategory(TranslatableModel):
|
|
|
|
"""
|
|
|
|
Blog category
|
|
|
|
"""
|
|
|
|
parent = models.ForeignKey('self', verbose_name=_('parent'), null=True,
|
|
|
|
blank=True)
|
|
|
|
date_created = models.DateTimeField(_('created at'), auto_now_add=True)
|
|
|
|
date_modified = models.DateTimeField(_('modified at'), auto_now=True)
|
|
|
|
|
|
|
|
translations = TranslatedFields(
|
|
|
|
name=models.CharField(_('name'), max_length=255),
|
2014-01-10 05:37:55 +00:00
|
|
|
slug=models.SlugField(_('slug'), blank=True, db_index=True),
|
|
|
|
meta={'unique_together': (('language_code', 'slug'),)}
|
2014-01-04 16:07:09 +00:00
|
|
|
)
|
|
|
|
|
2014-01-10 05:37:55 +00:00
|
|
|
objects = TranslationManager()
|
|
|
|
|
2014-01-04 16:07:09 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _('blog category')
|
|
|
|
verbose_name_plural = _('blog categories')
|
2014-01-10 05:37:55 +00:00
|
|
|
|
2014-01-08 21:33:20 +00:00
|
|
|
@property
|
|
|
|
def count(self):
|
2014-01-10 05:37:55 +00:00
|
|
|
return self.blog_posts.filter(publish=True).count()
|
2014-01-04 16:07:09 +00:00
|
|
|
|
|
|
|
def __unicode__(self):
|
2014-01-09 07:51:03 +00:00
|
|
|
return self.safe_translation_getter('name')
|
2014-01-04 16:07:09 +00:00
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
super(BlogCategory, self).save(*args, **kwargs)
|
2014-01-09 07:51:03 +00:00
|
|
|
for lang in self.get_available_languages():
|
|
|
|
self.set_current_language(lang)
|
|
|
|
if not self.slug and self.name:
|
2014-06-08 22:06:12 +00:00
|
|
|
self.slug = slugify(force_text(self.name))
|
2014-01-09 07:51:03 +00:00
|
|
|
self.save_translations()
|
2014-01-04 16:07:09 +00:00
|
|
|
|
|
|
|
|
2014-04-19 17:18:11 +00:00
|
|
|
class Post(ModelMeta, TranslatableModel):
|
2014-01-04 16:07:09 +00:00
|
|
|
"""
|
|
|
|
Blog post
|
|
|
|
"""
|
2014-04-01 14:32:15 +00:00
|
|
|
author = models.ForeignKey(User, verbose_name=_('Author'), null=True, blank=True,
|
2014-04-08 17:10:56 +00:00
|
|
|
related_name='djangocms_blog_post_author')
|
2014-01-04 16:07:09 +00:00
|
|
|
|
|
|
|
date_created = models.DateTimeField(auto_now_add=True)
|
|
|
|
date_modified = models.DateTimeField(auto_now=True)
|
|
|
|
date_published = models.DateTimeField(_('Published Since'),
|
|
|
|
default=timezone.now)
|
|
|
|
date_published_end = models.DateTimeField(_('Published Until'), null=True,
|
|
|
|
blank=True)
|
2014-02-27 12:46:54 +00:00
|
|
|
publish = models.BooleanField(_('Publish'), default=False)
|
2014-01-04 16:07:09 +00:00
|
|
|
categories = models.ManyToManyField(BlogCategory, verbose_name=_('category'),
|
|
|
|
related_name='blog_posts',)
|
2014-04-01 14:32:15 +00:00
|
|
|
main_image = FilerImageField(verbose_name=_('Main image'), blank=True, null=True,
|
2014-04-08 17:10:56 +00:00
|
|
|
related_name='djangocms_blog_post_image')
|
2014-01-04 16:07:09 +00:00
|
|
|
main_image_thumbnail = models.ForeignKey(ThumbnailOption,
|
2014-02-27 12:46:54 +00:00
|
|
|
verbose_name=_('Main image thumbnail'),
|
2014-04-08 17:10:56 +00:00
|
|
|
related_name='djangocms_blog_post_thumbnail',
|
2014-01-04 16:07:09 +00:00
|
|
|
blank=True, null=True)
|
|
|
|
main_image_full = models.ForeignKey(ThumbnailOption,
|
2014-02-27 12:46:54 +00:00
|
|
|
verbose_name=_('Main image full'),
|
2014-04-19 17:18:11 +00:00
|
|
|
related_name='djangocms_blog_post_full',
|
2014-01-04 20:13:25 +00:00
|
|
|
blank=True, null=True)
|
2014-04-10 17:11:23 +00:00
|
|
|
enable_comments = models.BooleanField(
|
2014-06-08 22:06:12 +00:00
|
|
|
verbose_name=_(u'Enable comments on post'),
|
2014-04-10 17:11:23 +00:00
|
|
|
default=settings.BLOG_ENABLE_COMMENTS
|
|
|
|
)
|
2014-01-04 16:07:09 +00:00
|
|
|
|
|
|
|
translations = TranslatedFields(
|
2014-02-27 12:46:54 +00:00
|
|
|
title=models.CharField(_('Title'), max_length=255),
|
2014-01-10 05:37:55 +00:00
|
|
|
slug=models.SlugField(_('slug'), blank=True, db_index=True),
|
2014-04-19 17:18:11 +00:00
|
|
|
abstract=HTMLField(_('Abstract')),
|
2014-03-05 17:11:32 +00:00
|
|
|
meta_description=models.TextField(verbose_name=_(u'Post meta description'),
|
|
|
|
blank=True, default=''),
|
2014-03-06 19:02:52 +00:00
|
|
|
meta_keywords=models.TextField(verbose_name=_(u'Post meta keywords'),
|
|
|
|
blank=True, default=''),
|
2014-04-16 13:14:30 +00:00
|
|
|
post_text=HTMLField(_('Text'), default='', blank=True),
|
2014-01-10 05:37:55 +00:00
|
|
|
meta={'unique_together': (('language_code', 'slug'),)}
|
2014-01-04 16:07:09 +00:00
|
|
|
)
|
|
|
|
content = PlaceholderField("post_content")
|
|
|
|
|
|
|
|
objects = GenericDateTaggedManager()
|
2014-04-01 14:32:15 +00:00
|
|
|
tags = TaggableManager(blank=True, related_name='djangocms_blog_tags')
|
2014-01-04 16:07:09 +00:00
|
|
|
|
2014-04-19 17:18:11 +00:00
|
|
|
_metadata = {
|
|
|
|
'title': 'title',
|
|
|
|
'description': 'get_description',
|
|
|
|
'og_description': 'get_description',
|
|
|
|
'twitter_description': 'get_description',
|
|
|
|
'gplus_description': 'get_description',
|
|
|
|
'keywords': 'get_keywords',
|
|
|
|
'locale': None,
|
|
|
|
'image': 'get_image_url',
|
|
|
|
'object_type': settings.BLOG_TYPE,
|
|
|
|
'og_type': settings.BLOG_FB_TYPE,
|
|
|
|
'og_app_id': settings.BLOG_FB_APPID,
|
|
|
|
'og_profile_id': settings.BLOG_FB_PROFILE_ID,
|
|
|
|
'og_publisher': settings.BLOG_FB_PUBLISHER,
|
|
|
|
'og_author_url': settings.BLOG_FB_AUTHOR_URL,
|
|
|
|
'twitter_type': settings.BLOG_TWITTER_TYPE,
|
|
|
|
'twitter_site': settings.BLOG_TWITTER_SITE,
|
|
|
|
'twitter_author': settings.BLOG_TWITTER_AUTHOR,
|
|
|
|
'gplus_type': settings.BLOG_GPLUS_TYPE,
|
|
|
|
'gplus_author': settings.BLOG_GPLUS_AUTHOR,
|
|
|
|
'published_time': 'date_published',
|
|
|
|
'modified_time': 'date_modified',
|
|
|
|
'expiration_time': 'date_published_end',
|
|
|
|
'tag': 'get_tags',
|
|
|
|
'url': 'get_absolute_url',
|
|
|
|
}
|
|
|
|
|
|
|
|
def get_keywords(self):
|
2014-06-08 22:06:12 +00:00
|
|
|
return self.safe_translation_getter('meta_keywords').strip().split(",")
|
2014-04-19 17:18:11 +00:00
|
|
|
|
|
|
|
def get_description(self):
|
2014-06-08 22:06:12 +00:00
|
|
|
description = self.safe_translation_getter('meta_description', any_language=True)
|
2014-04-19 17:18:11 +00:00
|
|
|
if not description:
|
2014-06-08 22:06:12 +00:00
|
|
|
description = self.safe_translation_getter('abstract', any_language=True)
|
2014-04-19 17:18:11 +00:00
|
|
|
return description.strip()
|
|
|
|
|
|
|
|
def get_image_url(self):
|
|
|
|
return self.main_image
|
|
|
|
|
|
|
|
def get_tags(self):
|
|
|
|
taglist = [tag.name for tag in self.tags.all()]
|
|
|
|
return ",".join(taglist)
|
|
|
|
|
|
|
|
def get_author(self):
|
|
|
|
return self.author
|
|
|
|
|
2014-01-04 16:07:09 +00:00
|
|
|
class Meta:
|
2014-01-10 05:37:55 +00:00
|
|
|
verbose_name = _('blog article')
|
|
|
|
verbose_name_plural = _('blog articles')
|
|
|
|
ordering = ("-date_published", "-date_created")
|
2014-03-13 21:16:40 +00:00
|
|
|
get_latest_by = 'date_published'
|
2014-01-04 16:07:09 +00:00
|
|
|
|
|
|
|
def __unicode__(self):
|
2014-01-09 07:51:03 +00:00
|
|
|
return self.safe_translation_getter('title')
|
2014-01-04 16:07:09 +00:00
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
super(Post, self).save(*args, **kwargs)
|
2014-01-09 07:51:03 +00:00
|
|
|
for lang in self.get_available_languages():
|
|
|
|
self.set_current_language(lang)
|
|
|
|
if not self.slug and self.title:
|
|
|
|
self.slug = slugify(self.title)
|
|
|
|
self.save_translations()
|
2014-01-04 16:07:09 +00:00
|
|
|
|
|
|
|
def get_absolute_url(self):
|
|
|
|
kwargs = {'year': self.date_published.year,
|
|
|
|
'month': self.date_published.month,
|
|
|
|
'day': self.date_published.day,
|
2014-06-08 22:06:12 +00:00
|
|
|
'slug': self.safe_translation_getter('slug', any_language=True)}
|
2014-01-04 16:07:09 +00:00
|
|
|
return reverse('djangocms_blog:post-detail', kwargs=kwargs)
|
|
|
|
|
|
|
|
def thumbnail_options(self):
|
|
|
|
if self.main_image_thumbnail_id:
|
2014-01-04 20:13:25 +00:00
|
|
|
return self.main_image_thumbnail.as_dict
|
2014-01-04 16:07:09 +00:00
|
|
|
else:
|
|
|
|
return settings.BLOG_IMAGE_THUMBNAIL_SIZE
|
2014-01-08 21:33:20 +00:00
|
|
|
|
2014-01-04 16:07:09 +00:00
|
|
|
def full_image_options(self):
|
2014-06-08 22:06:12 +00:00
|
|
|
if self.main_image_full_id:
|
2014-01-04 20:13:25 +00:00
|
|
|
return self.main_image_full.as_dict
|
2014-01-04 16:07:09 +00:00
|
|
|
else:
|
|
|
|
return settings.BLOG_IMAGE_FULL_SIZE
|
|
|
|
|
2014-04-19 17:18:11 +00:00
|
|
|
def get_full_url(self):
|
|
|
|
s = Site.objects.get_current()
|
|
|
|
if s.domain.find('http') > -1:
|
|
|
|
return "%s%s" % (s.domain, self.get_absolute_url())
|
|
|
|
else:
|
|
|
|
return "http://%s%s" % (s.domain, self.get_absolute_url())
|
|
|
|
|
2014-01-04 16:07:09 +00:00
|
|
|
|
|
|
|
class LatestPostsPlugin(CMSPlugin):
|
|
|
|
|
2014-01-10 05:37:55 +00:00
|
|
|
latest_posts = models.IntegerField(_(u'Articles'), default=settings.BLOG_LATEST_POSTS,
|
|
|
|
help_text=_('The number of latests articles to be displayed.'))
|
|
|
|
tags = models.ManyToManyField('taggit.Tag', blank=True,
|
|
|
|
help_text=_('Show only the blog articles tagged with chosen tags.'))
|
|
|
|
categories = models.ManyToManyField('BlogCategory', blank=True,
|
2014-02-27 08:39:02 +00:00
|
|
|
help_text=_('Show only the blog articles tagged with chosen categories.'))
|
2014-01-04 16:07:09 +00:00
|
|
|
|
|
|
|
def __unicode__(self):
|
2014-01-10 05:37:55 +00:00
|
|
|
return u"%s latest articles by tag" % self.latest_posts
|
2014-01-04 16:07:09 +00:00
|
|
|
|
|
|
|
def copy_relations(self, oldinstance):
|
|
|
|
self.tags = oldinstance.tags.all()
|
|
|
|
|
|
|
|
def get_posts(self):
|
|
|
|
posts = Post.objects.published()
|
|
|
|
tags = list(self.tags.all())
|
|
|
|
if tags:
|
|
|
|
posts = posts.filter(tags__in=tags)
|
|
|
|
return posts[:self.latest_posts]
|
|
|
|
|
|
|
|
|
|
|
|
class AuthorEntriesPlugin(CMSPlugin):
|
2014-02-23 15:09:27 +00:00
|
|
|
authors = models.ManyToManyField(User, verbose_name=_('Authors'),
|
2014-04-08 17:10:56 +00:00
|
|
|
limit_choices_to={'djangocms_blog_post_author__publish': True}
|
2014-02-23 15:09:27 +00:00
|
|
|
)
|
2014-01-10 05:37:55 +00:00
|
|
|
latest_posts = models.IntegerField(_(u'Articles'), default=settings.BLOG_LATEST_POSTS,
|
|
|
|
help_text=_('The number of author articles to be displayed.'))
|
2014-01-04 16:07:09 +00:00
|
|
|
|
|
|
|
def __unicode__(self):
|
2014-01-10 05:37:55 +00:00
|
|
|
return u"%s latest articles by author" % self.latest_posts
|
2014-01-04 16:07:09 +00:00
|
|
|
|
|
|
|
def copy_relations(self, oldinstance):
|
|
|
|
self.authors = oldinstance.authors.all()
|
|
|
|
|
|
|
|
def get_posts(self):
|
|
|
|
posts = (Post.objects.published().filter(author__in=self.authors.all()))
|
|
|
|
return posts[:self.latest_posts]
|
|
|
|
|
|
|
|
def get_authors(self):
|
|
|
|
authors = self.authors.all()
|
2014-01-10 05:37:55 +00:00
|
|
|
for author in authors:
|
2014-06-09 21:29:34 +00:00
|
|
|
author.count = 0
|
2014-04-08 17:10:56 +00:00
|
|
|
if author.djangocms_blog_post_author.filter(publish=True).exists():
|
|
|
|
author.count = author.djangocms_blog_post_author.filter(publish=True).count()
|
2014-01-04 16:07:09 +00:00
|
|
|
return authors
|