annotate gpp/messages/views.py @ 162:6a5bdcf93ad3

Fix #48; shoutbox was no longer escaping user input on display.
author Brian Neal <bgneal@gmail.com>
date Tue, 22 Dec 2009 03:55:37 +0000
parents 7ad1f3e77cd9
children d51743322bb2
rev   line source
gremmie@1 1 """Views for the messages application"""
gremmie@1 2
gremmie@1 3 import datetime
gremmie@1 4 from django.shortcuts import render_to_response
gremmie@1 5 from django.template import RequestContext
gremmie@1 6 from django.http import HttpResponseRedirect
gremmie@1 7 from django.contrib.auth.decorators import login_required
gremmie@1 8 from django.shortcuts import get_object_or_404
gremmie@1 9 from django.core.urlresolvers import reverse
gremmie@1 10 from django.http import Http404
bgneal@156 11 from django.views.decorators.http import require_POST
gremmie@1 12
gremmie@1 13 from messages.models import Message
gremmie@1 14 from messages.models import Options
gremmie@1 15 from messages.forms import ComposeForm
gremmie@1 16 from messages.forms import OptionsForm
gremmie@1 17 from messages.utils import reply_subject
gremmie@1 18 from messages.utils import quote_message
gremmie@1 19
gremmie@1 20
gremmie@1 21 BOX_MAP = {
gremmie@1 22 'inbox': 'messages-inbox',
gremmie@1 23 'outbox': 'messages-outbox',
gremmie@1 24 'trash': 'messages-trash',
gremmie@1 25 }
gremmie@1 26
gremmie@1 27
gremmie@1 28 def box_redirect(request):
gremmie@1 29 """
gremmie@1 30 Determines which box to redirect to by looking for a GET or
gremmie@1 31 POST parameter.
gremmie@1 32 """
gremmie@1 33 if request.method == 'GET':
gremmie@1 34 box = request.GET.get('box', 'inbox')
gremmie@1 35 else:
gremmie@1 36 box = request.POST.get('box', 'inbox')
gremmie@1 37 if BOX_MAP.has_key(box):
gremmie@1 38 url = reverse(BOX_MAP[box])
gremmie@1 39 else:
gremmie@1 40 url = reverse(BOX_MAP['inbox'])
gremmie@1 41 return HttpResponseRedirect(url)
gremmie@1 42
gremmie@1 43
gremmie@1 44 @login_required
gremmie@1 45 def inbox(request):
gremmie@1 46 """Displays the inbox for the user making the request."""
gremmie@1 47 msgs = Message.objects.inbox(request.user)
gremmie@1 48 return render_to_response('messages/inbox.html', {
gremmie@1 49 'msgs': msgs,
gremmie@1 50 },
gremmie@1 51 context_instance = RequestContext(request))
gremmie@1 52
gremmie@1 53
gremmie@1 54 @login_required
gremmie@1 55 def outbox(request):
gremmie@1 56 """Displays the outbox for the user making the request."""
gremmie@1 57 msgs = Message.objects.outbox(request.user)
gremmie@1 58 return render_to_response('messages/outbox.html', {
gremmie@1 59 'msgs': msgs,
gremmie@1 60 },
gremmie@1 61 context_instance = RequestContext(request))
gremmie@1 62
gremmie@1 63
gremmie@1 64 @login_required
gremmie@1 65 def trash(request):
gremmie@1 66 """Displays the trash for the user making the request."""
gremmie@1 67 msgs = Message.objects.trash(request.user)
gremmie@1 68 return render_to_response('messages/trash.html', {
gremmie@1 69 'msgs': msgs,
gremmie@1 70 },
gremmie@1 71 context_instance = RequestContext(request))
gremmie@1 72
gremmie@1 73
gremmie@1 74 @login_required
gremmie@1 75 def view(request, msg_id):
gremmie@1 76 """
gremmie@1 77 View a given message. Only the sender or receiver can see
gremmie@1 78 the message.
gremmie@1 79 """
gremmie@1 80 msg = get_object_or_404(Message, pk=msg_id)
gremmie@1 81 if msg.sender != request.user and msg.receiver != request.user:
gremmie@1 82 raise Http404
gremmie@1 83
gremmie@1 84 if msg.receiver == request.user and msg.read_date is None:
gremmie@1 85 msg.read_date = datetime.datetime.now()
gremmie@1 86 msg.save()
gremmie@1 87
gremmie@1 88 box = request.GET.get('box', None)
gremmie@1 89
gremmie@1 90 return render_to_response('messages/view.html', {
gremmie@1 91 'box': box,
gremmie@1 92 'msg': msg,
gremmie@1 93 'is_deleted': msg.is_deleted(request.user),
gremmie@1 94 },
gremmie@1 95 context_instance = RequestContext(request))
gremmie@1 96
gremmie@1 97
gremmie@1 98 @login_required
gremmie@1 99 def reply(request, msg_id):
gremmie@1 100 """
gremmie@1 101 Process or prepare the compose form in order to reply
gremmie@1 102 to a given message.
gremmie@1 103 """
gremmie@1 104 msg = get_object_or_404(Message, pk=msg_id)
gremmie@1 105
gremmie@1 106 if request.method == "POST":
gremmie@1 107 if request.POST.get('submit_button', 'Cancel') == 'Cancel':
gremmie@1 108 return box_redirect(request)
gremmie@1 109 compose_form = ComposeForm(request.user, request.POST)
gremmie@1 110 if compose_form.is_valid():
gremmie@1 111 compose_form.save(sender=request.user, parent_msg=msg)
gremmie@1 112 request.user.message_set.create(message='Reply sent.')
gremmie@1 113 return box_redirect(request)
gremmie@1 114 else:
gremmie@1 115 if msg.receiver == request.user:
gremmie@1 116 receiver_name = msg.sender.username
gremmie@1 117 else:
gremmie@1 118 # replying to message in outbox
gremmie@1 119 receiver_name = msg.receiver.username
gremmie@1 120
gremmie@1 121 form_data = {
gremmie@1 122 'receiver': receiver_name,
gremmie@1 123 'subject': reply_subject(msg.subject),
gremmie@1 124 'message': quote_message(msg.sender, msg.send_date, msg.message),
gremmie@1 125 'box': request.GET.get('box', 'inbox'),
gremmie@1 126 }
gremmie@1 127
gremmie@1 128 compose_form = ComposeForm(request.user, initial=form_data)
gremmie@1 129
gremmie@1 130 return render_to_response('messages/compose.html', {
gremmie@1 131 'compose_form': compose_form,
gremmie@1 132 },
gremmie@1 133 context_instance = RequestContext(request))
gremmie@1 134
gremmie@1 135
gremmie@1 136 @login_required
gremmie@1 137 def compose(request, receiver=None):
gremmie@1 138 """
gremmie@1 139 Process or prepare the compose form in order to create
gremmie@1 140 a new message.
gremmie@1 141 """
gremmie@1 142 if request.method == "POST":
gremmie@1 143 if request.POST.get('submit_button', 'Cancel') == 'Cancel':
gremmie@1 144 return HttpResponseRedirect(reverse('messages-inbox'))
gremmie@1 145 compose_form = ComposeForm(request.user, request.POST)
gremmie@1 146 if compose_form.is_valid():
gremmie@1 147 compose_form.save(sender=request.user)
gremmie@1 148 request.user.message_set.create(message='Message sent.')
gremmie@1 149 return HttpResponseRedirect(reverse('messages-inbox'))
gremmie@1 150 else:
gremmie@1 151 if receiver is not None:
gremmie@1 152 form_data = {
gremmie@1 153 'receiver': receiver,
gremmie@1 154 }
gremmie@1 155 compose_form = ComposeForm(request.user, initial=form_data)
gremmie@1 156 else:
gremmie@1 157 compose_form = ComposeForm(request.user)
gremmie@1 158
gremmie@1 159 return render_to_response('messages/compose.html', {
gremmie@1 160 'compose_form': compose_form,
gremmie@1 161 },
gremmie@1 162 context_instance = RequestContext(request))
gremmie@1 163
gremmie@1 164
gremmie@1 165 @login_required
bgneal@156 166 @require_POST
gremmie@1 167 def delete(request, msg_id):
gremmie@1 168 """
gremmie@1 169 Deletes a given message. The user must be either the sender or
gremmie@1 170 receiver for this to succeed.
gremmie@1 171 """
gremmie@1 172 msg = get_object_or_404(Message, pk=msg_id)
gremmie@1 173 if msg.sender == request.user:
gremmie@1 174 msg.sender_delete_date = datetime.datetime.now()
gremmie@1 175 elif msg.receiver == request.user:
gremmie@1 176 msg.receiver_delete_date = datetime.datetime.now()
gremmie@1 177 else:
gremmie@1 178 raise Http404
gremmie@1 179 msg.save()
gremmie@1 180 request.user.message_set.create(message='Message sent to trash.')
gremmie@1 181
gremmie@1 182 return box_redirect(request)
gremmie@1 183
gremmie@1 184
gremmie@1 185 @login_required
gremmie@1 186 def delete_bulk(request):
gremmie@1 187 """
gremmie@1 188 Deletes messages in bulk. The message ID's to be deleted are expected
gremmie@1 189 to be in the delete POST array. The user must be either the sender
gremmie@1 190 or receiver for this to succeed.
gremmie@1 191 """
gremmie@1 192 if request.method == "POST":
gremmie@1 193 delete_ids = request.POST.getlist('delete_ids')
gremmie@1 194 try:
gremmie@1 195 delete_ids = [int(id) for id in delete_ids]
gremmie@1 196 except ValueError:
gremmie@1 197 raise Http404
gremmie@1 198 msgs = Message.objects.filter(id__in = delete_ids)
gremmie@1 199 now = datetime.datetime.now()
gremmie@1 200 for msg in msgs:
gremmie@1 201 if msg.sender == request.user:
gremmie@1 202 msg.sender_delete_date = now
gremmie@1 203 msg.save()
gremmie@1 204 elif msg.receiver == request.user:
gremmie@1 205 msg.receiver_delete_date = now
gremmie@1 206 msg.save()
gremmie@1 207 request.user.message_set.create(message='Messages sent to the trash.')
gremmie@1 208
gremmie@1 209 return box_redirect(request)
gremmie@1 210
gremmie@1 211
gremmie@1 212 @login_required
bgneal@156 213 @require_POST
gremmie@1 214 def undelete(request, msg_id):
gremmie@1 215 """
gremmie@1 216 Undeletes a given message. The user must be either the sender or
gremmie@1 217 receiver for this to succeed.
gremmie@1 218 """
gremmie@1 219 msg = get_object_or_404(Message, pk=msg_id)
gremmie@1 220 if msg.sender == request.user:
gremmie@1 221 msg.sender_delete_date = None
gremmie@1 222 elif msg.receiver == request.user:
gremmie@1 223 msg.receiver_delete_date = None
gremmie@1 224 else:
gremmie@1 225 raise Http404
gremmie@1 226 msg.save()
gremmie@1 227 request.user.message_set.create(message='Message retrieved from the trash.')
gremmie@1 228
gremmie@1 229 return box_redirect(request)
gremmie@1 230
gremmie@1 231
gremmie@1 232 @login_required
gremmie@1 233 def undelete_bulk(request):
gremmie@1 234 """
gremmie@1 235 Undeletes messages in bulk. The message ID's to be deleted are expected
gremmie@1 236 to be in the delete POST array. The user must be either the sender
gremmie@1 237 or receiver for this to succeed.
gremmie@1 238 """
gremmie@1 239 if request.method == "POST":
gremmie@1 240 undelete_ids = request.POST.getlist('undelete_ids')
gremmie@1 241 try:
gremmie@1 242 undelete_ids = [int(id) for id in undelete_ids]
gremmie@1 243 except ValueError:
gremmie@1 244 raise Http404
gremmie@1 245 msgs = Message.objects.filter(id__in = undelete_ids)
gremmie@1 246 for msg in msgs:
gremmie@1 247 if msg.sender == request.user:
gremmie@1 248 msg.sender_delete_date = None
gremmie@1 249 msg.save()
gremmie@1 250 elif msg.receiver == request.user:
gremmie@1 251 msg.receiver_delete_date = None
gremmie@1 252 msg.save()
gremmie@1 253 request.user.message_set.create(message='Messages retrieved from the trash.')
gremmie@1 254
gremmie@1 255 return box_redirect(request)
gremmie@1 256
gremmie@1 257
gremmie@1 258 @login_required
gremmie@1 259 def options(request):
gremmie@1 260 """
gremmie@1 261 View to display/change user options.
gremmie@1 262 """
gremmie@1 263 if request.method == "POST":
gremmie@1 264 if request.POST.get('submit_button', 'Cancel') == 'Cancel':
gremmie@1 265 return HttpResponseRedirect(reverse('messages-inbox'))
gremmie@1 266 options = Options.objects.for_user(request.user)
gremmie@1 267 form = OptionsForm(request.POST, instance=options)
gremmie@1 268 if form.is_valid():
gremmie@1 269 form.save()
gremmie@1 270 request.user.message_set.create(message='Options saved.')
gremmie@1 271 return HttpResponseRedirect(reverse('messages-inbox'))
gremmie@1 272 else:
gremmie@1 273 try:
gremmie@1 274 options = Options.objects.for_user(request.user)
gremmie@1 275 except:
gremmie@1 276 options = Options()
gremmie@1 277 options.user = request.user
gremmie@1 278 options.save()
gremmie@1 279
gremmie@1 280 form = OptionsForm(instance=options)
gremmie@1 281
gremmie@1 282 return render_to_response('messages/options.html', {
gremmie@1 283 'form': form,
gremmie@1 284 },
gremmie@1 285 context_instance = RequestContext(request))
gremmie@1 286