Mercurial > public > sg101
comparison messages/views.py @ 806:59e36169a814
Private messages refactor: add delete functionality.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Mon, 01 Sep 2014 15:53:35 -0500 |
parents | e966d5553955 |
children | 22cc8c1aec03 |
comparison
equal
deleted
inserted
replaced
805:e966d5553955 | 806:59e36169a814 |
---|---|
4 """ | 4 """ |
5 import datetime | 5 import datetime |
6 import json | 6 import json |
7 | 7 |
8 from django.contrib.auth.decorators import login_required | 8 from django.contrib.auth.decorators import login_required |
9 from django.views.decorators.http import require_POST | |
9 from django.contrib.auth.models import User | 10 from django.contrib.auth.models import User |
10 from django.contrib import messages as django_messages | 11 from django.contrib import messages as django_messages |
11 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger | 12 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger |
12 from django.core.urlresolvers import reverse | 13 from django.core.urlresolvers import reverse |
13 from django.http import HttpResponse | 14 from django.http import HttpResponse |
201 'msg': msg, | 202 'msg': msg, |
202 'form': form, | 203 'form': form, |
203 }) | 204 }) |
204 | 205 |
205 | 206 |
206 def _only_integers(slist): | 207 def _delete_pms(user, pm_ids): |
207 """ | 208 """ |
208 Accepts a list of strings. Returns a list of integers consisting of only | 209 Process the request to delete the list of PM ids by user. |
209 those elements from the original list that could be converted to integers | 210 |
210 | 211 """ |
211 """ | 212 msgs = Message.objects.filter(id__in=pm_ids) |
212 result = [] | 213 now = datetime.datetime.now() |
213 for s in slist: | |
214 try: | |
215 n = int(s) | |
216 except ValueError: | |
217 pass | |
218 else: | |
219 result.append(n) | |
220 return result | |
221 | |
222 | |
223 def _delete_msgs(user, msg_ids): | |
224 """ | |
225 Deletes the messages given by the list of msg_ids. For this to succeed, the | |
226 user has to be either the sender or receiver on each message. | |
227 | |
228 """ | |
229 msg_ids = _only_integers(msg_ids) | |
230 msgs = Message.objects.filter(id__in=msg_ids) | |
231 | 214 |
232 for msg in msgs: | 215 for msg in msgs: |
233 if msg.sender == user: | 216 if msg.sender == user: |
234 if (msg.receiver_delete_date is not None or | 217 if msg.receiver_delete_date is not None or msg.read_date is None: |
235 msg.read_date is None): | |
236 # Both parties deleted the message or receiver hasn't read it | 218 # Both parties deleted the message or receiver hasn't read it |
237 # yet, we can delete it now | 219 # yet, we can delete it now |
238 msg.delete() | 220 msg.delete() |
239 else: | 221 else: |
240 # receiver still has it in inbox | 222 # receiver still has PM in their inbox |
241 msg.sender_delete_date = datetime.datetime.now() | 223 msg.sender_delete_date = now |
242 msg.save() | 224 msg.save() |
243 | |
244 elif msg.receiver == user: | 225 elif msg.receiver == user: |
245 if msg.sender_delete_date is not None: | 226 if msg.sender_delete_date is not None: |
246 # both parties deleted the message, we can delete it now | 227 # both parties deleted the message, we can delete it now |
247 msg.delete() | 228 msg.delete() |
248 else: | 229 else: |
249 # sender still has it in the outbox | 230 # sender still has PM in their inbox |
250 msg.receiver_delete_date = datetime.datetime.now() | 231 msg.receiver_delete_date = now |
251 msg.save() | 232 msg.save() |
233 | |
234 | |
235 @login_required | |
236 @require_POST | |
237 def delete(request): | |
238 """ | |
239 Deletes the requested PM's. The user must be either a sender or receiver for | |
240 this to work. | |
241 | |
242 """ | |
243 pm_ids = request.POST.getlist('pm_ids') | |
244 if pm_ids: | |
245 _delete_pms(request.user, pm_ids) | |
246 | |
247 # Figure out where to redirect to | |
248 src = request.POST.get('src', 'inbox') | |
249 try: | |
250 page = int(request.POST.get('page', '1')) | |
251 except ValueError: | |
252 page = 1 | |
253 | |
254 view_name = 'messages-inbox' if src == inbox else 'messages-outbox' | |
255 url = reverse(view_name) + '?page={}'.format(page) | |
256 return redirect(url) | |
252 | 257 |
253 | 258 |
254 def _undelete_msgs(user, msg_ids): | 259 def _undelete_msgs(user, msg_ids): |
255 """ | 260 """ |
256 Attempts to "undelete" the messages given by the msg_ids list. | 261 Attempts to "undelete" the messages given by the msg_ids list. |