Source

gnocchi-blog / gnocchi / blog / models.py

funkybob e89b710 



























funkybob 00f81ed 

funkybob e89b710 







funkybob a481442 
funkybob e89b710 

funkybob a481442 
funkybob e89b710 





























funkybob a481442 
funkybob e89b710 
























from django.db import models

from gnocchi.blog import settings
from taggit.managers import TaggableManager

from datetime import datetime

class BlogManager(models.Manager):
    def current(self):
        now = datetime.now()
        return self.get_query_set().filter(
            published=True,
            post_date__lte=now,
        ).exclude(
            kill_date__lte=now
        )

class Post(models.Model):
    title = models.CharField(max_length=200)
    slug = models.SlugField()
    content = models.TextField(blank=True)

    post_date = models.DateTimeField(default=datetime.now)
    kill_date = models.DateTimeField(null=True, blank=True, default=None)
    posted_by = models.ForeignKey('auth.User', blank=True, null=True,
        related_name='blog_posts')
    published = models.BooleanField(default=False)

    site = models.ForeignKey('sites.Site', null=True, blank=True)

    allow_comments = models.BooleanField(default=True)

    tags = TaggableManager(blank=True)

    objects = BlogManager()

    class Meta:
        ordering = ('-post_date',)

    def __unicode__(self):
        return self.title

    @models.permalink
    def get_absolute_url(self):
        kwargs = {'slug': self.slug,}
        if settings.BLOG_DATEBASED_URLS:
            kwargs.update({
                'year': '%04d' % self.post_date.year,
                'month': self.post_date.strftime('%b').lower(),
                'day': '%02d' % self.post_date.day,
            })
        if settings.BLOG_USERNAME_URLS:
            kwargs['username'] = self.posted_by.username
        return 'blog-detail', (), kwargs

class Comment(models.Model):
    '''Mostly a copy of django.contrib.comments Comment model'''
    post = models.ForeignKey(Post)
    content = models.TextField()

    post_date = models.DateTimeField(default=datetime.now)
    ip_address = models.IPAddressField(blank=True, null=True)

    user = models.ForeignKey('auth.User', null=True, blank=True)
    user_name = models.CharField(max_length=50)
    user_email  = models.EmailField(blank=True)

    is_public = models.BooleanField(default=True)
    is_removed = models.BooleanField(default=True)

    class Meta:
        ordering = ('post_date',)

    def __unicode__(self):
        return u'%s...' % self.content[:50]

    # Properties for variable values

    def get_name(self):
        if self.user_id:
            return self.user.get_full_name() or self.user.username
        return self.user_name
    def set_name(self, value):
        if self.user_id:
            raise AttributeError("This comment was posted by an authenticated "\
                "user and thus the name is read-only.")
        self.user_name = value
    name = property(get_name, set_name)

    def get_email(self):
        if self.user_id:
            return self.user.email
        return self.user_email
    def set_email(self, value):
        if self.user_id:
            raise AttributeError("This comment was posted by an authenticated "\
                "user and thus the email is read-only.")
        self.user_email = value
    email = property(get_email, set_email)