Source

cmsplugin-advancednews / cmsplugin_advancednews / models.py

import datetime

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

from cms.models import CMSPlugin

from multilingual.translation import TranslationModel
from multilingual.exceptions import TranslationDoesNotExist
from multilingual.manager import MultilingualManager
from cms.models.fields import PlaceholderField

class PublishedNewsManager(models.Manager):
    """ 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(models.Model):
    """ News model """
    
    slug = models.SlugField(_('Slug'), unique_for_date='pub_date', 
                            help_text=_('A slug is a short name which uniquely identifies the news item for this day'))

    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')
    
    published = PublishedNewsManager()
    objects = MultilingualManager()
    
    class Meta:
        verbose_name = _('News')
        verbose_name_plural = _('News')
        ordering = ('-pub_date', )

    class Translation(TranslationModel):
        title = models.CharField(_('Title'), max_length=255)
        excerpt = models.TextField(_('Excerpt'), blank=True)   
        
    def __unicode__(self):
        try:
            return u'%s'% (self.title)
        except TranslationDoesNotExist:
            return u'-not-available-'

    @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(models.Model):
    """
    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)
    
    class Meta:
        verbose_name = _('Category')
        verbose_name_plural = _('Categories')
        ordering = ('-pub_date', )
    
    
    class Translation(TranslationModel):
        title = models.CharField(_('Title'), max_length=255)

    def __unicode__(self):
        try:
            return u'%s'% (self.title)
        except TranslationDoesNotExist:
            return u'-not-available-'


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'))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.