Mercurial > public > sg101
comparison messages/views.py @ 810:4a4fa174a0ec
Private message refactor: adding ability to report PM's.
See also Bitbucket issue #57.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 06 Sep 2014 16:58:08 -0500 |
parents | 22cc8c1aec03 |
children | 42436d674ba8 |
comparison
equal
deleted
inserted
replaced
809:ab3deff7672a | 810:4a4fa174a0ec |
---|---|
10 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger | 10 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger |
11 from django.core.urlresolvers import reverse | 11 from django.core.urlresolvers import reverse |
12 from django.shortcuts import get_object_or_404 | 12 from django.shortcuts import get_object_or_404 |
13 from django.shortcuts import render, redirect | 13 from django.shortcuts import render, redirect |
14 | 14 |
15 from messages.models import Message, Options | 15 from messages.models import Flag, Message, Options |
16 from messages.forms import OptionsForm, ComposeForm | 16 from messages.forms import OptionsForm, ComposeForm, ReportForm |
17 from messages.utils import reply_subject | 17 from messages.utils import reply_subject |
18 from messages import MSG_BOX_LIMIT | 18 from messages import MSG_BOX_LIMIT |
19 from core.functions import quote_message | 19 from core.functions import quote_message |
20 | 20 |
21 | 21 |
182 else: | 182 else: |
183 initial_data['receiver'] = msg.receiver.username | 183 initial_data['receiver'] = msg.receiver.username |
184 | 184 |
185 form = ComposeForm(request.user, initial=initial_data) | 185 form = ComposeForm(request.user, initial=initial_data) |
186 | 186 |
187 try: | |
188 msg_flag = msg.flag | |
189 except Flag.DoesNotExist: | |
190 msg_flag = None | |
191 | |
187 return render(request, 'messages/view_message.html', { | 192 return render(request, 'messages/view_message.html', { |
188 'msg': msg, | 193 'msg': msg, |
189 'form': form, | 194 'form': form, |
195 'msg_flag': msg_flag, | |
190 }) | 196 }) |
191 | 197 |
192 | 198 |
193 def _delete_pms(user, pm_ids): | 199 def _delete_pms(user, pm_ids): |
194 """ | 200 """ |
293 except ValueError: | 299 except ValueError: |
294 page = 1 | 300 page = 1 |
295 | 301 |
296 url = reverse('messages-trash') + '?page={}'.format(page) | 302 url = reverse('messages-trash') + '?page={}'.format(page) |
297 return redirect(url) | 303 return redirect(url) |
304 | |
305 | |
306 @login_required | |
307 def report(request, msg_id): | |
308 """This view is for reporting a PM as spam or abuse. | |
309 | |
310 """ | |
311 msg = get_object_or_404(Message.objects.select_related(), pk=msg_id) | |
312 if msg.receiver != request.user: | |
313 django_messages.error(request, "You can't report this message.") | |
314 return redirect('messages-inbox') | |
315 try: | |
316 msg.flag | |
317 except Flag.DoesNotExist: | |
318 pass | |
319 else: | |
320 django_messages.error(request, "This message has already been reported.") | |
321 return redirect('messages-inbox') | |
322 | |
323 if request.method == 'POST': | |
324 form = ReportForm(request.POST) | |
325 if form.is_valid(): | |
326 flag = form.save(commit=False) | |
327 flag.message = msg | |
328 flag.save() | |
329 django_messages.success(request, | |
330 'Message reported. An admin will be notified. Thank you.') | |
331 return redirect('messages-inbox') | |
332 else: | |
333 form = ReportForm() | |
334 | |
335 return render(request, 'messages/report_message.html', { | |
336 'msg': msg, | |
337 'form': form, | |
338 }) |