-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathviews.py
115 lines (95 loc) · 3.96 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from django.shortcuts import get_object_or_404
from django.views.generic import ListView, DetailView
from django.contrib.postgres.search import (
SearchVector, SearchQuery, SearchRank
)
from django.views.generic.edit import FormMixin, FormView
from django.core.mail import send_mail
from django.db.models import Count
from taggit.models import Tag
from .models import Post, Comment
from .forms import CommentForm, EmailPostForm
class PostListView(ListView):
paginate_by = 10
template_name = 'blog/post-list.html'
def get_queryset(self, **kwargs):
queryset = Post.published.all()
tag_slug = self.kwargs.get('tag_slug')
query = self.request.GET.get('query')
if tag_slug:
tag = get_object_or_404(Tag, slug=tag_slug)
queryset = queryset.filter(tags__in=[tag])
if query:
search_vector = SearchVector('title', weight='A') +\
SearchVector('body', weight='B')
search_query = SearchQuery(query)
queryset = Post.published.annotate(
rank=SearchRank(search_vector, search_query)
).filter(rank__gte=0.3).order_by('-rank')
return queryset
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["tag"] = self.kwargs.get('tag_slug')
context["query"] = self.request.GET.get('query')
return context
class PostDetailView(FormMixin, DetailView):
template_name = 'blog/post-detail.html'
form_class = CommentForm
def get_object(self):
obj = get_object_or_404(Post, status='published',
slug=self.kwargs.get('slug'),
publish__year=self.kwargs.get('year'),
publish__month=self.kwargs.get('month'),
publish__day=self.kwargs.get('day')
)
return obj
def get_success_url(self):
return self.get_object().get_absolute_url()
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
post = self.get_object()
post_tags_ids = post.tags.values_list('id', flat=True)
similar_posts = Post.published.filter(tags__in=post_tags_ids)\
.exclude(id=post.id)
similar_posts = similar_posts.annotate(same_tags=Count('tags'))\
.order_by('-same_tags', '-publish')[:4]
comments = Comment.objects.filter(post=post, active=True)
context["similar_posts"] = similar_posts
context['comments'] = comments
context['form'] = self.get_form()
return context
def post(self, request, *args, **kwargs):
self.object = self.get_object()
form = self.get_form()
if form.is_valid():
comment = form.save(commit=False)
comment.post = self.get_object()
comment.save()
return self.form_valid(form)
else:
print("Invalid form")
return self.form_invalid(form)
class PostShareView(FormView):
form_class = EmailPostForm
template_name = 'blog/post-share.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["post"] = get_object_or_404(
Post, id=self.kwargs.get('post_id'))
context["sent"] = False
return context
def form_valid(self, form):
context = self.get_context_data()
# Send Email
cd = form.cleaned_data
post = context['post']
post_url = self.request.build_absolute_uri(
post.get_absolute_url()
)
subject = f"{cd['name']} recommends you read {post.title}"
message = f"Read {post.title} at {post_url}\n\n" \
f"{cd['name']}\'s comments: {cd['comments']}"
send_mail(subject, message, 'admin@myblog.com', [cd['to']])
context['sent'] = True
context['form'] = form
return self.render_to_response(context)