Source

cmsplugin-advancednews / cmsplugin_advancednews / models.py

Full commit
import datetime

from django.db import models
from django.utils.translation import ugettext_lazy as _

from cms.models import CMSPlugin

from hvad.models import TranslatableModel, TranslatedFields
from hvad.manager import TranslationManager
from cms.models.fields import PlaceholderField

class PublishedNewsManager(TranslationManager):
    """ Filters out all unpublished and items with a publication date in the future. """

    def get_query_set(self):
        return super(PublishedNewsManager, self).get_query_set().filter(is_published=True).filter(pub_date__lte=datetime.datetime.now())

    
class News(TranslatableModel):
    """ News model """
    
    slug = models.SlugField(_('Slug'), unique_for_date='pub_date', 
            help_text=_('Name in lowercase with no spaces which will be chown in the URL of the navigator'))

    category = models.ForeignKey("Category", related_name="n_category",
                        null=True, blank=True, default=None)

    is_published = models.BooleanField(_('Published'), default=False)
    pub_date = models.DateTimeField(_('Publication date'), default=datetime.datetime.now())
    
    created = models.DateTimeField(auto_now_add=True, editable=False)
    updated = models.DateTimeField(auto_now=True, editable=False)
    content = PlaceholderField('content', language_aware=True,
            verbose_name=_('Full news body'),
            help_text=_('Full content that will be shown in the body of the news item \
                    <br/><b>Quick note</b>: To add text content: \
                    <br/>Select Available Plutins > Text > Add a plugin'))   
    
    translations = TranslatedFields( 
        title = models.CharField(verbose_name=_('Title'), max_length=255,
            help_text=_('Title of the news item')),
        excerpt = models.TextField(verbose_name=_('Excerpt'), blank=True,
            help_text=_('Short exceprt of the new item. Will be shown on the news list.'))
    )
    
    published = PublishedNewsManager()
    objects = TranslationManager()
    
    class Meta:
        verbose_name = _('News')
        verbose_name_plural = _('News')
        ordering = ('-pub_date', )


    def __unicode__(self):
        return self.lazy_translation_getter('title', 'New: %s' % self.id)
            
        

    @models.permalink
    def get_absolute_url(self):
        return ('news_detail', (), {
            'year': self.pub_date.strftime("%Y"),
            'month': self.pub_date.strftime("%m"),
            'day': self.pub_date.strftime("%d"),
            'slug': self.slug,
            })

class Category(TranslatableModel):
    """
    Category
    """
    
    pub_date        = models.DateTimeField(_('Publication date'), default=datetime.datetime.now())
    created         = models.DateTimeField(auto_now_add=True, editable=False)
    updated         = models.DateTimeField(auto_now=True, editable=False)
    
    translations = TranslatedFields(
        title = models.CharField(_('Title'), max_length=255),
    )
    
    class Meta:
        verbose_name = _('Category')
        verbose_name_plural = _('Categories')
        ordering = ('-pub_date', )
    
    def __unicode__(self):        
        return self.lazy_translation_getter('title', 'Category: %s' % self.id)


class LatestAdvancedNewsPlugin(CMSPlugin):
    """ Model for the settings when using the latest news cms plugin. """
    
    category = models.ForeignKey(Category, null=True, blank=True, default=None)
    limit = models.PositiveIntegerField(_('Number of news items to show'), 
                                        help_text=_('Limits the number of items that will be displayed'))