Source

Django-Portfolio / demenza_site / blog / models.py

import datetime

from django.db import models
from django.contrib.auth.models import User

from taggit.managers import TaggableManager

class PostManager(models.Manager):
    def get_visible(self):
        return self.get_query_set().filter(publish_at__lte=datetime.datetime.now(), active=True)

class TimeStampedActivate(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)
    active = models.BooleanField(default=False,
                                 help_text="Controls whether or not this item is visible to the world.")

    class Meta:
        abstract = True

class Blog(TimeStampedActivate):
    """
    A blog belonging to a user.
    Blogs have multiple posts and one user can have many blogs.

    >>> b = Blog()
    >>> b.name = 'Foo Blog'
    >>> b.user = User.objects.create(username='foo', password='test')
    >>> b.slug = 'foo-blog'
    >>> b.save()
    >>> print b
    Foo Blog
    >>> print b.user.username
    foo
    """
    name = models.CharField(max_length=255,
                            help_text="Name of your blog. 255 characters limit.")
    slug = models.SlugField()
    description = models.TextField(blank=True,
                                   help_text="Describe your blog.")
    user = models.ForeignKey(User, related_name="blogs")

    def __unicode__(self):
        return self.name

    @models.permalink
    def get_absolute_url(self):
        return ('blog', (), {
            'slug': self.slug
        })

class Post(TimeStampedActivate):
    """
    Post which belongs to a blog.

    >>> b = Blog.objects.get(id=1)
    >>> p = Post()
    >>> p.title = "A Test Post"
    >>> p.blog = b
    >>> p.body = "Just a small test"
    >>> p.slug = "a-test-post"
    >>> p.save()
    >>> print p.blog.name
    Foo Blog
    >>> print p.active
    False
    """
    title = models.CharField(max_length=255,
                             help_text="Title of the post.")
    slug = models.SlugField()
    excerpt = models.TextField(blank=True,
                               help_text="Small teaser. (Optional)")
    body = models.TextField(help_text="Markdown <a href=\"http://daringfireball.net/projects/markdown/\">supported</a>")
    publish_at = models.DateTimeField(default=datetime.datetime.now(),
                                      help_text="Date and time post should become visible.")
    blog = models.ForeignKey(Blog, related_name="posts")
    tags = TaggableManager()
    objects = PostManager()

    def __unicode__(self):
        return self.title

    def is_visible(self):
        """
        Checks posts for visibility.
        I.e: If scheduled to show next year, do not show this item.
        """
        if self.active and self.publish_at <= datetime.datetime.now():
            return True
        return False

    @models.permalink
    def get_absolute_url(self):
        return ('post', (), {
            'blog': self.blog.slug,
            'slug': self.slug
        })

    class Meta:
        ordering = ['publish_at', '-modified', '-created']
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.