comparison messages/views.py @ 807:22cc8c1aec03

Private message refactor: add undelete functionality.
author Brian Neal <bgneal@gmail.com>
date Mon, 01 Sep 2014 16:22:13 -0500
parents 59e36169a814
children 4a4fa174a0ec
comparison
equal deleted inserted replaced
806:59e36169a814 807:22cc8c1aec03
1 """ 1 """
2 Views for the messages application. 2 Views for the messages application.
3 3
4 """ 4 """
5 import datetime 5 import datetime
6 import json
7 6
8 from django.contrib.auth.decorators import login_required 7 from django.contrib.auth.decorators import login_required
9 from django.views.decorators.http import require_POST 8 from django.views.decorators.http import require_POST
10 from django.contrib.auth.models import User
11 from django.contrib import messages as django_messages 9 from django.contrib import messages as django_messages
12 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger 10 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
13 from django.core.urlresolvers import reverse 11 from django.core.urlresolvers import reverse
14 from django.http import HttpResponse
15 from django.http import HttpResponseForbidden
16 from django.http import HttpResponseNotAllowed
17 from django.shortcuts import get_object_or_404 12 from django.shortcuts import get_object_or_404
18 from django.shortcuts import render, redirect 13 from django.shortcuts import render, redirect
19 14
20 from messages.models import Message, Options 15 from messages.models import Message, Options
21 from messages.forms import OptionsForm, ComposeForm 16 from messages.forms import OptionsForm, ComposeForm
23 from messages import MSG_BOX_LIMIT 18 from messages import MSG_BOX_LIMIT
24 from core.functions import quote_message 19 from core.functions import quote_message
25 20
26 21
27 MSGS_PER_PAGE = 20 # message pagination value 22 MSGS_PER_PAGE = 20 # message pagination value
28
29 # This must match the jQuery UI tab control
30 TAB_INDICES = {
31 'inbox': 0,
32 'compose': 1,
33 'outbox': 2,
34 'trash': 3,
35 'options': 4,
36 }
37 23
38 24
39 def _quota_check(box_name, count, request): 25 def _quota_check(box_name, count, request):
40 """ 26 """
41 Checks the message box count against MSG_BOX_LIMIT. 27 Checks the message box count against MSG_BOX_LIMIT.
206 192
207 def _delete_pms(user, pm_ids): 193 def _delete_pms(user, pm_ids):
208 """ 194 """
209 Process the request to delete the list of PM ids by user. 195 Process the request to delete the list of PM ids by user.
210 196
197 Returns the number of PM's deleted.
198
211 """ 199 """
212 msgs = Message.objects.filter(id__in=pm_ids) 200 msgs = Message.objects.filter(id__in=pm_ids)
213 now = datetime.datetime.now() 201 now = datetime.datetime.now()
214 202
203 count = 0
215 for msg in msgs: 204 for msg in msgs:
216 if msg.sender == user: 205 if msg.sender == user:
217 if msg.receiver_delete_date is not None or msg.read_date is None: 206 if msg.receiver_delete_date is not None or msg.read_date is None:
218 # Both parties deleted the message or receiver hasn't read it 207 # Both parties deleted the message or receiver hasn't read it
219 # yet, we can delete it now 208 # yet, we can delete it now
220 msg.delete() 209 msg.delete()
221 else: 210 else:
222 # receiver still has PM in their inbox 211 # receiver still has PM in their inbox
223 msg.sender_delete_date = now 212 msg.sender_delete_date = now
224 msg.save() 213 msg.save()
214 count += 1
225 elif msg.receiver == user: 215 elif msg.receiver == user:
226 if msg.sender_delete_date is not None: 216 if msg.sender_delete_date is not None:
227 # both parties deleted the message, we can delete it now 217 # both parties deleted the message, we can delete it now
228 msg.delete() 218 msg.delete()
229 else: 219 else:
230 # sender still has PM in their inbox 220 # sender still has PM in their inbox
231 msg.receiver_delete_date = now 221 msg.receiver_delete_date = now
232 msg.save() 222 msg.save()
223 count += 1
224
225 return count
233 226
234 227
235 @login_required 228 @login_required
236 @require_POST 229 @require_POST
237 def delete(request): 230 def delete(request):
240 this to work. 233 this to work.
241 234
242 """ 235 """
243 pm_ids = request.POST.getlist('pm_ids') 236 pm_ids = request.POST.getlist('pm_ids')
244 if pm_ids: 237 if pm_ids:
245 _delete_pms(request.user, pm_ids) 238 count = _delete_pms(request.user, pm_ids)
239 msg = '{} message{} deleted.'.format(count, '' if count == 1 else 's')
240 django_messages.success(request, msg)
246 241
247 # Figure out where to redirect to 242 # Figure out where to redirect to
248 src = request.POST.get('src', 'inbox') 243 src = request.POST.get('src', 'inbox')
249 try: 244 try:
250 page = int(request.POST.get('page', '1')) 245 page = int(request.POST.get('page', '1'))
251 except ValueError: 246 except ValueError:
252 page = 1 247 page = 1
253 248
254 view_name = 'messages-inbox' if src == inbox else 'messages-outbox' 249 view_name = 'messages-inbox' if src == 'inbox' else 'messages-outbox'
255 url = reverse(view_name) + '?page={}'.format(page) 250 url = reverse(view_name) + '?page={}'.format(page)
256 return redirect(url) 251 return redirect(url)
257 252
258 253
259 def _undelete_msgs(user, msg_ids): 254 def _undelete_pms(user, msg_ids):
260 """ 255 """
261 Attempts to "undelete" the messages given by the msg_ids list. 256 Attempts to "undelete" the messages given by the msg_ids list.
262 This will only succeed if the user is either the sender or receiver. 257 This will only succeed if the user is either the sender or receiver.
263 258
264 """ 259 Returns the number of PM's undeleted.
265 msg_ids = _only_integers(msg_ids) 260
261 """
266 msgs = Message.objects.filter(id__in=msg_ids) 262 msgs = Message.objects.filter(id__in=msg_ids)
263 count = 0
267 for msg in msgs: 264 for msg in msgs:
268 if msg.sender == user: 265 if msg.sender == user:
269 msg.sender_delete_date = None 266 msg.sender_delete_date = None
270 msg.save() 267 msg.save()
268 count += 1
271 elif msg.receiver == user: 269 elif msg.receiver == user:
272 msg.receiver_delete_date = None 270 msg.receiver_delete_date = None
273 msg.save() 271 msg.save()
274 272 count += 1
275 273 return count
276 def bulk(request): 274
277 """ 275
278 This view processes messages in bulk. Arrays of message ids are expected in 276 @login_required
279 the POST query dict: inbox_ids and outbox_ids will be deleted; trash_ids will 277 @require_POST
280 be undeleted. 278 def undelete(request):
281 279 """
282 """ 280 Undeletes the requested PM's. The user must be either a sender or receiver for
283 if not request.user.is_authenticated(): 281 this to work.
284 return HttpResponseForbidden() 282
285 if request.method != 'POST': 283 """
286 return HttpResponseNotAllowed(['POST']) 284 pm_ids = request.POST.getlist('pm_ids')
287 285 if pm_ids:
288 delete_ids = [] 286 count = _undelete_pms(request.user, pm_ids)
289 if 'inbox_ids' in request.POST: 287 msg = '{} message{} undeleted.'.format(count, '' if count == 1 else 's')
290 delete_ids.extend(request.POST.getlist('inbox_ids')) 288 django_messages.success(request, msg)
291 if 'outbox_ids' in request.POST: 289
292 delete_ids.extend(request.POST.getlist('outbox_ids')) 290 # Figure out where to redirect to
293 291 try:
294 if len(delete_ids): 292 page = int(request.POST.get('page', '1'))
295 _delete_msgs(request.user, delete_ids) 293 except ValueError:
296 294 page = 1
297 if 'trash_ids' in request.POST: 295
298 _undelete_msgs(request.user, request.POST.getlist('trash_ids')) 296 url = reverse('messages-trash') + '?page={}'.format(page)
299 297 return redirect(url)
300 return HttpResponse('');