comparison messages/views.py @ 818:cf486a8e8b43

Added the ability to export private messages to email.
author Brian Neal <bgneal@gmail.com>
date Sat, 13 Sep 2014 16:19:46 -0500
parents eca0c17ff9c8
children 79a71b9d0a2a
comparison
equal deleted inserted replaced
817:74e84f5fc948 818:cf486a8e8b43
9 from django.contrib import messages as django_messages 9 from django.contrib import messages as django_messages
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 from django.db.models import Q
15 from django.template.loader import render_to_string
16 from django.contrib.sites.models import Site
17 from django.conf import settings
14 18
15 from messages.models import Flag, Message, Options 19 from messages.models import Flag, Message, Options
16 from messages.forms import OptionsForm, ComposeForm, ReportForm 20 from messages.forms import OptionsForm, ComposeForm, ReportForm
17 from messages.utils import reply_subject 21 from messages.utils import reply_subject
18 from messages import MSG_BOX_LIMIT 22 from messages import MSG_BOX_LIMIT
19 from core.functions import email_admins, quote_message 23 from core.functions import email_admins, quote_message, send_mail
20 24
21 25
22 MSGS_PER_PAGE = 20 # message pagination value 26 MSGS_PER_PAGE = 20 # message pagination value
27 PM_EMAIL_MAX = 100 # max messages we will send via email
23 28
24 REPORT_SUBJECT = 'A user has flagged a private message' 29 REPORT_SUBJECT = 'A user has flagged a private message'
25 REPORT_MSG = """Hello, 30 REPORT_MSG = """Hello,
26 31
27 A user has flagged a private message for review. 32 A user has flagged a private message for review.
202 'form': form, 207 'form': form,
203 'msg_flag': msg_flag, 208 'msg_flag': msg_flag,
204 }) 209 })
205 210
206 211
207 def _delete_pms(user, pm_ids): 212 @login_required
213 @require_POST
214 def bulk(request):
215 """
216 Perform bulk action on a list of PMs.
217
218 """
219 pm_ids = request.POST.getlist('pm_ids')
220
221 # Figure out what action to perform
222 action = None
223 action_val = request.POST.get('action', '')
224 if action_val.startswith('Delete'):
225 action = _delete_pms
226 elif action_val.startswith('Email'):
227 action = _email_pms
228
229 if pm_ids and action:
230 action(request, pm_ids)
231
232 # Figure out where to redirect to
233 src = request.POST.get('src', 'inbox')
234 try:
235 page = int(request.POST.get('page', '1'))
236 except ValueError:
237 page = 1
238
239 view_name = 'messages-inbox' if src == 'inbox' else 'messages-outbox'
240 url = reverse(view_name) + '?page={}'.format(page)
241 return redirect(url)
242
243
244 def _delete_pms(request, pm_ids):
208 """ 245 """
209 Process the request to delete the list of PM ids by user. 246 Process the request to delete the list of PM ids by user.
210 247
211 Returns the number of PM's deleted. 248 Returns the number of PM's deleted.
212 249
213 """ 250 """
251 user = request.user
214 msgs = Message.objects.filter(id__in=pm_ids) 252 msgs = Message.objects.filter(id__in=pm_ids)
215 now = datetime.datetime.now() 253 now = datetime.datetime.now()
216 254
217 count = 0 255 count = 0
218 for msg in msgs: 256 for msg in msgs:
234 # sender still has PM in their inbox 272 # sender still has PM in their inbox
235 msg.receiver_delete_date = now 273 msg.receiver_delete_date = now
236 msg.save() 274 msg.save()
237 count += 1 275 count += 1
238 276
239 return count 277 msg = '{} message{} deleted.'.format(count, '' if count == 1 else 's')
240 278 if count > 0:
241
242 @login_required
243 @require_POST
244 def delete(request):
245 """
246 Deletes the requested PM's. The user must be either a sender or receiver for
247 this to work.
248
249 """
250 pm_ids = request.POST.getlist('pm_ids')
251 if pm_ids:
252 count = _delete_pms(request.user, pm_ids)
253 msg = '{} message{} deleted.'.format(count, '' if count == 1 else 's')
254 django_messages.success(request, msg) 279 django_messages.success(request, msg)
255 280 else:
256 # Figure out where to redirect to 281 django_messages.error(request, msg)
257 src = request.POST.get('src', 'inbox')
258 try:
259 page = int(request.POST.get('page', '1'))
260 except ValueError:
261 page = 1
262
263 view_name = 'messages-inbox' if src == 'inbox' else 'messages-outbox'
264 url = reverse(view_name) + '?page={}'.format(page)
265 return redirect(url)
266 282
267 283
268 def _undelete_pms(user, msg_ids): 284 def _undelete_pms(user, msg_ids):
269 """ 285 """
270 Attempts to "undelete" the messages given by the msg_ids list. 286 Attempts to "undelete" the messages given by the msg_ids list.
343 359
344 return render(request, 'messages/report_message.html', { 360 return render(request, 'messages/report_message.html', {
345 'msg': msg, 361 'msg': msg,
346 'form': form, 362 'form': form,
347 }) 363 })
364
365
366 def _email_pms(request, msg_ids):
367 """
368 Attempts to email the messages given by the msg_ids list to the user.
369 This will only succeed if the user is either the sender or receiver.
370
371 Returns the number of PM's sent.
372
373 """
374 # Does the user have an email address?
375 user = request.user
376 email_addr = user.email
377 if not email_addr:
378 return 0
379
380 msgs = Message.objects.filter(
381 Q(sender=user) | Q(receiver=user),
382 id__in=msg_ids).order_by('pk')[:PM_EMAIL_MAX]
383
384 count = len(msgs)
385 if count == 0:
386 return 0
387
388 site = Site.objects.get_current()
389 admin_email = settings.ADMINS[0][1]
390
391 subject = 'Private messages from {} - {}'.format(site.domain,
392 msgs[0].subject)
393 if count > 1:
394 subject += ' (et al.)'
395 from_email = '{}@{}'.format(settings.GPP_NO_REPLY_EMAIL, site.domain)
396 msg_body = render_to_string('messages/pm_email.txt', {
397 'site': site,
398 'user': user,
399 'msgs': msgs,
400 'admin_email': admin_email,
401 })
402 send_mail(subject, msg_body, from_email, [email_addr], defer=False)
403
404 msg = '{} message{} sent to email.'.format(count, '' if count == 1 else 's')
405 if count > 0:
406 django_messages.success(request, msg)
407 else:
408 django_messages.error(request, msg)