annotate gpp/messages/views.py @ 102:e67c4dd98db5

Forums: new topic form sprouts boolean fields for sticky and locking if the user has rights. Implemented the locked logic. Fixed a bug where topics where getting out of order (the view_count was bumping the update_date because of auto_now).
author Brian Neal <bgneal@gmail.com>
date Wed, 16 Sep 2009 02:01:57 +0000
parents dbd703f7d63a
children ab7830b067b3
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.contrib.auth.models import User
gremmie@1 7 from django.http import HttpResponseRedirect
gremmie@1 8 from django.http import HttpResponse
gremmie@1 9 from django.http import HttpResponseBadRequest
gremmie@1 10 from django.http import HttpResponseForbidden
gremmie@1 11 from django.contrib.auth.decorators import login_required
gremmie@1 12 from django.shortcuts import get_object_or_404
gremmie@1 13 from django.core.urlresolvers import reverse
gremmie@1 14 from django.http import Http404
gremmie@1 15
gremmie@1 16 from messages.models import Message
gremmie@1 17 from messages.models import Options
gremmie@1 18 from messages.forms import ComposeForm
gremmie@1 19 from messages.forms import OptionsForm
gremmie@1 20 from messages.utils import reply_subject
gremmie@1 21 from messages.utils import quote_message
gremmie@1 22
gremmie@1 23
gremmie@1 24 BOX_MAP = {
gremmie@1 25 'inbox': 'messages-inbox',
gremmie@1 26 'outbox': 'messages-outbox',
gremmie@1 27 'trash': 'messages-trash',
gremmie@1 28 }
gremmie@1 29
gremmie@1 30
gremmie@1 31 def box_redirect(request):
gremmie@1 32 """
gremmie@1 33 Determines which box to redirect to by looking for a GET or
gremmie@1 34 POST parameter.
gremmie@1 35 """
gremmie@1 36 if request.method == 'GET':
gremmie@1 37 box = request.GET.get('box', 'inbox')
gremmie@1 38 else:
gremmie@1 39 box = request.POST.get('box', 'inbox')
gremmie@1 40 if BOX_MAP.has_key(box):
gremmie@1 41 url = reverse(BOX_MAP[box])
gremmie@1 42 else:
gremmie@1 43 url = reverse(BOX_MAP['inbox'])
gremmie@1 44 return HttpResponseRedirect(url)
gremmie@1 45
gremmie@1 46
gremmie@1 47 @login_required
gremmie@1 48 def inbox(request):
gremmie@1 49 """Displays the inbox for the user making the request."""
gremmie@1 50 msgs = Message.objects.inbox(request.user)
gremmie@1 51 return render_to_response('messages/inbox.html', {
gremmie@1 52 'msgs': msgs,
gremmie@1 53 },
gremmie@1 54 context_instance = RequestContext(request))
gremmie@1 55
gremmie@1 56
gremmie@1 57 @login_required
gremmie@1 58 def outbox(request):
gremmie@1 59 """Displays the outbox for the user making the request."""
gremmie@1 60 msgs = Message.objects.outbox(request.user)
gremmie@1 61 return render_to_response('messages/outbox.html', {
gremmie@1 62 'msgs': msgs,
gremmie@1 63 },
gremmie@1 64 context_instance = RequestContext(request))
gremmie@1 65
gremmie@1 66
gremmie@1 67 @login_required
gremmie@1 68 def trash(request):
gremmie@1 69 """Displays the trash for the user making the request."""
gremmie@1 70 msgs = Message.objects.trash(request.user)
gremmie@1 71 return render_to_response('messages/trash.html', {
gremmie@1 72 'msgs': msgs,
gremmie@1 73 },
gremmie@1 74 context_instance = RequestContext(request))
gremmie@1 75
gremmie@1 76
gremmie@1 77 @login_required
gremmie@1 78 def view(request, msg_id):
gremmie@1 79 """
gremmie@1 80 View a given message. Only the sender or receiver can see
gremmie@1 81 the message.
gremmie@1 82 """
gremmie@1 83 msg = get_object_or_404(Message, pk=msg_id)
gremmie@1 84 if msg.sender != request.user and msg.receiver != request.user:
gremmie@1 85 raise Http404
gremmie@1 86
gremmie@1 87 if msg.receiver == request.user and msg.read_date is None:
gremmie@1 88 msg.read_date = datetime.datetime.now()
gremmie@1 89 msg.save()
gremmie@1 90
gremmie@1 91 box = request.GET.get('box', None)
gremmie@1 92
gremmie@1 93 return render_to_response('messages/view.html', {
gremmie@1 94 'box': box,
gremmie@1 95 'msg': msg,
gremmie@1 96 'is_deleted': msg.is_deleted(request.user),
gremmie@1 97 },
gremmie@1 98 context_instance = RequestContext(request))
gremmie@1 99
gremmie@1 100
gremmie@1 101 @login_required
gremmie@1 102 def reply(request, msg_id):
gremmie@1 103 """
gremmie@1 104 Process or prepare the compose form in order to reply
gremmie@1 105 to a given message.
gremmie@1 106 """
gremmie@1 107 msg = get_object_or_404(Message, pk=msg_id)
gremmie@1 108
gremmie@1 109 if request.method == "POST":
gremmie@1 110 if request.POST.get('submit_button', 'Cancel') == 'Cancel':
gremmie@1 111 return box_redirect(request)
gremmie@1 112 compose_form = ComposeForm(request.user, request.POST)
gremmie@1 113 if compose_form.is_valid():
gremmie@1 114 compose_form.save(sender=request.user, parent_msg=msg)
gremmie@1 115 request.user.message_set.create(message='Reply sent.')
gremmie@1 116 return box_redirect(request)
gremmie@1 117 else:
gremmie@1 118 if msg.receiver == request.user:
gremmie@1 119 receiver_name = msg.sender.username
gremmie@1 120 else:
gremmie@1 121 # replying to message in outbox
gremmie@1 122 receiver_name = msg.receiver.username
gremmie@1 123
gremmie@1 124 form_data = {
gremmie@1 125 'receiver': receiver_name,
gremmie@1 126 'subject': reply_subject(msg.subject),
gremmie@1 127 'message': quote_message(msg.sender, msg.send_date, msg.message),
gremmie@1 128 'box': request.GET.get('box', 'inbox'),
gremmie@1 129 }
gremmie@1 130
gremmie@1 131 compose_form = ComposeForm(request.user, initial=form_data)
gremmie@1 132
gremmie@1 133 return render_to_response('messages/compose.html', {
gremmie@1 134 'compose_form': compose_form,
gremmie@1 135 },
gremmie@1 136 context_instance = RequestContext(request))
gremmie@1 137
gremmie@1 138
gremmie@1 139 @login_required
gremmie@1 140 def compose(request, receiver=None):
gremmie@1 141 """
gremmie@1 142 Process or prepare the compose form in order to create
gremmie@1 143 a new message.
gremmie@1 144 """
gremmie@1 145 if request.method == "POST":
gremmie@1 146 if request.POST.get('submit_button', 'Cancel') == 'Cancel':
gremmie@1 147 return HttpResponseRedirect(reverse('messages-inbox'))
gremmie@1 148 compose_form = ComposeForm(request.user, request.POST)
gremmie@1 149 if compose_form.is_valid():
gremmie@1 150 compose_form.save(sender=request.user)
gremmie@1 151 request.user.message_set.create(message='Message sent.')
gremmie@1 152 return HttpResponseRedirect(reverse('messages-inbox'))
gremmie@1 153 else:
gremmie@1 154 if receiver is not None:
gremmie@1 155 form_data = {
gremmie@1 156 'receiver': receiver,
gremmie@1 157 }
gremmie@1 158 compose_form = ComposeForm(request.user, initial=form_data)
gremmie@1 159 else:
gremmie@1 160 compose_form = ComposeForm(request.user)
gremmie@1 161
gremmie@1 162 return render_to_response('messages/compose.html', {
gremmie@1 163 'compose_form': compose_form,
gremmie@1 164 },
gremmie@1 165 context_instance = RequestContext(request))
gremmie@1 166
gremmie@1 167
gremmie@1 168 @login_required
gremmie@1 169 def delete(request, msg_id):
gremmie@1 170 """
gremmie@1 171 Deletes a given message. The user must be either the sender or
gremmie@1 172 receiver for this to succeed.
gremmie@1 173 """
gremmie@1 174 msg = get_object_or_404(Message, pk=msg_id)
gremmie@1 175 if msg.sender == request.user:
gremmie@1 176 msg.sender_delete_date = datetime.datetime.now()
gremmie@1 177 elif msg.receiver == request.user:
gremmie@1 178 msg.receiver_delete_date = datetime.datetime.now()
gremmie@1 179 else:
gremmie@1 180 raise Http404
gremmie@1 181 msg.save()
gremmie@1 182 request.user.message_set.create(message='Message sent to trash.')
gremmie@1 183
gremmie@1 184 return box_redirect(request)
gremmie@1 185
gremmie@1 186
gremmie@1 187 @login_required
gremmie@1 188 def delete_bulk(request):
gremmie@1 189 """
gremmie@1 190 Deletes messages in bulk. The message ID's to be deleted are expected
gremmie@1 191 to be in the delete POST array. The user must be either the sender
gremmie@1 192 or receiver for this to succeed.
gremmie@1 193 """
gremmie@1 194 if request.method == "POST":
gremmie@1 195 delete_ids = request.POST.getlist('delete_ids')
gremmie@1 196 try:
gremmie@1 197 delete_ids = [int(id) for id in delete_ids]
gremmie@1 198 except ValueError:
gremmie@1 199 raise Http404
gremmie@1 200 msgs = Message.objects.filter(id__in = delete_ids)
gremmie@1 201 now = datetime.datetime.now()
gremmie@1 202 for msg in msgs:
gremmie@1 203 if msg.sender == request.user:
gremmie@1 204 msg.sender_delete_date = now
gremmie@1 205 msg.save()
gremmie@1 206 elif msg.receiver == request.user:
gremmie@1 207 msg.receiver_delete_date = now
gremmie@1 208 msg.save()
gremmie@1 209 request.user.message_set.create(message='Messages sent to the trash.')
gremmie@1 210
gremmie@1 211 return box_redirect(request)
gremmie@1 212
gremmie@1 213
gremmie@1 214 @login_required
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()
gremmie@1 228 request.user.message_set.create(message='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()
gremmie@1 254 request.user.message_set.create(message='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()
gremmie@1 271 request.user.message_set.create(message='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
gremmie@1 288
gremmie@1 289 def ajax_users(request):
gremmie@1 290 """
gremmie@1 291 If the user is authenticated, return a string of usernames whose names start with
gremmie@1 292 the 'q' GET parameter, limited by the 'limit' GET parameters. The names are separated
gremmie@1 293 by newlines.
gremmie@1 294 If the user is not authenticated, return an empty string.
gremmie@1 295 This is used by the auto-complete function in the compose form.
gremmie@1 296 """
gremmie@1 297 q = request.GET.get('q', None)
gremmie@1 298 if q is None:
gremmie@1 299 return HttpResponseBadRequest()
gremmie@1 300
gremmie@1 301 if request.user.is_authenticated():
gremmie@1 302 q = request.GET.get('q', ' ')
gremmie@1 303 limit = int(request.GET.get('limit', 10))
gremmie@1 304 users = User.objects.filter(username__istartswith=q).values_list('username', flat=True)[:limit]
gremmie@1 305 user_list = u"\n".join(users)
gremmie@1 306 return HttpResponse(user_list)
gremmie@1 307 return HttpResponseForbidden()
gremmie@1 308
gremmie@1 309
gremmie@1 310 # vim: ts=4 sw=4