annotate gpp/messages/views.py @ 334:6805d15cda13

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