# HG changeset patch # User Brian Neal # Date 1304391418 0 # Node ID d0f0800eef0cd2a9f290acd6a36cb63ee9175289 # Parent 77b3b01843b5fa60eede4a6f7598397d32260146 Making the jquery tabbed version of the messages app the current version and removing the old. Also figured out how to dynamically update the base template's count of unread messages when messages are read. diff -r 77b3b01843b5 -r d0f0800eef0c gpp/messages/models.py --- a/gpp/messages/models.py Sun May 01 02:11:48 2011 +0000 +++ b/gpp/messages/models.py Tue May 03 02:56:58 2011 +0000 @@ -67,10 +67,6 @@ self.html = site_markup(self.message) super(Message, self).save(*args, **kwargs) - @models.permalink - def get_absolute_url(self): - return ('messages.views.view', [str(self.id)]) - def __unicode__(self): return self.subject diff -r 77b3b01843b5 -r d0f0800eef0c gpp/messages/static/js/box.js --- a/gpp/messages/static/js/box.js Sun May 01 02:11:48 2011 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,67 +0,0 @@ -function messages_master_click() -{ - var state = document.getElementById('master_select').checked - for (i = 0; i < document.messages_box_form.length; ++i) - { - if (document.messages_box_form.elements[i].type == 'checkbox') - { - document.messages_box_form.elements[i].checked = state; - } - } -} - -function messages_set_master() -{ - var count = 0; - var numChkBoxes = 0; - for (i = 0; i < document.messages_box_form.length; ++i) - { - if (document.messages_box_form.elements[i].type == 'checkbox' && - document.messages_box_form.elements[i].id != 'master_select') - { - ++numChkBoxes; - if (document.messages_box_form.elements[i].checked) - { - ++count; - } - } - } - document.getElementById('master_select').checked = count == numChkBoxes; -} - -function messages_count_selected() -{ - var count = 0; - for (i = 0; i < document.messages_box_form.length; ++i) - { - if (document.messages_box_form.elements[i].type == 'checkbox' && - document.messages_box_form.elements[i].checked && - document.messages_box_form.elements[i].id != 'master_select') - { - ++count; - } - } - return count; -} - -function messages_confirm_delete() -{ - var count = messages_count_selected(); - if (count == 0) - { - alert("No messages selected."); - return false; - } - return confirm('Really delete selected messages?'); -} - -function messages_confirm_undelete() -{ - var count = messages_count_selected(); - if (count == 0) - { - alert("No messages selected."); - return false; - } - return confirm('Really undelete selected messages?'); -} diff -r 77b3b01843b5 -r d0f0800eef0c gpp/messages/static/js/tabbed_messages.js --- a/gpp/messages/static/js/tabbed_messages.js Sun May 01 02:11:48 2011 +0000 +++ b/gpp/messages/static/js/tabbed_messages.js Tue May 03 02:56:58 2011 +0000 @@ -55,13 +55,36 @@ var doReply = false; var selectedTab = 0; +function updateUnreadMsgText(n) +{ + var txt = ''; + if (n == 1) { + txt = "1 New Message"; + } + else if (n > 1) { + txt = n + " New Messages"; + } + else { + txt = "Private Messages"; + } + $('#unread_msg_text').html(txt); +} + function showMsg(link, id) { $msgDialog.msgId = id; // create a msgId attribute on the dialog var msg = msgCache[id]; // mark as read if necessary - if (username == msg.receiver) { + var $link = $(link); + + if (username == msg.receiver && $link.hasClass('unread')) { $(link).removeClass('unread'); + + // decrement count of unread messages in base template + if (unreadMsgCount > 0) + { + updateUnreadMsgText(--unreadMsgCount); + } } $msgDialog.html(msg.content); @@ -76,7 +99,7 @@ return; } $.ajax({ - url: '/messages/beta/message/', + url: '/messages/message/', type: 'POST', data: { msg_id : id @@ -95,7 +118,7 @@ function submitOptions(form) { $.ajax({ - url: '/messages/beta/options-tab/', + url: '/messages/options-tab/', type: 'POST', data: $(form).serialize(), dataType: 'html', @@ -112,7 +135,7 @@ function messageSubmit(form) { $.ajax({ - url: '/messages/beta/compose-tab/', + url: '/messages/compose-tab/', type: 'POST', data: $(form).serialize(), dataType: 'html', @@ -137,7 +160,7 @@ function bulkMsgAction(form, action) { if (confirm("Really " + action + " checked messages?")) { $.ajax({ - url: '/messages/beta/bulk/', + url: '/messages/bulk/', type: 'POST', data: $(form).serialize(), dataType: 'text', diff -r 77b3b01843b5 -r d0f0800eef0c gpp/messages/templatetags/messages_tags.py --- a/gpp/messages/templatetags/messages_tags.py Sun May 01 02:11:48 2011 +0000 +++ b/gpp/messages/templatetags/messages_tags.py Tue May 03 02:56:58 2011 +0000 @@ -9,17 +9,7 @@ register = template.Library() -@register.simple_tag +@register.inclusion_tag('messages/unread_messages_tag.html') def unread_messages(user): - inbox_url = reverse('messages-inbox') unread_count = Message.objects.unread_count(user) - if unread_count == 0: - link_text = u"Private Messages" - elif unread_count == 1: - link_text = u"1 New Message" - else: - link_text = u"%s New Messages" % unread_count - return u'%s' % (inbox_url, link_text) - - -# vim: ts=4 sw=4 + return {'unread_count': unread_count} diff -r 77b3b01843b5 -r d0f0800eef0c gpp/messages/urls.py --- a/gpp/messages/urls.py Sun May 01 02:11:48 2011 +0000 +++ b/gpp/messages/urls.py Tue May 03 02:56:58 2011 +0000 @@ -1,59 +1,35 @@ """urls for the Messages application""" from django.conf.urls.defaults import * -from django.views.generic import RedirectView - urlpatterns = patterns('messages.views', - url(r'^inbox/$', 'inbox', name='messages-inbox'), - url(r'^outbox/$', 'outbox', name='messages-outbox'), - url(r'^trash/$', 'trash', name='messages-trash'), - url(r'^view/(\d+)/$', 'view', name='messages-view'), - url(r'^reply/(\d+)/$', 'reply', name='messages-reply'), - url(r'^compose/$', 'compose', name='messages-compose'), - url(r'^compose/([\w.@+-]{1,30})/$', 'compose', name='messages-compose_to'), - url(r'^delete/$', 'delete_bulk', name='messages-delete_bulk'), - url(r'^delete/(\d+)/$', 'delete', name='messages-delete'), - url(r'^undelete/$', 'undelete_bulk', name='messages-undelete_bulk'), - url(r'^undelete/(\d+)/$', 'undelete', name='messages-undelete'), - url(r'^options/$', 'options', name='messages-options'), + url(r'^$', + 'index', + name='messages-index'), + url(r'^(inbox|compose|outbox|trash|options)/$', + 'index', + name='messages-index_named'), + url(r'^compose/([\w.@+-]{1,30})/$', + 'compose_to', + name='messages-compose_to'), + url(r'^inbox-tab/$', + 'inbox', + name='messages-inbox'), + url(r'^outbox-tab/$', + 'outbox', + name='messages-outbox'), + url(r'^trash-tab/$', + 'trash', + name='messages-trash'), + url(r'^message/$', + 'message', + name='messages-message'), + url(r'^options-tab/$', + 'options', + name='messages-options'), + url(r'^compose-tab/$', + 'compose', + name='messages-compose'), + url(r'^bulk/$', + 'bulk', + name='messages-bulk'), ) - -urlpatterns += patterns('', - (r'^$', RedirectView.as_view(url='inbox/')), -) - -urlpatterns += patterns('messages.views2', - url(r'^beta/$', - 'index', - name='messages-beta_index'), - url(r'^beta/(inbox|compose|outbox|trash|options)/$', - 'index', - name='messages-beta_index_named'), - url(r'^beta/compose/([\w.@+-]{1,30})/$', - 'compose_to', - name='messages-beta_compose_to'), - url(r'^beta/inbox-tab/$', - 'inbox', - name='messages-beta_inbox'), - url(r'^beta/outbox-tab/$', - 'outbox', - name='messages-beta_outbox'), - url(r'^beta/trash-tab/$', - 'trash', - name='messages-beta_trash'), - url(r'^beta/message/$', - 'message', - name='messages-beta_message'), - url(r'^beta/options-tab/$', - 'options', - name='messages-beta_options'), - url(r'^beta/compose-tab/$', - 'compose', - name='messages-beta_compose'), - url(r'^beta/compose-tab/([\w.@+-]{1,30})/$', - 'compose', - name='messages-beta_compose_to'), - url(r'^beta/bulk/$', - 'bulk', - name='messages-beta_bulk'), -) diff -r 77b3b01843b5 -r d0f0800eef0c gpp/messages/views.py --- a/gpp/messages/views.py Sun May 01 02:11:48 2011 +0000 +++ b/gpp/messages/views.py Tue May 03 02:56:58 2011 +0000 @@ -1,333 +1,306 @@ -"""Views for the messages application""" +""" +Views for the messages application. -import collections +""" import datetime -from django.shortcuts import render_to_response -from django.template import RequestContext +from django.contrib.auth.decorators import login_required +from django.contrib.auth.models import User +from django.contrib import messages +from django.core.paginator import Paginator, EmptyPage, InvalidPage +from django.core.urlresolvers import reverse +from django.http import HttpResponse +from django.http import HttpResponseForbidden +from django.http import HttpResponseNotAllowed from django.http import HttpResponseRedirect -from django.contrib.auth.decorators import login_required -from django.contrib import messages from django.shortcuts import get_object_or_404 -from django.core.urlresolvers import reverse -from django.http import Http404 -from django.views.decorators.http import require_POST +from django.shortcuts import render +import django.utils.simplejson as json -from messages.models import Message -from messages.models import Options -from messages.forms import ComposeForm -from messages.forms import OptionsForm -from messages.utils import reply_subject -from messages.utils import quote_message +from messages.models import Message, Options +from messages.forms import OptionsForm, ComposeForm +from messages.utils import reply_subject, quote_message -BOX_MAP = { - 'inbox': 'messages-inbox', - 'outbox': 'messages-outbox', - 'trash': 'messages-trash', +MSGS_PER_PAGE = 20 + +TAB_INDICES = { + 'inbox': 0, + 'compose': 1, + 'outbox': 2, + 'trash': 3, + 'options': 4, } -MSG_DELETED, MSG_TRASHED, MSG_ERROR = range(3) - -def box_redirect(request): - """ - Determines which box to redirect to by looking for a GET or - POST parameter. - """ - if request.method == 'GET': - box = request.GET.get('box', 'inbox') - else: - box = request.POST.get('box', 'inbox') - if BOX_MAP.has_key(box): - url = reverse(BOX_MAP[box]) - else: - url = reverse(BOX_MAP['inbox']) - return HttpResponseRedirect(url) +def _get_page(request): + try: + n = int(request.GET.get('page', '1')) + except ValueError: + n = 1 + return n @login_required -def inbox(request): - """Displays the inbox for the user making the request.""" - msgs = Message.objects.inbox(request.user) - return render_to_response('messages/inbox.html', { - 'msgs': msgs, - }, - context_instance = RequestContext(request)) +def index(request, tab=None): + """ + This function displays the base tabbed private messages view. + + """ + tab_index = TAB_INDICES[tab] if tab else 0 + return render(request, 'messages/tabbed_base.html', { + 'tab': tab_index, + 'unread_count': Message.objects.unread_count(request.user), + }) @login_required +def compose_to(request, receiver): + """ + This function displays the base tabbed private messages view, + and configures it to display the compose PM tab for the given + receiver. + + """ + user = get_object_or_404(User, username=receiver) + tab_index = TAB_INDICES['compose'] + return render(request, 'messages/tabbed_base.html', { + 'tab': tab_index, + 'receiver': receiver, + 'unread_count': Message.objects.unread_count(request.user), + }) + + +def inbox(request): + """ + Returns the inbox for the user. + + """ + if not request.user.is_authenticated(): + return HttpResponseForbidden() + + msg_list = Message.objects.inbox(request.user) + paginator = Paginator(msg_list, MSGS_PER_PAGE) + try: + msgs = paginator.page(_get_page(request)) + except EmptyPage, InvalidPage: + msgs = paginator.page(paginator.num_pages) + + return render(request, 'messages/inbox_tab.html', { + 'msgs': msgs, + 'url': reverse('messages-inbox'), + }) + + def outbox(request): - """Displays the outbox for the user making the request.""" - msgs = Message.objects.outbox(request.user) - return render_to_response('messages/outbox.html', { + """ + Returns the outbox for the user. + + """ + if not request.user.is_authenticated(): + return HttpResponseForbidden() + + msg_list = Message.objects.outbox(request.user) + paginator = Paginator(msg_list, MSGS_PER_PAGE) + try: + msgs = paginator.page(_get_page(request)) + except EmptyPage, InvalidPage: + msgs = paginator.page(paginator.num_pages) + + return render(request, 'messages/outbox_tab.html', { 'msgs': msgs, - }, - context_instance = RequestContext(request)) + 'url': reverse('messages-outbox'), + }) -@login_required def trash(request): - """Displays the trash for the user making the request.""" - msgs = Message.objects.trash(request.user) - return render_to_response('messages/trash.html', { + """ + Returns the trash for the user. + + """ + if not request.user.is_authenticated(): + return HttpResponseForbidden() + + msg_list = Message.objects.trash(request.user) + paginator = Paginator(msg_list, MSGS_PER_PAGE) + try: + msgs = paginator.page(_get_page(request)) + except EmptyPage, InvalidPage: + msgs = paginator.page(paginator.num_pages) + + return render(request, 'messages/trash_tab.html', { 'msgs': msgs, - }, - context_instance = RequestContext(request)) + 'url': reverse('messages-trash'), + }) -@login_required -def view(request, msg_id): +def message(request): """ - View a given message. Only the sender or receiver can see - the message. + This view function retrieves a message and returns it as a JSON object. + """ - msg = get_object_or_404(Message, pk=msg_id) + if not request.user.is_authenticated(): + return HttpResponseForbidden() + if request.method != 'POST': + return HttpResponseNotAllowed(['POST']) + + msg_id = request.POST.get('msg_id') + msg = get_object_or_404(Message.objects.select_related(), pk=msg_id) if msg.sender != request.user and msg.receiver != request.user: - raise Http404 + return HttpResponseForbidden() if msg.receiver == request.user and msg.read_date is None: msg.read_date = datetime.datetime.now() msg.save() - box = request.GET.get('box', None) + msg_dict = dict(subject=msg.subject, + sender=msg.sender.username, + receiver=msg.receiver.username, + content=msg.html, + re_subject=reply_subject(msg.subject), + re_content=quote_message(msg.sender.username, msg.send_date, + msg.message)) - return render_to_response('messages/view.html', { - 'box': box, - 'msg': msg, - 'is_deleted': msg.is_deleted(request.user), - }, - context_instance = RequestContext(request)) + result = json.dumps(msg_dict, ensure_ascii=False) + return HttpResponse(result, content_type='application/json') -@login_required -def reply(request, msg_id): +def options(request): """ - Process or prepare the compose form in order to reply - to a given message. + This view handles the displaying and changing of private message options. + """ - msg = get_object_or_404(Message, pk=msg_id) + if not request.user.is_authenticated(): + return HttpResponseForbidden() if request.method == "POST": - if request.POST.get('submit_button', 'Cancel') == 'Cancel': - return box_redirect(request) - compose_form = ComposeForm(request.user, request.POST) - if compose_form.is_valid(): - compose_form.save(sender=request.user, parent_msg=msg) - messages.success(request, 'Reply sent.') - return box_redirect(request) + options = Options.objects.for_user(request.user) + form = OptionsForm(request.POST, instance=options, prefix='opts') + if form.is_valid(): + form.save() + messages.success(request, 'Options saved.') else: - if msg.receiver == request.user: - receiver_name = msg.sender.username - else: - # replying to message in outbox - receiver_name = msg.receiver.username + options = Options.objects.for_user(request.user) + form = OptionsForm(instance=options, prefix='opts') - form_data = { - 'receiver': receiver_name, - 'subject': reply_subject(msg.subject), - 'message': quote_message(msg.sender, msg.send_date, msg.message), - 'box': request.GET.get('box', 'inbox'), - } + return render(request, 'messages/options_tab.html', { + 'form': form, + }) - compose_form = ComposeForm(request.user, initial=form_data) - return render_to_response('messages/compose.html', { - 'compose_form': compose_form, - }, - context_instance = RequestContext(request)) - - -@login_required def compose(request, receiver=None): """ - Process or prepare the compose form in order to create - a new message. + Process or prepare the compose form to create a new private message. + """ + if not request.user.is_authenticated(): + return HttpResponseForbidden() + if request.method == "POST": - if request.POST.get('submit_button', 'Cancel') == 'Cancel': - return HttpResponseRedirect(reverse('messages-inbox')) compose_form = ComposeForm(request.user, request.POST) if compose_form.is_valid(): compose_form.save(sender=request.user) messages.success(request, 'Message sent.') - return HttpResponseRedirect(reverse('messages-inbox')) + return HttpResponseRedirect(reverse('messages-index_named', args=['compose'])) else: if receiver is not None: - form_data = { - 'receiver': receiver, - } + form_data = {'receiver': receiver} compose_form = ComposeForm(request.user, initial=form_data) else: compose_form = ComposeForm(request.user) - return render_to_response('messages/compose.html', { + return render(request, 'messages/compose_tab.html', { 'compose_form': compose_form, - }, - context_instance = RequestContext(request)) + }) -def _delete_message(user, msg): +def _only_integers(slist): """ - Deletes a given message. The user must be either the sender or - receiver for this to succeed. - If both parties have deleted the message, it is deleted from the - database. If only one party has deleted the message, the message - is just sent to the trash. - - Returns MSG_DELETED, MSG_TRASHED, or MSG_ERROR to indicate what - action was performed. + Accepts a list of strings. Returns a list of integers consisting of only + those elements from the original list that could be converted to integers """ - if msg.sender == user: - if (msg.receiver_delete_date is not None or - msg.read_date is None): - # Both parties deleted the message or receiver hasn't read it yet, - # we can delete it now - msg.delete() - return MSG_DELETED + result = [] + for s in slist: + try: + n = int(s) + except ValueError: + pass else: - # receiver still has it in inbox - msg.sender_delete_date = datetime.datetime.now() + result.append(n) + return result + + +def _delete_msgs(user, msg_ids): + """ + Deletes the messages given by the list of msg_ids. For this to succeed, the + user has to be either the sender or receiver on each message. + + """ + msg_ids = _only_integers(msg_ids) + msgs = Message.objects.filter(id__in=msg_ids) + + for msg in msgs: + if msg.sender == user: + if (msg.receiver_delete_date is not None or + msg.read_date is None): + # Both parties deleted the message or receiver hasn't read it + # yet, we can delete it now + msg.delete() + else: + # receiver still has it in inbox + msg.sender_delete_date = datetime.datetime.now() + msg.save() + + elif msg.receiver == user: + if msg.sender_delete_date is not None: + # both parties deleted the message, we can delete it now + msg.delete() + else: + # sender still has it in the outbox + msg.receiver_delete_date = datetime.datetime.now() + msg.save() + + +def _undelete_msgs(user, msg_ids): + """ + Attempts to "undelete" the messages given by the msg_ids list. + This will only succeed if the user is either the sender or receiver. + + """ + msg_ids = _only_integers(msg_ids) + msgs = Message.objects.filter(id__in=msg_ids) + for msg in msgs: + if msg.sender == user: + msg.sender_delete_date = None msg.save() - return MSG_TRASHED + elif msg.receiver == user: + msg.receiver_delete_date = None + msg.save() - elif msg.receiver == user: - if msg.sender_delete_date is not None: - # both parties deleted the message, we can delete it now - msg.delete() - return MSG_DELETED - else: - # sender still has it in the outbox - msg.receiver_delete_date = datetime.datetime.now() - msg.save() - return MSG_TRASHED - return MSG_ERROR +def bulk(request): + """ + This view processes messages in bulk. Arrays of message ids are expected in + the POST query dict: inbox_ids and outbox_ids will be deleted; trash_ids will + be undeleted. + """ + if not request.user.is_authenticated(): + return HttpResponseForbidden() + if request.method != 'POST': + return HttpResponseNotAllowed(['POST']) -@login_required -@require_POST -def delete(request, msg_id): - """ - Deletes a given message. The user must be either the sender or - receiver for this to succeed. - """ - msg = get_object_or_404(Message, pk=msg_id) - result = _delete_message(request.user, msg) - if result == MSG_DELETED: - messages.success(request, 'Message deleted.') - elif result == MSG_TRASHED: - messages.success(request, 'Message sent to trash.') - else: - messages.error(request, 'Error deleting message.') + delete_ids = [] + if 'inbox_ids' in request.POST: + delete_ids.extend(request.POST.getlist('inbox_ids')) + if 'outbox_ids' in request.POST: + delete_ids.extend(request.POST.getlist('outbox_ids')) - return box_redirect(request) + if len(delete_ids): + _delete_msgs(request.user, delete_ids) + if 'trash_ids' in request.POST: + _undelete_msgs(request.user, request.POST.getlist('trash_ids')) -@login_required -def delete_bulk(request): - """ - Deletes messages in bulk. The message ID's to be deleted are expected - to be in the delete POST array. The user must be either the sender - or receiver for this to succeed. - """ - if request.method == "POST": - delete_ids = request.POST.getlist('delete_ids') - try: - delete_ids = [int(id) for id in delete_ids] - except ValueError: - raise Http404 - - msgs = Message.objects.filter(id__in=delete_ids) - - counts = collections.defaultdict(int) - for msg in msgs: - result = _delete_message(request.user, msg) - counts[result] += 1 - - if counts[MSG_DELETED]: - messages.success(request, 'Messages deleted: %d' % counts[MSG_DELETED]) - if counts[MSG_TRASHED]: - messages.success(request, 'Messages sent to trash: %d' % counts[MSG_TRASHED]) - if counts[MSG_ERROR]: - messages.error(request, 'Message errors: %d' % counts[MSG_ERROR]) - - return box_redirect(request) - - -@login_required -@require_POST -def undelete(request, msg_id): - """ - Undeletes a given message. The user must be either the sender or - receiver for this to succeed. - """ - msg = get_object_or_404(Message, pk=msg_id) - if msg.sender == request.user: - msg.sender_delete_date = None - elif msg.receiver == request.user: - msg.receiver_delete_date = None - else: - raise Http404 - msg.save() - messages.success(request, 'Message retrieved from the trash.') - - return box_redirect(request) - - -@login_required -def undelete_bulk(request): - """ - Undeletes messages in bulk. The message ID's to be deleted are expected - to be in the delete POST array. The user must be either the sender - or receiver for this to succeed. - """ - if request.method == "POST": - undelete_ids = request.POST.getlist('undelete_ids') - try: - undelete_ids = [int(id) for id in undelete_ids] - except ValueError: - raise Http404 - msgs = Message.objects.filter(id__in = undelete_ids) - for msg in msgs: - if msg.sender == request.user: - msg.sender_delete_date = None - msg.save() - elif msg.receiver == request.user: - msg.receiver_delete_date = None - msg.save() - messages.success(request, 'Messages retrieved from the trash.') - - return box_redirect(request) - - -@login_required -def options(request): - """ - View to display/change user options. - """ - if request.method == "POST": - if request.POST.get('submit_button', 'Cancel') == 'Cancel': - return HttpResponseRedirect(reverse('messages-inbox')) - options = Options.objects.for_user(request.user) - form = OptionsForm(request.POST, instance=options) - if form.is_valid(): - form.save() - messages.success(request, 'Options saved.') - return HttpResponseRedirect(reverse('messages-inbox')) - else: - try: - options = Options.objects.for_user(request.user) - except: - options = Options() - options.user = request.user - options.save() - - form = OptionsForm(instance=options) - - return render_to_response('messages/options.html', { - 'form': form, - }, - context_instance = RequestContext(request)) - + return HttpResponse(''); diff -r 77b3b01843b5 -r d0f0800eef0c gpp/messages/views2.py --- a/gpp/messages/views2.py Sun May 01 02:11:48 2011 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,304 +0,0 @@ -""" -Views for the messages application. - -""" -import datetime - -from django.contrib.auth.decorators import login_required -from django.contrib.auth.models import User -from django.contrib import messages -from django.core.paginator import Paginator, EmptyPage, InvalidPage -from django.core.urlresolvers import reverse -from django.http import HttpResponse -from django.http import HttpResponseForbidden -from django.http import HttpResponseNotAllowed -from django.http import HttpResponseRedirect -from django.shortcuts import get_object_or_404 -from django.shortcuts import render -import django.utils.simplejson as json - -from messages.models import Message, Options -from messages.forms import OptionsForm, ComposeForm -from messages.utils import reply_subject, quote_message - - -MSGS_PER_PAGE = 20 - -TAB_INDICES = { - 'inbox': 0, - 'compose': 1, - 'outbox': 2, - 'trash': 3, - 'options': 4, -} - - -def _get_page(request): - try: - n = int(request.GET.get('page', '1')) - except ValueError: - n = 1 - return n - - -@login_required -def index(request, tab=None): - """ - This function displays the base tabbed private messages view. - - """ - tab_index = TAB_INDICES[tab] if tab else 0 - return render(request, 'messages/tabbed_base.html', { - 'tab': tab_index, - }) - - -@login_required -def compose_to(request, receiver): - """ - This function displays the base tabbed private messages view, - and configures it to display the compose PM tab for the given - receiver. - - """ - user = get_object_or_404(User, username=receiver) - tab_index = TAB_INDICES['compose'] - return render(request, 'messages/tabbed_base.html', { - 'tab': tab_index, - 'receiver': receiver, - }) - - -def inbox(request): - """ - Returns the inbox for the user. - - """ - if not request.user.is_authenticated(): - return HttpResponseForbidden() - - msg_list = Message.objects.inbox(request.user) - paginator = Paginator(msg_list, MSGS_PER_PAGE) - try: - msgs = paginator.page(_get_page(request)) - except EmptyPage, InvalidPage: - msgs = paginator.page(paginator.num_pages) - - return render(request, 'messages/inbox_tab.html', { - 'msgs': msgs, - 'url': reverse('messages-beta_inbox'), - }) - - -def outbox(request): - """ - Returns the outbox for the user. - - """ - if not request.user.is_authenticated(): - return HttpResponseForbidden() - - msg_list = Message.objects.outbox(request.user) - paginator = Paginator(msg_list, MSGS_PER_PAGE) - try: - msgs = paginator.page(_get_page(request)) - except EmptyPage, InvalidPage: - msgs = paginator.page(paginator.num_pages) - - return render(request, 'messages/outbox_tab.html', { - 'msgs': msgs, - 'url': reverse('messages-beta_outbox'), - }) - - -def trash(request): - """ - Returns the trash for the user. - - """ - if not request.user.is_authenticated(): - return HttpResponseForbidden() - - msg_list = Message.objects.trash(request.user) - paginator = Paginator(msg_list, MSGS_PER_PAGE) - try: - msgs = paginator.page(_get_page(request)) - except EmptyPage, InvalidPage: - msgs = paginator.page(paginator.num_pages) - - return render(request, 'messages/trash_tab.html', { - 'msgs': msgs, - 'url': reverse('messages-beta_trash'), - }) - - -def message(request): - """ - This view function retrieves a message and returns it as a JSON object. - - """ - if not request.user.is_authenticated(): - return HttpResponseForbidden() - if request.method != 'POST': - return HttpResponseNotAllowed(['POST']) - - msg_id = request.POST.get('msg_id') - msg = get_object_or_404(Message.objects.select_related(), pk=msg_id) - if msg.sender != request.user and msg.receiver != request.user: - return HttpResponseForbidden() - - if msg.receiver == request.user and msg.read_date is None: - msg.read_date = datetime.datetime.now() - msg.save() - - msg_dict = dict(subject=msg.subject, - sender=msg.sender.username, - receiver=msg.receiver.username, - content=msg.html, - re_subject=reply_subject(msg.subject), - re_content=quote_message(msg.sender.username, msg.send_date, - msg.message)) - - result = json.dumps(msg_dict, ensure_ascii=False) - return HttpResponse(result, content_type='application/json') - - -def options(request): - """ - This view handles the displaying and changing of private message options. - - """ - if not request.user.is_authenticated(): - return HttpResponseForbidden() - - if request.method == "POST": - options = Options.objects.for_user(request.user) - form = OptionsForm(request.POST, instance=options, prefix='opts') - if form.is_valid(): - form.save() - messages.success(request, 'Options saved.') - else: - options = Options.objects.for_user(request.user) - form = OptionsForm(instance=options, prefix='opts') - - return render(request, 'messages/options_tab.html', { - 'form': form, - }) - - -def compose(request, receiver=None): - """ - Process or prepare the compose form to create a new private message. - - """ - if not request.user.is_authenticated(): - return HttpResponseForbidden() - - if request.method == "POST": - compose_form = ComposeForm(request.user, request.POST) - if compose_form.is_valid(): - compose_form.save(sender=request.user) - messages.success(request, 'Message sent.') - return HttpResponseRedirect(reverse('messages-beta_index_named', args=['compose'])) - else: - if receiver is not None: - form_data = {'receiver': receiver} - compose_form = ComposeForm(request.user, initial=form_data) - else: - compose_form = ComposeForm(request.user) - - return render(request, 'messages/compose_tab.html', { - 'compose_form': compose_form, - }) - - -def _only_integers(slist): - """ - Accepts a list of strings. Returns a list of integers consisting of only - those elements from the original list that could be converted to integers - - """ - result = [] - for s in slist: - try: - n = int(s) - except ValueError: - pass - else: - result.append(n) - return result - - -def _delete_msgs(user, msg_ids): - """ - Deletes the messages given by the list of msg_ids. For this to succeed, the - user has to be either the sender or receiver on each message. - - """ - msg_ids = _only_integers(msg_ids) - msgs = Message.objects.filter(id__in=msg_ids) - - for msg in msgs: - if msg.sender == user: - if (msg.receiver_delete_date is not None or - msg.read_date is None): - # Both parties deleted the message or receiver hasn't read it - # yet, we can delete it now - msg.delete() - else: - # receiver still has it in inbox - msg.sender_delete_date = datetime.datetime.now() - msg.save() - - elif msg.receiver == user: - if msg.sender_delete_date is not None: - # both parties deleted the message, we can delete it now - msg.delete() - else: - # sender still has it in the outbox - msg.receiver_delete_date = datetime.datetime.now() - msg.save() - - -def _undelete_msgs(user, msg_ids): - """ - Attempts to "undelete" the messages given by the msg_ids list. - This will only succeed if the user is either the sender or receiver. - - """ - msg_ids = _only_integers(msg_ids) - msgs = Message.objects.filter(id__in=msg_ids) - for msg in msgs: - if msg.sender == user: - msg.sender_delete_date = None - msg.save() - elif msg.receiver == user: - msg.receiver_delete_date = None - msg.save() - - -def bulk(request): - """ - This view processes messages in bulk. Arrays of message ids are expected in - the POST query dict: inbox_ids and outbox_ids will be deleted; trash_ids will - be undeleted. - - """ - if not request.user.is_authenticated(): - return HttpResponseForbidden() - if request.method != 'POST': - return HttpResponseNotAllowed(['POST']) - - delete_ids = [] - if 'inbox_ids' in request.POST: - delete_ids.extend(request.POST.getlist('inbox_ids')) - if 'outbox_ids' in request.POST: - delete_ids.extend(request.POST.getlist('outbox_ids')) - - if len(delete_ids): - _delete_msgs(request.user, delete_ids) - - if 'trash_ids' in request.POST: - _undelete_msgs(request.user, request.POST.getlist('trash_ids')) - - return HttpResponse(''); diff -r 77b3b01843b5 -r d0f0800eef0c gpp/templates/base.html --- a/gpp/templates/base.html Sun May 01 02:11:48 2011 +0000 +++ b/gpp/templates/base.html Tue May 03 02:56:58 2011 +0000 @@ -65,7 +65,7 @@
  • IRC
  • Member List
  • Member Map
  • -
  • Private Messages
  • +
  • Private Messages
  • Podcast
  • Polls
  • Photo of the Day
  • diff -r 77b3b01843b5 -r d0f0800eef0c gpp/templates/messages/base.html --- a/gpp/templates/messages/base.html Sun May 01 02:11:48 2011 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,24 +0,0 @@ -{% extends 'base.html' %} -{% load url from future %} -{% block custom_css %} - - -{% endblock %} -{% block content %} -

    Your Private Messages

    - -{% block messages_content %} -{% endblock %} -
    -{% endblock %} diff -r 77b3b01843b5 -r d0f0800eef0c gpp/templates/messages/compose.html --- a/gpp/templates/messages/compose.html Sun May 01 02:11:48 2011 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,24 +0,0 @@ -{% extends 'messages/base.html' %} -{% load core_tags %} -{% block title %}Messages: Compose{% endblock %} -{% block custom_js %} - {{ compose_form.media }} -{% endblock %} -{% block compose-class %}class="active"{% endblock %} -{% block messages_content %} -

    Compose Message

    -
    {% csrf_token %} - -{{ compose_form.as_table }} - - - - -
      - {% comment_dialogs %} - - -
    -
    -
    -{% endblock %} diff -r 77b3b01843b5 -r d0f0800eef0c gpp/templates/messages/compose_tab.html --- a/gpp/templates/messages/compose_tab.html Sun May 01 02:11:48 2011 +0000 +++ b/gpp/templates/messages/compose_tab.html Tue May 03 02:56:58 2011 +0000 @@ -9,7 +9,7 @@ {% endfor %} {% endif %} -
    {% csrf_token %} +{% csrf_token %} {{ compose_form.as_table }} diff -r 77b3b01843b5 -r d0f0800eef0c gpp/templates/messages/inbox.html --- a/gpp/templates/messages/inbox.html Sun May 01 02:11:48 2011 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,54 +0,0 @@ -{% extends 'messages/base.html' %} -{% load url from future %} -{% block title %}Messages: Inbox{% endblock %} -{% block custom_js %} - -{% endblock %} -{% block inbox-class %}class="active"{% endblock %} -{% block messages_content %} -

    Inbox

    -{% if messages %} - -{% endif %} -{% if msgs %} - {% csrf_token %} -
    - - - - - - - {% for msg in msgs %} - - - - - - - {% endfor %} - -
    FromSubjectDate
    - {{ msg.sender.username }} - {% if msg.unread %}{% endif %} - {% if msg.replied_to %}{% endif %} - {{ msg.subject }} - {% if msg.replied_to %}{% endif %} - {% if msg.unread %}{% endif %} - {{ msg.send_date|date:"M j, Y g:i A" }}
    - -
    - -{% else %} -

    Your Inbox is empty.

    -{% endif %} -{% endblock %} diff -r 77b3b01843b5 -r d0f0800eef0c gpp/templates/messages/markdown.html --- a/gpp/templates/messages/markdown.html Sun May 01 02:11:48 2011 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,3 +0,0 @@ -{% load markup %} -{% load smiley_tags %} -{{ data|markdown:"safe"|smilify }} diff -r 77b3b01843b5 -r d0f0800eef0c gpp/templates/messages/options.html --- a/gpp/templates/messages/options.html Sun May 01 02:11:48 2011 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,26 +0,0 @@ -{% extends 'messages/base.html' %} -{% block title %}Messages: Options{% endblock %} -{% block options-class %}active{% endblock %} -{% block messages_content %} -

    Private Message Options

    -{% if messages %} - -{% endif %} -
    {% csrf_token %} - -{{ form.as_table }} - - - - -
      - - -
    -
    -
    -{% endblock %} diff -r 77b3b01843b5 -r d0f0800eef0c gpp/templates/messages/outbox.html --- a/gpp/templates/messages/outbox.html Sun May 01 02:11:48 2011 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,52 +0,0 @@ -{% extends 'messages/base.html' %} -{% load url from future %} -{% block title %}Messages: Outbox{% endblock %} -{% block custom_js %} - -{% endblock %} -{% block outbox-class %}class="active"{% endblock %} -{% block messages_content %} -

    Outbox

    -{% if messages %} - -{% endif %} -{% if msgs %} -
    {% csrf_token %} - - - - - - - - - {% for msg in msgs %} - - - - - - - - {% endfor %} - -
    ToSubjectSentReceived
    - {{ msg.receiver.username }} - {% if msg.unread %}{% endif %} - {% if msg.replied_to %}{% endif %} - {{ msg.subject }} - {% if msg.replied_to %}{% endif %} - {% if msg.unread %}{% endif %} - {{ msg.send_date|date:"M j, Y g:i A" }}{% if msg.unread %}Unread{% else %}{{ msg.read_date|date:"M j, Y g:i A" }}{% endif %}
    - -
    -{% else %} -

    Your Outbox is empty.

    -{% endif %} -{% endblock %} diff -r 77b3b01843b5 -r d0f0800eef0c gpp/templates/messages/tabbed_base.html --- a/gpp/templates/messages/tabbed_base.html Sun May 01 02:11:48 2011 +0000 +++ b/gpp/templates/messages/tabbed_base.html Tue May 03 02:56:58 2011 +0000 @@ -15,26 +15,21 @@ {% else %} var receiver = ""; {% endif %} + var unreadMsgCount = {{ unread_count }}; //]]> {% endblock %} {% block content %} -

    Your Private Messages (Beta)

    -

    -This is an experimental version of the SG101 private message system. Don't worry, if this -isn't working for you, you can always go back to the -current system. Please leave any feedback on this -change in forums. Thanks! -

    +

    Your Private Messages

    diff -r 77b3b01843b5 -r d0f0800eef0c gpp/templates/messages/trash.html --- a/gpp/templates/messages/trash.html Sun May 01 02:11:48 2011 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,52 +0,0 @@ -{% extends 'messages/base.html' %} -{% load url from future %} -{% block title %}Messages: Trash{% endblock %} -{% block custom_js %} - -{% endblock %} -{% block trash-class %}class="active"{% endblock %} -{% block messages_content %} -

    Trash

    -{% if messages %} - -{% endif %} -{% if msgs %} -
    {% csrf_token %} - - - - - - - - - {% for msg in msgs %} - - - - - - - - {% endfor %} - -
    FromToSubjectDate
    - {{ msg.sender.username }} - {{ msg.receiver.username }} - {% if msg.unread %}{% endif %} - {% if msg.replied_to %}{% endif %} - {{ msg.subject }} - {% if msg.replied_to %}{% endif %} - {% if msg.unread %}{% endif %} - {{ msg.send_date|date:"M j, Y g:i:s A T" }}
    -
    -{% else %} -

    Your Trash is empty.

    -{% endif %} -{% endblock %} diff -r 77b3b01843b5 -r d0f0800eef0c gpp/templates/messages/unread_messages_tag.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/templates/messages/unread_messages_tag.html Tue May 03 02:56:58 2011 +0000 @@ -0,0 +1,10 @@ +{% load url from future %} +{% if unread_count == 0 %} +Private Messages +{% endif %} +{% if unread_count == 1 %} +1 New Message +{% endif %} +{% if unread_count > 1 %} +{{ unread_count }} New Messages +{% endif %} diff -r 77b3b01843b5 -r d0f0800eef0c gpp/templates/messages/view.html --- a/gpp/templates/messages/view.html Sun May 01 02:11:48 2011 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,38 +0,0 @@ -{% extends 'messages/base.html' %} -{% load url from future %} -{% block title %}Messages: {{ msg.subject }}{% endblock %} -{% block messages_content %} -

    Viewing Message: {{ msg.subject }}

    - - - - - - - -
    Subject:{{ msg.subject }}
    From: - {{ msg.sender }}
    To: - {{ msg.receiver }}
    Date Sent:{{ msg.send_date|date:"F d, Y g:i:s A T" }}
    Date Received:{% if msg.unread %}Unread{% else %}{{ msg.read_date|date:"F d, Y g:i:s A T" }}{% endif %}
    - -
    - {{ msg.html|safe }} -
    -{% if msg.signature_attached %} -
    -
    - {{ msg.sender.get_profile.signature_html|safe }} -
    -{% endif %} -{% if is_deleted %} -
    {% csrf_token %} - {% if box %}{% endif %} - -
    -{% else %} -Reply | -
    {% csrf_token %} - {% if box %}{% endif %} - -
    -{% endif %} -{% endblock %}