generated from Code-Institute-Org/gitpod-full-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathviews.py
223 lines (168 loc) · 7.34 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
from django.shortcuts import render, get_object_or_404, redirect, reverse
from django.db.models import Q
from django.views.generic import ListView, UpdateView
from django.views import generic, View
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.contrib.auth.mixins import LoginRequiredMixin
from .forms import PostForm, CommentForm
from .models import Post, User, Comment, Topic
from django.template.defaultfilters import slugify
from django.contrib import messages
class PostList(View):
"""
Post List Class view gets the posts and topics to be displayed.
It also allows the display to be filtered by topic, author, title
"""
def get(self, request):
q = request.GET.get('q') if request.GET.get('q') is not None else ''
posts = Post.objects.filter(
Q(topic__name__icontains=q) |
Q(author__username__icontains=q) |
Q(title__icontains=q)
)
topics = Topic.objects.all()
context = {"posts": posts, "topics": topics}
return render(request, "index.html", context)
class PostDetail(View):
"""
Post detail class view gets the correct slug to view the
chosen post. It also gets the comment model form to be
used to for the foem and validating it. The comment form cam gets the
amount of objects in the current comments
section to give the count of comments.
"""
def get(self, request, slug, *args, **kwargs):
queryset = Post.objects.all()
post = get_object_or_404(queryset, slug=slug)
comments = post.comment_post_set.all()
number_of_comments = comments.count()
comment_form = CommentForm()
upvoted = False
if post.upvotes.filter(id=self.request.user.id).exists():
upvoted = True
context = {
"post": post,
"comments": comments,
"comment_form": comment_form,
"upvoted": upvoted,
'number_of_comments': number_of_comments
}
return render(request, "post_detail.html", context)
def post(self, request, slug, *args, **kwargs):
queryset = Post.objects.all()
post = get_object_or_404(queryset, slug=slug)
comment_form = CommentForm(request.POST)
comment_form.instance.user = request.user
comment_form.instance.post = post
if comment_form.is_valid():
comment_form.save()
messages.success(request, 'Comment was successful')
return HttpResponseRedirect(self.request.path_info)
class PostCreate(LoginRequiredMixin, View):
""" Uses the post form from models forms to alow users to make posts"""
def get(self, request, *args, **kwargs):
form = PostForm()
context = {'form': form}
return render(request, "post_create.html", context)
def post(self, request, *args, **kwargs):
form = PostForm(request.POST)
form.instance.author = request.user
if form.is_valid():
form.save()
messages.success(request, 'Post was created successfuly')
return redirect(reverse('home'))
class PostUpvote(LoginRequiredMixin, View):
"""
Post upvote view gets the correct post to
see if the current user has upvoted it. If they have
it removes the user if they haven't it adds them.
"""
def post(self, request, slug):
post = get_object_or_404(Post, slug=slug)
if post.upvotes.filter(id=request.user.id).exists():
post.upvotes.remove(request.user)
else:
post.upvotes.add(request.user)
return HttpResponseRedirect(reverse('post_detail', args=[slug]))
class PostUpdate(LoginRequiredMixin, View):
"""
Post update gets the current post with the slug
an displays. It also prefills it for updating.
"""
def get(self, request, slug, *args, **kwargs):
queryset = Post.objects.all()
post = get_object_or_404(queryset, slug=slug)
form = PostForm(instance=post)
context = {'post': post,'form': form}
return render(request, "post_update.html", context)
def post(self, request, slug, *args, **kwargs):
queryset = Post.objects.all()
post = get_object_or_404(queryset, slug=slug)
form = PostForm(request.POST, instance=post)
if form.is_valid():
form.save()
post.slug = slugify(form.instance.title)
post.save()
messages.success(request, 'Post was updated successfully')
return redirect(reverse('home'))
class PostDelete(LoginRequiredMixin, View):
"""
Post delete view gets the selcted post through its slug
and allows the user to delete it
"""
def get(self, request, slug, *args, **kwargs):
queryset = Post.objects.all()
post = get_object_or_404(queryset, slug=slug)
context = {'post': post}
return render(request, "post_delete.html", context)
def post(self, request, slug, *args, **kwargs):
queryset = Post.objects.all()
post = get_object_or_404(queryset, slug=slug)
post.delete()
messages.success(request, 'Post was deleted successfully')
return redirect(reverse('home'))
class CommentUpdate(LoginRequiredMixin, View):
"""
Comment update form gets the correct comment
with the slug and comment id. It prefills it and
lets the user update their comment
"""
def get(self, request, comment_id, *args, **kwargs):
comment = get_object_or_404(Comment, id=comment_id)
comment_form = CommentForm(instance=comment)
context = {'comment_form': comment_form}
return render(request, "comment_update.html", context)
def post(self, request, slug, comment_id, *args, **kwargs):
post_slug = get_object_or_404(Post, slug=slug)
slug = post_slug.slug
comment = get_object_or_404(Comment, id=comment_id)
comment_form = CommentForm(request.POST, instance=comment)
if comment_form.is_valid():
comment_form.save()
messages.success(request, 'Updated comment successfully')
return HttpResponseRedirect(reverse('post_detail', args=[slug]))
class CommentDelete(LoginRequiredMixin, View):
"""
Comment delete view gets the correct comment though
the slug and comment id and allows the user to delete.
"""
def get(self, request, comment_id, *args, **kwargs):
comment = get_object_or_404(Comment, id=comment_id)
context = {'comment': comment}
return render(request, "comment_delete.html", context)
def post(self, request, slug, comment_id, *args, **kwargs):
post_slug = get_object_or_404(Post, slug=slug)
slug = post_slug.slug
comment = get_object_or_404(Comment, id=comment_id)
comment.delete()
messages.success(request, 'Comment has been deleted')
return HttpResponseRedirect(reverse('post_detail', args=[slug]))
class Profile(View):
""" Profile view gets the correct user and enables them to be displayed """
def get(self, request, pk, *args, **kwargs):
user = User.objects.get(id=pk)
posts = user.post_author_set.all()
comments = user.comment_user_set.all()
context = {'user': user, 'posts': posts, 'comments': comments}
return render(request, 'profile.html', context)