views.py
13.7 KB
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
from django.shortcuts import get_object_or_404, redirect, render
from django.views import generic
from django.contrib import messages
from django.http import JsonResponse
from django.template.loader import render_to_string
from django.core.urlresolvers import reverse, reverse_lazy
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.decorators import login_required
from django.db.models import Q, Count
from channels import Group
import json
from users.models import User
from .models import Mural, GeneralPost, CategoryPost, SubjectPost, MuralVisualizations, MuralFavorites, Comment
from .forms import GeneralPostForm, CommentForm
class GeneralIndex(LoginRequiredMixin, generic.ListView):
login_url = reverse_lazy("users:login")
redirect_field_name = 'next'
template_name = 'mural/list.html'
context_object_name = "posts"
paginate_by = 10
totals = {}
def get_queryset(self):
user = self.request.user
favorites = self.request.GET.get('favorite', False)
mines = self.request.GET.get('mine', False)
showing = self.request.GET.get('showing', False)
page = self.request.GET.get('page', False)
if not favorites:
if mines:
general = GeneralPost.objects.extra(select = {"most_recent": "greatest(last_update, (select max(mural_comment.last_update) from mural_comment where mural_comment.post_id = mural_generalpost.mural_ptr_id))"}).filter(mural_ptr__user = user)
else:
general = GeneralPost.objects.extra(select = {"most_recent": "greatest(last_update, (select max(mural_comment.last_update) from mural_comment where mural_comment.post_id = mural_generalpost.mural_ptr_id))"})
else:
if mines:
general = GeneralPost.objects.extra(select = {"most_recent": "greatest(last_update, (select max(mural_comment.last_update) from mural_comment where mural_comment.post_id = mural_generalpost.mural_ptr_id))"}).filter(favorites_post__isnull = False, favorites_post__user = user, mural_ptr__user = user)
else:
general = GeneralPost.objects.extra(select = {"most_recent": "greatest(last_update, (select max(mural_comment.last_update) from mural_comment where mural_comment.post_id = mural_generalpost.mural_ptr_id))"}).filter(favorites_post__isnull = False, favorites_post__user = user)
if showing: #Exclude ajax creation posts results
showing = showing.split(',')
general = general.exclude(id__in = showing)
if not page: #Don't need this if is pagination
general_visualizations = MuralVisualizations.objects.filter(Q(user = user) & Q(viewed = False) & (Q(post__generalpost__isnull = False) | Q(comment__post__generalpost__isnull = False))).distinct()
self.totals['general'] = general_visualizations.count()
self.totals['category'] = MuralVisualizations.objects.filter(Q(user = user) & Q(viewed = False) & (Q(post__categorypost__space__coordinators = user) | Q(comment__post__categorypost__space__coordinators = user) | Q(post__categorypost__space__subject_category__professor = user) | Q(post__categorypost__space__subject_category__students = user) | Q(comment__post__categorypost__space__subject_category__professor = user) | Q(comment__post__categorypost__space__subject_category__students = user))).distinct().count()
self.totals['subject'] = MuralVisualizations.objects.filter(Q(user = user) & Q(viewed = False) & (Q(post__subjectpost__space__professor = user) | Q(comment__post__subjectpost__space__professor = user) | Q(post__subjectpost__space__students = user) | Q(comment__post__subjectpost__space__students = user))).distinct().count()
general_visualizations.update(viewed = True)
return general.order_by("-most_recent")
def get_context_data(self, **kwargs):
context = super(GeneralIndex, self).get_context_data(**kwargs)
page = self.request.GET.get('page', '')
if page:
self.template_name = "mural/_list_view.html"
context['title'] = _('Mural')
context['totals'] = self.totals
context['mural_menu_active'] = 'subjects_menu_active'
context['favorites'] = ""
context['mines'] = ""
favs = self.request.GET.get('favorite', False)
if favs:
context['favorites'] = "checked"
mines = self.request.GET.get('mine', False)
if mines:
context['mines'] = "checked"
return context
class GeneralCreate(LoginRequiredMixin, generic.edit.CreateView):
login_url = reverse_lazy("users:login")
redirect_field_name = 'next'
template_name = 'mural/_form.html'
form_class = GeneralPostForm
def form_invalid(self, form):
context = super(GeneralCreate, self).form_invalid(form)
context.status_code = 400
return context
def form_valid(self, form):
self.object = form.save(commit = False)
self.object.user = self.request.user
self.object.save()
users = User.objects.all().exclude(id = self.request.user.id)
entries = []
notify_type = "mural"
user_icon = self.object.user.image_url
_view = render_to_string("mural/_view.html", {"post": self.object}, self.request)
simple_notify = _("%s has made a post in General")%(str(self.object.user))
pathname = reverse("mural:manage_general")
for user in users:
entries.append(MuralVisualizations(viewed = False, user = user, post = self.object))
Group("user-%s" % user.id).send({'text': json.dumps({"type": notify_type, "subtype": "create", "user_icon": user_icon, "pathname": pathname, "simple": simple_notify, "complete": _view})})
MuralVisualizations.objects.bulk_create(entries)
return super(GeneralCreate, self).form_valid(form)
def get_context_data(self, *args, **kwargs):
context = super(GeneralCreate, self).get_context_data(*args, **kwargs)
context['form_url'] = reverse_lazy("mural:create_general")
return context
def get_success_url(self):
return reverse_lazy('mural:render_post_general', args = (self.object.id, 'create', ))
class GeneralUpdate(LoginRequiredMixin, generic.UpdateView):
login_url = reverse_lazy("users:login")
redirect_field_name = 'next'
template_name = 'mural/_form.html'
model = GeneralPost
form_class = GeneralPostForm
def form_invalid(self, form):
context = super(GeneralUpdate, self).form_invalid(form)
context.status_code = 400
return context
def form_valid(self, form):
self.object = form.save(commit = False)
self.object.edited = True
self.object.save()
users = User.objects.all().exclude(id = self.request.user.id)
notify_type = "mural"
_view = render_to_string("mural/_view.html", {"post": self.object}, self.request)
pathname = reverse("mural:manage_general")
for user in users:
Group("user-%s" % user.id).send({'text': json.dumps({"type": notify_type, "subtype": "update", "pathname": pathname, "complete": _view, "post_id": self.object.id})})
return super(GeneralUpdate, self).form_valid(form)
def get_context_data(self, *args, **kwargs):
context = super(GeneralUpdate, self).get_context_data(*args, **kwargs)
context['form_url'] = reverse_lazy("mural:update_general", args = (), kwargs = {'pk': self.object.id})
return context
def get_success_url(self):
return reverse_lazy('mural:render_post_general', args = (self.object.id, 'update', ))
class GeneralDelete(LoginRequiredMixin, generic.DeleteView):
login_url = reverse_lazy("users:login")
redirect_field_name = 'next'
template_name = 'mural/delete.html'
model = GeneralPost
def get_context_data(self, *args, **kwargs):
context = super(GeneralDelete, self).get_context_data(*args, **kwargs)
context['form_url'] = reverse_lazy("mural:delete_general", args = (), kwargs = {'pk': self.object.id})
context['message'] = _('Are you sure you want to delete this post?')
return context
def get_success_url(self):
users = User.objects.all().exclude(id = self.request.user.id)
notify_type = "mural"
pathname = reverse("mural:manage_general")
for user in users:
Group("user-%s" % user.id).send({'text': json.dumps({"type": notify_type, "subtype": "delete", "pathname": pathname, "post_id": self.object.id})})
return reverse_lazy('mural:deleted_post')
def render_gen_post(request, post, msg):
post = get_object_or_404(GeneralPost, id = post)
context = {}
context['post'] = post
msg = ""
if msg == 'create':
msg = _('Your post was published successfully!')
else:
msg = _('Your post was edited successfully!')
html = render_to_string("mural/_view.html", context, request)
return JsonResponse({'message': msg, 'view': html, 'new_id': post.id})
def deleted_post(request):
return JsonResponse({'msg': _('Post deleted successfully!')})
@login_required
def favorite(request, post):
action = request.GET.get('action', '')
post = get_object_or_404(Mural, id = post)
if action == 'favorite':
MuralFavorites.objects.create(post = post, user = request.user)
return JsonResponse({'label': _('Unfavorite')})
else:
MuralFavorites.objects.filter(post = post, user = request.user).delete()
return JsonResponse({'label': _('Favorite')})
class CommentCreate(LoginRequiredMixin, generic.edit.CreateView):
login_url = reverse_lazy("users:login")
redirect_field_name = 'next'
template_name = 'mural/_form_comment.html'
form_class = CommentForm
def form_invalid(self, form):
context = super(CommentCreate, self).form_invalid(form)
context.status_code = 400
return context
def form_valid(self, form):
self.object = form.save(commit = False)
post_id = self.kwargs.get('post', '')
post = get_object_or_404(Mural, id = post_id)
self.object.user = self.request.user
self.object.post = post
self.object.save()
users = User.objects.all().exclude(id = self.request.user.id)
notify_type = "mural"
user_icon = self.object.user.image_url
#_view = render_to_string("mural/_view.html", {"post": self.object}, self.request)
simple_notify = _("%s has made a post in General")%(str(self.object.user))
pathname = reverse("mural:manage_general")
#for user in users:
# entries.append(MuralVisualizations(viewed = False, user = user, post = self.object))
# Group("user-%s" % user.id).send({'text': json.dumps({"type": notify_type, "subtype": "create", "user_icon": user_icon, "pathname": pathname, "simple": simple_notify, "complete": _view})})
return super(CommentCreate, self).form_valid(form)
def get_context_data(self, *args, **kwargs):
context = super(CommentCreate, self).get_context_data(*args, **kwargs)
post_id = self.kwargs.get('post', '')
context['form_url'] = reverse_lazy("mural:create_comment", kwargs = {"post": post_id})
return context
def get_success_url(self):
return reverse_lazy('mural:render_comment', args = (self.object.id, 'create', ))
class CommentUpdate(LoginRequiredMixin, generic.UpdateView):
login_url = reverse_lazy("users:login")
redirect_field_name = 'next'
template_name = 'mural/_form_comment.html'
model = Comment
form_class = CommentForm
def form_invalid(self, form):
context = super(CommentUpdate, self).form_invalid(form)
context.status_code = 400
return context
def form_valid(self, form):
self.object = form.save(commit = False)
self.object.edited = True
self.object.save()
users = User.objects.all().exclude(id = self.request.user.id)
entries = []
notify_type = "mural"
user_icon = self.object.user.image_url
#_view = render_to_string("mural/_view.html", {"post": self.object}, self.request)
simple_notify = _("%s has made a post in General")%(str(self.object.user))
pathname = reverse("mural:manage_general")
#for user in users:
# entries.append(MuralVisualizations(viewed = False, user = user, post = self.object))
# Group("user-%s" % user.id).send({'text': json.dumps({"type": notify_type, "subtype": "create", "user_icon": user_icon, "pathname": pathname, "simple": simple_notify, "complete": _view})})
#MuralVisualizations.objects.bulk_create(entries)
return super(CommentUpdate, self).form_valid(form)
def get_context_data(self, *args, **kwargs):
context = super(CommentUpdate, self).get_context_data(*args, **kwargs)
context['form_url'] = reverse_lazy("mural:update_comment", args = (), kwargs = {'pk': self.object.id})
return context
def get_success_url(self):
return reverse_lazy('mural:render_comment', args = (self.object.id, 'update', ))
class CommentDelete(LoginRequiredMixin, generic.DeleteView):
login_url = reverse_lazy("users:login")
redirect_field_name = 'next'
template_name = 'mural/delete.html'
model = Comment
def get_context_data(self, *args, **kwargs):
context = super(CommentDelete, self).get_context_data(*args, **kwargs)
context['form_url'] = reverse_lazy("mural:delete_comment", args = (), kwargs = {'pk': self.object.id})
context['message'] = _('Are you sure you want to delete this comment?')
return context
def get_success_url(self):
users = User.objects.all().exclude(id = self.request.user.id)
notify_type = "mural"
pathname = reverse("mural:manage_general")
#for user in users:
# Group("user-%s" % user.id).send({'text': json.dumps({"type": notify_type, "subtype": "delete", "pathname": pathname, "post_id": self.object.id})})
return reverse_lazy('mural:deleted_comment')
def render_comment(request, comment, msg):
comment = get_object_or_404(Comment, id = comment)
context = {}
context['comment'] = comment
msg = ""
if msg == 'create':
msg = _('Your comment was published successfully!')
else:
msg = _('Your comment was edited successfully!')
html = render_to_string("mural/_view_comment.html", context, request)
return JsonResponse({'message': msg, 'view': html, 'new_id': comment.id})
def deleted_comment(request):
return JsonResponse({'msg': _('Comment deleted successfully!')})
def suggest_users(request):
param = request.GET.get('param', '')
users = User.objects.filter(Q(username__icontains = param) | Q(last_name__icontains = param) | Q(social_name__icontains = param)).exclude(id = request.user.id)[:5]
context = {}
context['users'] = users
response = render_to_string("mural/_user_suggestions_list.html", context, request)
return JsonResponse({"search_result": response})