Coverage for posts/views.py: 60.00%

93 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.mixins import LoginRequiredMixin, UserPassesTestMixin 1a

4from django.contrib.postgres.search import SearchQuery, SearchRank, SearchVector 1a

5from django.core.mail import send_mail 1a

6from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator 1a

7from django.db.models import Count 1a

8from django.shortcuts import get_object_or_404, render 1a

9from django.urls import reverse_lazy 1a

10from django.views.decorators.http import require_POST 1a

11from django.views.generic.edit import CreateView, DeleteView, UpdateView 1a

12from taggit.models import Tag 1a

13 

14from .forms import CommentForm, EmailPostForm, SearchForm 1a

15from .models import Post 1a

16 

17 

18def post_list(request, tag_slug=None): 1a

19 post_list = Post.published.all() 1a

20 tag = None 1a

21 if tag_slug: 21 ↛ 22line 21 didn't jump to line 22 because the condition on line 21 was never true1a

22 tag = get_object_or_404(Tag, slug=tag_slug) 

23 post_list = post_list.filter(tags__in=[tag]) 

24 paginator = Paginator(post_list, 3) 1a

25 page_number = request.GET.get("page", 1) 1a

26 try: 1a

27 posts = paginator = paginator.page(page_number) 1a

28 except PageNotAnInteger: 

29 posts = paginator.page(1) 

30 except EmptyPage: 

31 posts = paginator.page(paginator.num_pages) 

32 return render(request, "posts/post_list.html", {"posts": posts, "tag": tag}) 1a

33 

34 

35def post_detail(request, year, month, day, post): 1a

36 post = get_object_or_404( 1a

37 Post, 

38 status=Post.Status.PUBLISHED, 

39 slug=post, 

40 publish__year=year, 

41 publish__month=month, 

42 publish__day=day, 

43 ) 

44 comments = post.comments.filter(active=True) 1a

45 form = CommentForm() 1a

46 

47 post_tags_ids = post.tags.values_list("id", flat=True) 1a

48 similar_posts = Post.published.filter(tags__in=post_tags_ids).exclude(id=post.id) 1a

49 similar_posts = similar_posts.annotate(same_tags=Count("tags")).order_by( 1a

50 "-same_tags", "-publish" 

51 )[:4] 

52 

53 return render( 1a

54 request, 

55 "posts/post_detail.html", 

56 { 

57 "post": post, 

58 "comments": comments, 

59 "form": form, 

60 "similar_posts": similar_posts, 

61 }, 

62 ) 

63 

64 

65class PostCreateView(LoginRequiredMixin, CreateView): 1a

66 model = Post 1a

67 template_name = "posts/post_new.html" 1a

68 fields: ClassVar[list[str]] = ["title", "tags", "status", "body"] 1a

69 

70 def form_valid(self, form): 1a

71 form.instance.author = self.request.user 

72 return super().form_valid(form) 

73 

74 

75class PostUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView): 1a

76 model = Post 1a

77 template_name = "posts/post_edit.html" 1a

78 fields: ClassVar[list[str]] = ["title", "tags", "status", "body"] 1a

79 

80 def test_func(self): 1a

81 obj = self.get_object() 1a

82 return obj.author == self.request.user 1a

83 

84 

85class PostDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView): 1a

86 model = Post 1a

87 template_name = "posts/post_delete.html" 1a

88 success_url = reverse_lazy("post_list") 1a

89 

90 def test_func(self): 1a

91 obj = self.get_object() 1a

92 return obj.author == self.request.user 1a

93 

94 

95def post_share(request, post_id): 1a

96 post = get_object_or_404(Post, id=post_id, status=Post.Status.PUBLISHED) 

97 sent = False 

98 

99 if request.method == "POST": 

100 form = EmailPostForm(request.POST) 

101 if form.is_valid(): 

102 # fmt: off 

103 cd = form.cleaned_data 

104 post_url = request.build_absolute_uri(post.get_absolute_url()) 

105 subject = f"{cd['name']} recommends you read " f"{post.title}" 

106 message = ( 

107 f"Read {post.title} at {post_url}\n\n" 

108 f"{cd['name']}'s comments: {cd['comments']}" 

109 ) 

110 send_mail(subject, message, "kevin.bowen@gmail.com", [cd["to"]]) 

111 sent = True 

112 # fmt: on 

113 else: 

114 form = EmailPostForm() 

115 return render( 

116 request, "posts/post_share.html", {"post": post, "form": form, "sent": sent} 

117 ) 

118 

119 

120def post_search(request): 1a

121 form = SearchForm() 

122 query = None 

123 results = [] 

124 

125 if "query" in request.GET: 

126 form = SearchForm(request.GET) 

127 if form.is_valid(): 

128 query = form.cleaned_data["query"] 

129 search_vector = SearchVector("title", weight="A") + SearchVector( 

130 "body", weight="B" 

131 ) 

132 search_query = SearchQuery(query) 

133 results = ( 

134 Post.published 

135 .annotate( 

136 search=search_vector, rank=SearchRank(search_vector, search_query) 

137 ) 

138 .filter(rank__gte=0.3) 

139 .order_by("-rank") 

140 ) 

141 

142 return render( 

143 request, "posts/search.html", {"form": form, "query": query, "results": results} 

144 ) 

145 

146 

147@require_POST 1a

148def comment_add(request, post_id): 1a

149 post = get_object_or_404(Post, id=post_id, status=Post.Status.PUBLISHED) 1a

150 comment = None 1a

151 form = CommentForm(data=request.POST) 1a

152 if form.is_valid(): 152 ↛ 156line 152 didn't jump to line 156 because the condition on line 152 was always true1a

153 comment = form.save(commit=False) 1a

154 comment.post = post 1a

155 comment.save() 1a

156 return render( 1a

157 request, 

158 "posts/comment_add.html", 

159 {"post": post, "form": form, "comment": comment}, 

160 )