gremmie@1: """Views for the messages application""" gremmie@1: gremmie@1: import datetime gremmie@1: from django.shortcuts import render_to_response gremmie@1: from django.template import RequestContext gremmie@1: from django.http import HttpResponseRedirect gremmie@1: from django.contrib.auth.decorators import login_required bgneal@178: from django.contrib import messages gremmie@1: from django.shortcuts import get_object_or_404 gremmie@1: from django.core.urlresolvers import reverse gremmie@1: from django.http import Http404 bgneal@156: from django.views.decorators.http import require_POST gremmie@1: gremmie@1: from messages.models import Message gremmie@1: from messages.models import Options gremmie@1: from messages.forms import ComposeForm gremmie@1: from messages.forms import OptionsForm gremmie@1: from messages.utils import reply_subject gremmie@1: from messages.utils import quote_message gremmie@1: gremmie@1: gremmie@1: BOX_MAP = { gremmie@1: 'inbox': 'messages-inbox', gremmie@1: 'outbox': 'messages-outbox', gremmie@1: 'trash': 'messages-trash', gremmie@1: } gremmie@1: gremmie@1: gremmie@1: def box_redirect(request): gremmie@1: """ gremmie@1: Determines which box to redirect to by looking for a GET or gremmie@1: POST parameter. gremmie@1: """ gremmie@1: if request.method == 'GET': gremmie@1: box = request.GET.get('box', 'inbox') gremmie@1: else: gremmie@1: box = request.POST.get('box', 'inbox') gremmie@1: if BOX_MAP.has_key(box): gremmie@1: url = reverse(BOX_MAP[box]) gremmie@1: else: gremmie@1: url = reverse(BOX_MAP['inbox']) gremmie@1: return HttpResponseRedirect(url) gremmie@1: gremmie@1: gremmie@1: @login_required gremmie@1: def inbox(request): gremmie@1: """Displays the inbox for the user making the request.""" gremmie@1: msgs = Message.objects.inbox(request.user) gremmie@1: return render_to_response('messages/inbox.html', { gremmie@1: 'msgs': msgs, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: gremmie@1: @login_required gremmie@1: def outbox(request): gremmie@1: """Displays the outbox for the user making the request.""" gremmie@1: msgs = Message.objects.outbox(request.user) gremmie@1: return render_to_response('messages/outbox.html', { gremmie@1: 'msgs': msgs, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: gremmie@1: @login_required gremmie@1: def trash(request): gremmie@1: """Displays the trash for the user making the request.""" gremmie@1: msgs = Message.objects.trash(request.user) gremmie@1: return render_to_response('messages/trash.html', { gremmie@1: 'msgs': msgs, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: gremmie@1: @login_required gremmie@1: def view(request, msg_id): gremmie@1: """ gremmie@1: View a given message. Only the sender or receiver can see gremmie@1: the message. gremmie@1: """ gremmie@1: msg = get_object_or_404(Message, pk=msg_id) gremmie@1: if msg.sender != request.user and msg.receiver != request.user: gremmie@1: raise Http404 gremmie@1: gremmie@1: if msg.receiver == request.user and msg.read_date is None: gremmie@1: msg.read_date = datetime.datetime.now() gremmie@1: msg.save() gremmie@1: gremmie@1: box = request.GET.get('box', None) gremmie@1: gremmie@1: return render_to_response('messages/view.html', { gremmie@1: 'box': box, gremmie@1: 'msg': msg, gremmie@1: 'is_deleted': msg.is_deleted(request.user), gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: gremmie@1: @login_required gremmie@1: def reply(request, msg_id): gremmie@1: """ gremmie@1: Process or prepare the compose form in order to reply gremmie@1: to a given message. gremmie@1: """ gremmie@1: msg = get_object_or_404(Message, pk=msg_id) gremmie@1: gremmie@1: if request.method == "POST": gremmie@1: if request.POST.get('submit_button', 'Cancel') == 'Cancel': gremmie@1: return box_redirect(request) gremmie@1: compose_form = ComposeForm(request.user, request.POST) gremmie@1: if compose_form.is_valid(): gremmie@1: compose_form.save(sender=request.user, parent_msg=msg) bgneal@178: messages.success(request, 'Reply sent.') gremmie@1: return box_redirect(request) gremmie@1: else: gremmie@1: if msg.receiver == request.user: gremmie@1: receiver_name = msg.sender.username gremmie@1: else: gremmie@1: # replying to message in outbox gremmie@1: receiver_name = msg.receiver.username gremmie@1: gremmie@1: form_data = { gremmie@1: 'receiver': receiver_name, gremmie@1: 'subject': reply_subject(msg.subject), gremmie@1: 'message': quote_message(msg.sender, msg.send_date, msg.message), gremmie@1: 'box': request.GET.get('box', 'inbox'), gremmie@1: } gremmie@1: gremmie@1: compose_form = ComposeForm(request.user, initial=form_data) gremmie@1: gremmie@1: return render_to_response('messages/compose.html', { gremmie@1: 'compose_form': compose_form, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: gremmie@1: @login_required gremmie@1: def compose(request, receiver=None): gremmie@1: """ gremmie@1: Process or prepare the compose form in order to create gremmie@1: a new message. gremmie@1: """ gremmie@1: if request.method == "POST": gremmie@1: if request.POST.get('submit_button', 'Cancel') == 'Cancel': gremmie@1: return HttpResponseRedirect(reverse('messages-inbox')) gremmie@1: compose_form = ComposeForm(request.user, request.POST) gremmie@1: if compose_form.is_valid(): gremmie@1: compose_form.save(sender=request.user) bgneal@178: messages.success(request, 'Message sent.') gremmie@1: return HttpResponseRedirect(reverse('messages-inbox')) gremmie@1: else: gremmie@1: if receiver is not None: gremmie@1: form_data = { gremmie@1: 'receiver': receiver, gremmie@1: } gremmie@1: compose_form = ComposeForm(request.user, initial=form_data) gremmie@1: else: gremmie@1: compose_form = ComposeForm(request.user) gremmie@1: gremmie@1: return render_to_response('messages/compose.html', { gremmie@1: 'compose_form': compose_form, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: gremmie@1: @login_required bgneal@156: @require_POST gremmie@1: def delete(request, msg_id): gremmie@1: """ gremmie@1: Deletes a given message. The user must be either the sender or gremmie@1: receiver for this to succeed. gremmie@1: """ gremmie@1: msg = get_object_or_404(Message, pk=msg_id) gremmie@1: if msg.sender == request.user: gremmie@1: msg.sender_delete_date = datetime.datetime.now() gremmie@1: elif msg.receiver == request.user: gremmie@1: msg.receiver_delete_date = datetime.datetime.now() gremmie@1: else: gremmie@1: raise Http404 gremmie@1: msg.save() bgneal@178: messages.success(request, 'Message sent to trash.') gremmie@1: gremmie@1: return box_redirect(request) gremmie@1: gremmie@1: gremmie@1: @login_required gremmie@1: def delete_bulk(request): gremmie@1: """ gremmie@1: Deletes messages in bulk. The message ID's to be deleted are expected gremmie@1: to be in the delete POST array. The user must be either the sender gremmie@1: or receiver for this to succeed. gremmie@1: """ gremmie@1: if request.method == "POST": gremmie@1: delete_ids = request.POST.getlist('delete_ids') gremmie@1: try: gremmie@1: delete_ids = [int(id) for id in delete_ids] gremmie@1: except ValueError: gremmie@1: raise Http404 gremmie@1: msgs = Message.objects.filter(id__in = delete_ids) gremmie@1: now = datetime.datetime.now() gremmie@1: for msg in msgs: gremmie@1: if msg.sender == request.user: gremmie@1: msg.sender_delete_date = now gremmie@1: msg.save() gremmie@1: elif msg.receiver == request.user: gremmie@1: msg.receiver_delete_date = now gremmie@1: msg.save() bgneal@178: messages.success(request, 'Messages sent to the trash.') gremmie@1: gremmie@1: return box_redirect(request) gremmie@1: gremmie@1: gremmie@1: @login_required bgneal@156: @require_POST gremmie@1: def undelete(request, msg_id): gremmie@1: """ gremmie@1: Undeletes a given message. The user must be either the sender or gremmie@1: receiver for this to succeed. gremmie@1: """ gremmie@1: msg = get_object_or_404(Message, pk=msg_id) gremmie@1: if msg.sender == request.user: gremmie@1: msg.sender_delete_date = None gremmie@1: elif msg.receiver == request.user: gremmie@1: msg.receiver_delete_date = None gremmie@1: else: gremmie@1: raise Http404 gremmie@1: msg.save() bgneal@178: messages.success(request, 'Message retrieved from the trash.') gremmie@1: gremmie@1: return box_redirect(request) gremmie@1: gremmie@1: gremmie@1: @login_required gremmie@1: def undelete_bulk(request): gremmie@1: """ gremmie@1: Undeletes messages in bulk. The message ID's to be deleted are expected gremmie@1: to be in the delete POST array. The user must be either the sender gremmie@1: or receiver for this to succeed. gremmie@1: """ gremmie@1: if request.method == "POST": gremmie@1: undelete_ids = request.POST.getlist('undelete_ids') gremmie@1: try: gremmie@1: undelete_ids = [int(id) for id in undelete_ids] gremmie@1: except ValueError: gremmie@1: raise Http404 gremmie@1: msgs = Message.objects.filter(id__in = undelete_ids) gremmie@1: for msg in msgs: gremmie@1: if msg.sender == request.user: gremmie@1: msg.sender_delete_date = None gremmie@1: msg.save() gremmie@1: elif msg.receiver == request.user: gremmie@1: msg.receiver_delete_date = None gremmie@1: msg.save() bgneal@178: messages.success(request, 'Messages retrieved from the trash.') gremmie@1: gremmie@1: return box_redirect(request) gremmie@1: gremmie@1: gremmie@1: @login_required gremmie@1: def options(request): gremmie@1: """ gremmie@1: View to display/change user options. gremmie@1: """ gremmie@1: if request.method == "POST": gremmie@1: if request.POST.get('submit_button', 'Cancel') == 'Cancel': gremmie@1: return HttpResponseRedirect(reverse('messages-inbox')) gremmie@1: options = Options.objects.for_user(request.user) gremmie@1: form = OptionsForm(request.POST, instance=options) gremmie@1: if form.is_valid(): gremmie@1: form.save() bgneal@178: messages.success(request, 'Options saved.') gremmie@1: return HttpResponseRedirect(reverse('messages-inbox')) gremmie@1: else: gremmie@1: try: gremmie@1: options = Options.objects.for_user(request.user) gremmie@1: except: gremmie@1: options = Options() gremmie@1: options.user = request.user gremmie@1: options.save() gremmie@1: gremmie@1: form = OptionsForm(instance=options) gremmie@1: gremmie@1: return render_to_response('messages/options.html', { gremmie@1: 'form': form, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: