Guest User

Untitled

a guest
Jan 18th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. import datetime
  2.  
  3. from django.db import models
  4. from django.contrib.auth.models import User
  5. from taggit.managers import TaggableManager
  6.  
  7.  
  8. class PostManager(models.Manager):
  9. def get_visible(self):
  10. return self.get_query_set().filter(publish_at__lte=datetime.datetime.now(), active=True)
  11.  
  12.  
  13. class TimeStampedActivate(models.Model):
  14. created = models.DateTimeField(auto_now_add=True)
  15. modified = models.DateTimeField(auto_now=True)
  16. active = models.BooleanField(
  17. default=False,
  18. help_text="Controls whether or not this item is live."
  19. )
  20.  
  21. class Meta:
  22. abstract = True
  23. class Blog(TimeStampedActivate):
  24. """
  25. A blog belonging to a user.
  26. Blogs have multiple posts and one use can have multiple blogs.
  27.  
  28. ### TESTS
  29.  
  30. >>> b = Blog()
  31. >>> b.name = "Foo Blog"
  32. >>> b.user = User.objects.create(username='foo', password='test')
  33. >>> b.slug = 'foo-blog'
  34. >>> b.save()
  35. >>> print b
  36. Foo Blog
  37. >>> print b.user.username
  38. foo
  39. >>> print b.slug
  40. foo-blog
  41. """
  42. name = models.CharField(
  43. max_length=255,
  44. help_text="Name of your blog. Upto 255 characters"
  45. )
  46. slug = models.SlugField()
  47. description = models.TextField(
  48. blank=True,
  49. help_text="Describe your blog."
  50. )
  51. user = models.ForeignKey(User, related_name='blog')
  52.  
  53. def __unicode__(self):
  54. return self.name
  55.  
  56. class Post(TimeStampedActivate):
  57. """
  58. A post that belongs to a blog
  59.  
  60. ### TESTS
  61.  
  62. >>> b = Blog.objects.get(id=1)
  63. >>> p = Post()
  64. >>> p.title = "A test post"
  65. >>> p.blog = b
  66. >>> p.body = "This here is the body."
  67. >>> p.slug = 'a-test-post'
  68. >>> p.save()
  69. >>> print p.blog.name
  70. Foo Blog
  71. >>> print p.active
  72. False
  73.  
  74. """
  75.  
  76. title = models.CharField(
  77. max_length=255,
  78. help_text="Title of the post, upto 255 characters long."
  79. )
  80.  
  81. slug = models.SlugField()
  82. excerpt = models.TextField(
  83. blank=True,
  84. help_text="A small teaser of your content."
  85. )
  86.  
  87. body = models.TextField(
  88. blank=False,
  89. help_text="The main content of your post."
  90. )
  91. publish_at = models.DateTimeField(
  92. default=datetime.datetime.now(),
  93. help_text="Date and time post should become visible."
  94. )
  95. tags = TaggableManager()
  96.  
  97. blog = models.ForeignKey(Blog, related_name="posts")
  98. objects = PostManager()
  99.  
  100. def __unicode__(self):
  101. return self.title
  102.  
  103.  
  104. class Meta:
  105. ordering = ['-publish_at', '-modified', '-created']
Add Comment
Please, Sign In to add comment