Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 live."
- )
- class Meta:
- abstract = True
- class Blog(TimeStampedActivate):
- """
- A blog belonging to a user.
- Blogs have multiple posts and one use can have multiple blogs.
- ### TESTS
- >>> 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
- >>> print b.slug
- foo-blog
- """
- name = models.CharField(
- max_length=255,
- help_text="Name of your blog. Upto 255 characters"
- )
- slug = models.SlugField()
- description = models.TextField(
- blank=True,
- help_text="Describe your blog."
- )
- user = models.ForeignKey(User, related_name='blog')
- def __unicode__(self):
- return self.name
- class Post(TimeStampedActivate):
- """
- A post that belongs to a blog
- ### TESTS
- >>> b = Blog.objects.get(id=1)
- >>> p = Post()
- >>> p.title = "A test post"
- >>> p.blog = b
- >>> p.body = "This here is the body."
- >>> 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, upto 255 characters long."
- )
- slug = models.SlugField()
- excerpt = models.TextField(
- blank=True,
- help_text="A small teaser of your content."
- )
- body = models.TextField(
- blank=False,
- help_text="The main content of your post."
- )
- publish_at = models.DateTimeField(
- default=datetime.datetime.now(),
- help_text="Date and time post should become visible."
- )
- tags = TaggableManager()
- blog = models.ForeignKey(Blog, related_name="posts")
- objects = PostManager()
- def __unicode__(self):
- return self.title
- class Meta:
- ordering = ['-publish_at', '-modified', '-created']
Add Comment
Please, Sign In to add comment