changeset 807:22cc8c1aec03

Private message refactor: add undelete functionality.
author Brian Neal <bgneal@gmail.com>
date Mon, 01 Sep 2014 16:22:13 -0500
parents 59e36169a814
children 0f0ba45704b7
files messages/urls.py messages/views.py sg101/templates/messages/trash.html
diffstat 3 files changed, 45 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/messages/urls.py	Mon Sep 01 15:53:35 2014 -0500
+++ b/messages/urls.py	Mon Sep 01 16:22:13 2014 -0500
@@ -24,9 +24,9 @@
     url(r'^delete/$',
         'messages.views.delete',
         name='messages-delete'),
-#   url(r'^undelete/$',
-#       'messages.views.undelete',
-#       name='messages-undelete'),
+    url(r'^undelete/$',
+        'messages.views.undelete',
+        name='messages-undelete'),
     url(r'^view/(\d+)/$',
         'messages.views.view',
         name='messages-view'),
--- a/messages/views.py	Mon Sep 01 15:53:35 2014 -0500
+++ b/messages/views.py	Mon Sep 01 16:22:13 2014 -0500
@@ -3,17 +3,12 @@
 
 """
 import datetime
-import json
 
 from django.contrib.auth.decorators import login_required
 from django.views.decorators.http import require_POST
-from django.contrib.auth.models import User
 from django.contrib import messages as django_messages
 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
 from django.core.urlresolvers import reverse
-from django.http import HttpResponse
-from django.http import HttpResponseForbidden
-from django.http import HttpResponseNotAllowed
 from django.shortcuts import get_object_or_404
 from django.shortcuts import render, redirect
 
@@ -26,15 +21,6 @@
 
 MSGS_PER_PAGE = 20      # message pagination value
 
-# This must match the jQuery UI tab control
-TAB_INDICES = {
-    'inbox': 0,
-    'compose': 1,
-    'outbox': 2,
-    'trash': 3,
-    'options': 4,
-}
-
 
 def _quota_check(box_name, count, request):
     """
@@ -208,10 +194,13 @@
     """
     Process the request to delete the list of PM ids by user.
 
+    Returns the number of PM's deleted.
+
     """
     msgs = Message.objects.filter(id__in=pm_ids)
     now = datetime.datetime.now()
 
+    count = 0
     for msg in msgs:
         if msg.sender == user:
             if msg.receiver_delete_date is not None or msg.read_date is None:
@@ -222,6 +211,7 @@
                 # receiver still has PM in their inbox
                 msg.sender_delete_date = now
                 msg.save()
+            count += 1
         elif msg.receiver == user:
             if msg.sender_delete_date is not None:
                 # both parties deleted the message, we can delete it now
@@ -230,6 +220,9 @@
                 # sender still has PM in their inbox
                 msg.receiver_delete_date = now
                 msg.save()
+            count += 1
+
+    return count
 
 
 @login_required
@@ -242,7 +235,9 @@
     """
     pm_ids = request.POST.getlist('pm_ids')
     if pm_ids:
-        _delete_pms(request.user, pm_ids)
+        count = _delete_pms(request.user, pm_ids)
+        msg = '{} message{} deleted.'.format(count, '' if count == 1 else 's')
+        django_messages.success(request, msg)
 
     # Figure out where to redirect to
     src = request.POST.get('src', 'inbox')
@@ -251,50 +246,52 @@
     except ValueError:
         page = 1
 
-    view_name = 'messages-inbox' if src == inbox else 'messages-outbox'
+    view_name = 'messages-inbox' if src == 'inbox' else 'messages-outbox'
     url = reverse(view_name) + '?page={}'.format(page)
     return redirect(url)
 
 
-def _undelete_msgs(user, msg_ids):
+def _undelete_pms(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.
 
+    Returns the number of PM's undeleted.
+
     """
-    msg_ids = _only_integers(msg_ids)
     msgs = Message.objects.filter(id__in=msg_ids)
+    count = 0
     for msg in msgs:
         if msg.sender == user:
             msg.sender_delete_date = None
             msg.save()
+            count += 1
         elif msg.receiver == user:
             msg.receiver_delete_date = None
             msg.save()
+            count += 1
+    return count
 
 
-def bulk(request):
+@login_required
+@require_POST
+def undelete(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.
+    Undeletes the requested PM's. The user must be either a sender or receiver for
+    this to work.
 
     """
-    if not request.user.is_authenticated():
-        return HttpResponseForbidden()
-    if request.method != 'POST':
-        return HttpResponseNotAllowed(['POST'])
+    pm_ids = request.POST.getlist('pm_ids')
+    if pm_ids:
+        count = _undelete_pms(request.user, pm_ids)
+        msg = '{} message{} undeleted.'.format(count, '' if count == 1 else 's')
+        django_messages.success(request, msg)
 
-    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'))
+    # Figure out where to redirect to
+    try:
+        page = int(request.POST.get('page', '1'))
+    except ValueError:
+        page = 1
 
-    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('');
+    url = reverse('messages-trash') + '?page={}'.format(page)
+    return redirect(url)
--- a/sg101/templates/messages/trash.html	Mon Sep 01 15:53:35 2014 -0500
+++ b/sg101/templates/messages/trash.html	Mon Sep 01 16:22:13 2014 -0500
@@ -1,4 +1,7 @@
 {% extends 'messages/messages_base.html' %}
+{% block custom_js %}
+<script src="{{ STATIC_URL }}js/messages.js"></script>
+{% endblock %}
 {% block messages_content %}
 <h3>Trash <small>(Page {{ page.number }} of {{ page.paginator.num_pages }})</small></h3>
 <p>
@@ -11,14 +14,14 @@
 </p>
 {% if page.object_list %}
    {% include "messages/pagination.html" %}
-   <form action="." onsubmit="return bulkMsgAction(this, 'undelete');">
+   <form action="{% url 'messages-undelete' %}" method="post" onsubmit="return bulkMsgAction('undelete');">{% csrf_token %}
    <table class="messages">
    <tr>
       <th>From</th>
       <th>To</th>
       <th>Subject</th>
       <th>Date</th>
-      <th><input type="checkbox" onclick="tabMasterCheckClick(this, 'trash_ids');" /></th>
+      <th><input type="checkbox" onclick="tabMasterCheckClick(this, 'pm_ids');" /></th>
    </tr>
    {% for msg in page.object_list %}
    <tr>
@@ -31,11 +34,12 @@
             class="{% if msg.unread %}unread {% endif %}{% if msg.replied_to %}replied_to{% endif %}">{{ msg.subject }}</a>
          </td>
       <td>{{ msg.send_date|date:"M j, Y g:i:s A T" }}</td>
-      <td><input type="checkbox" name="trash_ids" value="{{ msg.id }}" /></td>
+      <td><input type="checkbox" name="pm_ids" value="{{ msg.id }}" /></td>
    </tr>
    {% endfor %}
    <tr><td colspan="5"><input type="submit" value="Undelete Checked Messages" /></td></tr>
    </table>
+   <input type="hidden" name="page" value="{{ page.number }}" />
    </form>
    {% include "messages/pagination.html" %}
 {% else %}