forked from ericls/niji
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
335 lines (284 loc) · 11.1 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# -*- coding: utf-8 -*-
from django.shortcuts import render
from django.http import HttpResponseRedirect, HttpResponseForbidden
from django.core.urlresolvers import reverse
from django.contrib.auth import authenticate, login, logout
from django.contrib import messages
from django.contrib.auth import get_user_model
from django.views.generic import ListView
from django.utils.translation import ugettext as _
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required
from .models import Topic, Node, Post, Notification, ForumAvatar
from .forms import TopicForm, TopicEditForm, AppendixForm, ForumAvatarForm, ReplyForm
from .misc import get_query
import re
EMAIL_REGEX = re.compile(r"[^@]+@[^@]+\.[^@]+")
User = get_user_model()
# Create your views here.
class Index(ListView):
model = Topic
paginate_by = 30
template_name = 'niji/index.html'
context_object_name = 'topics'
def get_queryset(self):
return Topic.objects.visible().select_related(
'user', 'node'
).prefetch_related(
'user__forum_avatar'
)
def get_context_data(self, **kwargs):
context = super(ListView, self).get_context_data(**kwargs)
context['panel_title'] = _('New Topics')
context['title'] = _('Index')
return context
class NodeView(ListView):
model = Topic
paginate_by = 30
template_name = 'niji/node.html'
context_object_name = 'topics'
def get_queryset(self):
return Topic.objects.visible().filter(
node__id=self.kwargs.get('pk')
).select_related(
'user', 'node'
).prefetch_related(
'user__forum_avatar'
)
def get_context_data(self, **kwargs):
context = super(ListView, self).get_context_data(**kwargs)
context['node'] = node = Node.objects.get(pk=self.kwargs.get('pk'))
context['title'] = context['panel_title'] = node.title
return context
class TopicView(ListView):
model = Post
paginate_by = 30
template_name = 'niji/topic.html'
context_object_name = 'posts'
def get_queryset(self):
return Post.objects.visible().filter(
topic_id=self.kwargs.get('pk')
).select_related(
'user'
).prefetch_related(
'user__forum_avatar'
).order_by('pub_date')
def get_context_data(self, **kwargs):
context = super(ListView, self).get_context_data(**kwargs)
current = Topic.objects.visible().get(pk=self.kwargs.get('pk'))
current.increase_view_count()
context['topic'] = current
context['title'] = context['topic'].title
context['node'] = context['topic'].node
context['form'] = ReplyForm()
return context
@method_decorator(login_required)
def post(self, request, *args, **kwargs):
topic_id=self.kwargs.get('pk')
form = ReplyForm(
request.POST,
user=request.user,
topic_id=topic_id
)
if form.is_valid():
form.save()
return HttpResponseRedirect(
reverse('niji:topic', kwargs={'pk': topic_id})
)
def user_info(request, pk):
u = User.objects.get(pk=pk)
return render(request, 'niji/user_info.html', {
'title': u.username,
'user': u,
'topics': u.topics.visible().select_related('node')[:10],
'replies': u.posts.visible().select_related('topic', 'user').order_by('-pub_date')[:30],
})
class UserTopics(ListView):
model = Post
paginate_by = 30
template_name = 'niji/user_topics.html'
context_object_name = 'topics'
def get_queryset(self):
return Topic.objects.visible().filter(
user_id=self.kwargs.get('pk')
).select_related(
'user', 'node'
).prefetch_related(
'user__forum_avatar'
)
def get_context_data(self, **kwargs):
context = super(ListView, self).get_context_data(**kwargs)
context['user'] = User.objects.get(pk=self.kwargs.get('pk'))
context['panel_title'] = context['title'] = context['user'].username
return context
class SearchView(ListView):
model = Topic
paginate_by = 30
template_name = 'niji/search.html'
context_object_name = 'topics'
def get_queryset(self):
keywords = self.kwargs.get('keyword')
query = get_query(keywords, ['title'])
return Topic.objects.visible().filter(
query
).select_related(
'user', 'node'
).prefetch_related(
'user__forum_avatar'
)
def get_context_data(self, **kwargs):
context = super(ListView, self).get_context_data(**kwargs)
context['title'] = context['panel_title'] = _('Search: ') + self.kwargs.get('keyword')
return context
def search_redirect(request):
if request.method == 'GET':
keyword = request.GET.get('keyword')
return HttpResponseRedirect(reverse('niji:search', kwargs={'keyword': keyword}))
else:
return HttpResponseForbidden('Post you cannot')
@login_required
def create_topic(request):
if request.method == 'POST':
form = TopicForm(request.POST, user=request.user)
if form.is_valid():
t = form.save()
return HttpResponseRedirect(reverse('niji:topic', kwargs={'pk': t.pk}))
else:
form = TopicForm()
return render(request, 'niji/create_topic.html', {'form': form, 'title': _('Create Topic')})
@login_required
def edit_topic(request, pk):
topic = Topic.objects.get(pk=pk)
if topic.reply_count > 0:
return HttpResponseForbidden(_('Editing is not allowed when topic has been replied'))
if not topic.user == request.user:
return HttpResponseForbidden(_('You are not allowed to edit other\'s topic'))
if request.method == 'POST':
form = TopicEditForm(request.POST, instance=topic)
if form.is_valid():
t = form.save()
return HttpResponseRedirect(reverse('niji:topic', kwargs={'pk': t.pk}))
else:
form = TopicEditForm(instance=topic)
return render(request, 'niji/edit_topic.html', {'form': form, 'title': _('Edit Topic')})
@login_required
def create_appendix(request, pk):
topic = Topic.objects.get(pk=pk)
if not topic.user == request.user:
return HttpResponseForbidden(_('You are not allowed to append other\'s topic'))
if request.method == 'POST':
form = AppendixForm(request.POST, topic=topic)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('niji:topic', kwargs={'pk': topic.pk}))
else:
form = AppendixForm()
return render(request, 'niji/create_appendix.html', {
'form': form, 'title': _('Create Appendix'), 'pk': pk
})
@login_required
def upload_avatar(request):
avatar = ForumAvatar.objects.filter(user_id=request.user.id).first()
if request.method == 'POST':
if avatar:
form = ForumAvatarForm(request.POST, request.FILES, instance=avatar, user=request.user)
else:
form = ForumAvatarForm(request.POST, request.FILES, user=request.user)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('niji:index'))
else:
if avatar:
form = ForumAvatarForm(instance=request.user.forum_avatar)
else:
form = ForumAvatarForm()
return render(request, 'niji/upload_avatar.html', {'form': form, 'title': _('Upload Avatar')})
@login_required
def notification_view(request):
notifications = request.user.received_notifications.all().order_by('-pub_date')
Notification.objects.filter(to=request.user).update(read=True)
return render(request, 'niji/notifications.html', {
'title': _("Notifications"),
'notifications': notifications,
})
class NotificationView(ListView):
model = Notification
paginate_by = 30
template_name = 'niji/notifications.html'
context_object_name = 'notifications'
def get_queryset(self):
Notification.objects.filter(to=self.request.user).update(read=True)
return Notification.objects.filter(
to=self.request.user
).select_related(
'sender', 'topic', 'post'
).prefetch_related(
'sender__forum_avatar', 'post__topic'
).order_by('-pub_date')
def get_context_data(self, **kwargs):
context = super(ListView, self).get_context_data(**kwargs)
context['title'] = _("Notifications")
return context
def login_view(request):
if request.method == "GET":
return render(request, 'niji/login.html', {'title': _("Login")})
if request.method == "POST":
username = request.POST.get('username')
password = request.POST.get('password')
valid = True
if not username or not password:
valid = False
messages.add_message(request, messages.INFO, _("Username and password cannot be empty"))
user = User.objects.filter(username=username).first()
if not user:
valid = False
messages.add_message(request, messages.INFO, _("User does not exist"))
user = authenticate(username=username, password=password)
if (user is not None) and valid:
if user.is_active:
login(request, user)
return HttpResponseRedirect(reverse('niji:index'))
else:
valid = False
messages.add_message(request, messages.INFO, _("User deactivated"))
else:
valid = False
messages.add_message(request, messages.INFO, _("Incorrect password"))
if not valid:
return HttpResponseRedirect(reverse("niji:login"))
def reg_view(request):
if request.method == "GET":
return render(request, 'niji/reg.html', {'title': _("Reg")})
if request.method == "POST":
username = request.POST.get("username")
email = request.POST.get("email")
password = request.POST.get("password1")
password2 = request.POST.get("password2")
valid = True
if User.objects.filter(username=username).exists():
valid = False
messages.add_message(request, messages.INFO, _("User already exists"))
if password != password2:
valid = False
messages.add_message(request, messages.INFO, _("Password does not match"))
if not EMAIL_REGEX.match(email):
valid = False
messages.add_message(request, messages.INFO, _("Invalid Email"))
if not valid:
return HttpResponseRedirect(reverse("niji:reg"))
else:
User.objects.create_user(
username=username,
password=password,
email=email
)
user = authenticate(
username=username,
password=password
)
login(request, user)
return HttpResponseRedirect(reverse("niji:index"))
@login_required
def logout_view(request):
logout(request)
return HttpResponseRedirect(reverse("niji:index"))