Coverage for posts/models.py: 100.00%

50 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-10 10:53 -0800

1from typing import ClassVar 1a

2 

3from django.contrib.auth import get_user_model 1a

4from django.db import models 1a

5from django.template.defaultfilters import slugify 1a

6from django.urls import reverse 1a

7from django.utils import timezone 1a

8from taggit.managers import TaggableManager 1a

9 

10 

11class PublishedManager(models.Manager): 1a

12 def get_queryset(self): 1a

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

14 

15 

16""" 1a

17class DraftManager(models.Manager): 

18 def get_queryset(self): 

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

20""" 

21 

22 

23class Post(models.Model): 1a

24 class Status(models.TextChoices): 1a

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

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

27 

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

29 slug = models.SlugField( 1a

30 max_length=250, 

31 null=False, 

32 unique_for_date="publish", 

33 ) 

34 author = models.ForeignKey( 1a

35 get_user_model(), 

36 on_delete=models.CASCADE, 

37 related_name="blog_posts", 

38 ) 

39 body = models.TextField() 1a

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

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

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

43 status = models.CharField( 1a

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

45 ) 

46 

47 objects = models.Manager() 1a

48 # draft = DraftManager() 

49 published = PublishedManager() 1a

50 tags = TaggableManager() 1a

51 

52 class Meta: 1a

53 ordering: ClassVar[list[str]] = ["-publish"] 1a

54 indexes: ClassVar[list] = [ 1a

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

56 ] 

57 

58 def __str__(self): 1a

59 return self.title 1a

60 

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

62 if not self.slug: 1a

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

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

65 

66 def get_absolute_url(self): 1a

67 return reverse( 1a

68 "post_detail", 

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

70 ) 

71 

72 

73class Comment(models.Model): 1a

74 post = models.ForeignKey( 1a

75 Post, 

76 on_delete=models.CASCADE, 

77 related_name="comments", 

78 ) 

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

80 email = models.EmailField() 1a

81 body = models.TextField() 1a

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

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

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

85 

86 class Meta: 1a

87 ordering: ClassVar[list[str]] = ["created"] 1a

88 indexes: ClassVar[list] = [ 1a

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

90 ] 

91 

92 def __str__(self): 1a

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