annotate gpp/messages/views.py @ 152:bc657962941e

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