Coverage for posts/models.py: 100.00%

49 statements  

« prev     ^ index     » next       coverage.py v7.16.1, created at 2026-09-22 12:02 -0700

1from django.contrib.auth import get_user_model 1a

2from django.db import models 1a

3from django.template.defaultfilters import slugify 1a

4from django.urls import reverse 1a

5from django.utils import timezone 1a

6from taggit.managers import TaggableManager 1a

7 

8 

9class PublishedManager(models.Manager): 1a

10 def get_queryset(self): 1a

11 return super().get_queryset().filter(status=Post.Status.PUBLISHED) 1a

12 

13 

14""" 1a

15class DraftManager(models.Manager): 

16 def get_queryset(self): 

17 return super().get_queryset().filter(status=Post.Status.DRAFT) 

18""" 

19 

20 

21class Post(models.Model): 1a

22 class Status(models.TextChoices): 1a

23 DRAFT = "DF", "Draft" 1a

24 PUBLISHED = "PB", "Published" 1a

25 

26 title = models.CharField(max_length=250) 1a

27 slug = models.SlugField( 1a

28 max_length=250, 

29 null=False, 

30 unique_for_date="publish", 

31 ) 

32 author = models.ForeignKey( 1a

33 get_user_model(), 

34 on_delete=models.CASCADE, 

35 related_name="blog_posts", 

36 ) 

37 body = models.TextField() 1a

38 publish = models.DateTimeField(default=timezone.now) 1a

39 created = models.DateTimeField(auto_now_add=True) 1a

40 updated = models.DateTimeField(auto_now=True) 1a

41 status = models.CharField( 1a

42 max_length=2, choices=Status.choices, default=Status.PUBLISHED 

43 ) 

44 

45 objects = models.Manager() 1a

46 # draft = DraftManager() 

47 published = PublishedManager() 1a

48 tags = TaggableManager() 1a

49 

50 class Meta: 1a

51 ordering = ["-publish"] 1a

52 indexes = [ 1a

53 models.Index(fields=["-publish"]), 

54 ] 

55 

56 def __str__(self): 1a

57 return self.title 1a

58 

59 def save(self, *args, **kwargs): 1a

60 if not self.slug: 1a

61 self.slug = slugify(self.title) 1a

62 return super().save(*args, **kwargs) 1a

63 

64 def get_absolute_url(self): 1a

65 return reverse( 1a

66 "post_detail", 

67 args=[self.publish.year, self.publish.month, self.publish.day, self.slug], 

68 ) 

69 

70 

71class Comment(models.Model): 1a

72 post = models.ForeignKey( 1a

73 Post, 

74 on_delete=models.CASCADE, 

75 related_name="comments", 

76 ) 

77 name = models.CharField(max_length=80) 1a

78 email = models.EmailField() 1a

79 body = models.TextField() 1a

80 created = models.DateTimeField(auto_now_add=True) 1a

81 updated = models.DateTimeField(auto_now=True) 1a

82 active = models.BooleanField(default=True) 1a

83 

84 class Meta: 1a

85 ordering = ["created"] 1a

86 indexes = [ 1a

87 models.Index(fields=["created"]), 

88 ] 

89 

90 def __str__(self): 1a

91 return f"Comment by {self.name} on {self.post}" 1a