comparison gpp/messages/views.py @ 371:c8c0fa817a05

Fixing #181; delete PM's if we can. Add ability for a sender to take back a PM if they delete it before it has been read.
author Brian Neal <bgneal@gmail.com>
date Sun, 06 Mar 2011 20:23:09 +0000
parents d51743322bb2
children
comparison
equal deleted inserted replaced
370:e9a066db3f54 371:c8c0fa817a05
1 """Views for the messages application""" 1 """Views for the messages application"""
2 2
3 import collections
3 import datetime 4 import datetime
5
4 from django.shortcuts import render_to_response 6 from django.shortcuts import render_to_response
5 from django.template import RequestContext 7 from django.template import RequestContext
6 from django.http import HttpResponseRedirect 8 from django.http import HttpResponseRedirect
7 from django.contrib.auth.decorators import login_required 9 from django.contrib.auth.decorators import login_required
8 from django.contrib import messages 10 from django.contrib import messages
23 'inbox': 'messages-inbox', 25 'inbox': 'messages-inbox',
24 'outbox': 'messages-outbox', 26 'outbox': 'messages-outbox',
25 'trash': 'messages-trash', 27 'trash': 'messages-trash',
26 } 28 }
27 29
30 MSG_DELETED, MSG_TRASHED, MSG_ERROR = range(3)
31
28 32
29 def box_redirect(request): 33 def box_redirect(request):
30 """ 34 """
31 Determines which box to redirect to by looking for a GET or 35 Determines which box to redirect to by looking for a GET or
32 POST parameter. 36 POST parameter.
46 def inbox(request): 50 def inbox(request):
47 """Displays the inbox for the user making the request.""" 51 """Displays the inbox for the user making the request."""
48 msgs = Message.objects.inbox(request.user) 52 msgs = Message.objects.inbox(request.user)
49 return render_to_response('messages/inbox.html', { 53 return render_to_response('messages/inbox.html', {
50 'msgs': msgs, 54 'msgs': msgs,
51 }, 55 },
52 context_instance = RequestContext(request)) 56 context_instance = RequestContext(request))
53 57
54 58
55 @login_required 59 @login_required
56 def outbox(request): 60 def outbox(request):
57 """Displays the outbox for the user making the request.""" 61 """Displays the outbox for the user making the request."""
58 msgs = Message.objects.outbox(request.user) 62 msgs = Message.objects.outbox(request.user)
59 return render_to_response('messages/outbox.html', { 63 return render_to_response('messages/outbox.html', {
60 'msgs': msgs, 64 'msgs': msgs,
61 }, 65 },
62 context_instance = RequestContext(request)) 66 context_instance = RequestContext(request))
63 67
64 68
65 @login_required 69 @login_required
66 def trash(request): 70 def trash(request):
67 """Displays the trash for the user making the request.""" 71 """Displays the trash for the user making the request."""
68 msgs = Message.objects.trash(request.user) 72 msgs = Message.objects.trash(request.user)
69 return render_to_response('messages/trash.html', { 73 return render_to_response('messages/trash.html', {
70 'msgs': msgs, 74 'msgs': msgs,
71 }, 75 },
72 context_instance = RequestContext(request)) 76 context_instance = RequestContext(request))
73 77
74 78
75 @login_required 79 @login_required
76 def view(request, msg_id): 80 def view(request, msg_id):
90 94
91 return render_to_response('messages/view.html', { 95 return render_to_response('messages/view.html', {
92 'box': box, 96 'box': box,
93 'msg': msg, 97 'msg': msg,
94 'is_deleted': msg.is_deleted(request.user), 98 'is_deleted': msg.is_deleted(request.user),
95 }, 99 },
96 context_instance = RequestContext(request)) 100 context_instance = RequestContext(request))
97 101
98 102
99 @login_required 103 @login_required
100 def reply(request, msg_id): 104 def reply(request, msg_id):
128 132
129 compose_form = ComposeForm(request.user, initial=form_data) 133 compose_form = ComposeForm(request.user, initial=form_data)
130 134
131 return render_to_response('messages/compose.html', { 135 return render_to_response('messages/compose.html', {
132 'compose_form': compose_form, 136 'compose_form': compose_form,
133 }, 137 },
134 context_instance = RequestContext(request)) 138 context_instance = RequestContext(request))
135 139
136 140
137 @login_required 141 @login_required
138 def compose(request, receiver=None): 142 def compose(request, receiver=None):
157 else: 161 else:
158 compose_form = ComposeForm(request.user) 162 compose_form = ComposeForm(request.user)
159 163
160 return render_to_response('messages/compose.html', { 164 return render_to_response('messages/compose.html', {
161 'compose_form': compose_form, 165 'compose_form': compose_form,
162 }, 166 },
163 context_instance = RequestContext(request)) 167 context_instance = RequestContext(request))
168
169
170 def _delete_message(user, msg):
171 """
172 Deletes a given message. The user must be either the sender or
173 receiver for this to succeed.
174 If both parties have deleted the message, it is deleted from the
175 database. If only one party has deleted the message, the message
176 is just sent to the trash.
177
178 Returns MSG_DELETED, MSG_TRASHED, or MSG_ERROR to indicate what
179 action was performed.
180
181 """
182 if msg.sender == user:
183 if (msg.receiver_delete_date is not None or
184 msg.read_date is None):
185 # Both parties deleted the message or receiver hasn't read it yet,
186 # we can delete it now
187 msg.delete()
188 return MSG_DELETED
189 else:
190 # receiver still has it in inbox
191 msg.sender_delete_date = datetime.datetime.now()
192 msg.save()
193 return MSG_TRASHED
194
195 elif msg.receiver == user:
196 if msg.sender_delete_date is not None:
197 # both parties deleted the message, we can delete it now
198 msg.delete()
199 return MSG_DELETED
200 else:
201 # sender still has it in the outbox
202 msg.receiver_delete_date = datetime.datetime.now()
203 msg.save()
204 return MSG_TRASHED
205
206 return MSG_ERROR
164 207
165 208
166 @login_required 209 @login_required
167 @require_POST 210 @require_POST
168 def delete(request, msg_id): 211 def delete(request, msg_id):
169 """ 212 """
170 Deletes a given message. The user must be either the sender or 213 Deletes a given message. The user must be either the sender or
171 receiver for this to succeed. 214 receiver for this to succeed.
172 """ 215 """
173 msg = get_object_or_404(Message, pk=msg_id) 216 msg = get_object_or_404(Message, pk=msg_id)
174 if msg.sender == request.user: 217 result = _delete_message(request.user, msg)
175 msg.sender_delete_date = datetime.datetime.now() 218 if result == MSG_DELETED:
176 elif msg.receiver == request.user: 219 messages.success(request, 'Message deleted.')
177 msg.receiver_delete_date = datetime.datetime.now() 220 elif result == MSG_TRASHED:
178 else: 221 messages.success(request, 'Message sent to trash.')
179 raise Http404 222 else:
180 msg.save() 223 messages.error(request, 'Error deleting message.')
181 messages.success(request, 'Message sent to trash.')
182 224
183 return box_redirect(request) 225 return box_redirect(request)
184 226
185 227
186 @login_required 228 @login_required
194 delete_ids = request.POST.getlist('delete_ids') 236 delete_ids = request.POST.getlist('delete_ids')
195 try: 237 try:
196 delete_ids = [int(id) for id in delete_ids] 238 delete_ids = [int(id) for id in delete_ids]
197 except ValueError: 239 except ValueError:
198 raise Http404 240 raise Http404
199 msgs = Message.objects.filter(id__in = delete_ids) 241
200 now = datetime.datetime.now() 242 msgs = Message.objects.filter(id__in=delete_ids)
243
244 counts = collections.defaultdict(int)
201 for msg in msgs: 245 for msg in msgs:
202 if msg.sender == request.user: 246 result = _delete_message(request.user, msg)
203 msg.sender_delete_date = now 247 counts[result] += 1
204 msg.save() 248
205 elif msg.receiver == request.user: 249 if counts[MSG_DELETED]:
206 msg.receiver_delete_date = now 250 messages.success(request, 'Messages deleted: %d' % counts[MSG_DELETED])
207 msg.save() 251 if counts[MSG_TRASHED]:
208 messages.success(request, 'Messages sent to the trash.') 252 messages.success(request, 'Messages sent to trash: %d' % counts[MSG_TRASHED])
253 if counts[MSG_ERROR]:
254 messages.error(request, 'Message errors: %d' % counts[MSG_ERROR])
209 255
210 return box_redirect(request) 256 return box_redirect(request)
211 257
212 258
213 @login_required 259 @login_required
280 326
281 form = OptionsForm(instance=options) 327 form = OptionsForm(instance=options)
282 328
283 return render_to_response('messages/options.html', { 329 return render_to_response('messages/options.html', {
284 'form': form, 330 'form': form,
285 }, 331 },
286 context_instance = RequestContext(request)) 332 context_instance = RequestContext(request))
287 333