changeset 805:e966d5553955

Private message refactor: view & reply to existing PMs.
author Brian Neal <bgneal@gmail.com>
date Sun, 31 Aug 2014 16:01:00 -0500
parents 95b3d59913ad
children 59e36169a814
files messages/static/css/messages.css messages/urls.py messages/views.py sg101/templates/messages/inbox.html sg101/templates/messages/outbox.html sg101/templates/messages/trash.html sg101/templates/messages/view_message.html
diffstat 7 files changed, 94 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/messages/static/css/messages.css	Sun Aug 31 13:51:06 2014 -0500
+++ b/messages/static/css/messages.css	Sun Aug 31 16:01:00 2014 -0500
@@ -74,3 +74,19 @@
 .pagination {
    text-align: right;
 }
+table.pm {
+   border-collapse: collapse;
+   border: solid thin;
+   margin-left: auto;
+   margin-right: auto;
+   width: 100%;
+}
+table.pm th, table.pm td {
+   border: solid thin;
+}
+table.pm th {
+   background-color: #E0F2F6;
+}
+table.pm td {
+   background-color: #EDF7F6;
+}
--- a/messages/urls.py	Sun Aug 31 13:51:06 2014 -0500
+++ b/messages/urls.py	Sun Aug 31 16:01:00 2014 -0500
@@ -27,9 +27,9 @@
 #   url(r'^undelete/$',
 #       'messages.views.undelete',
 #       name='messages-undelete'),
-#   url(r'^view/(\d+)/$',
-#       'messages.views.view',
-#       name='messages-view'),
+    url(r'^view/(\d+)/$',
+        'messages.views.view',
+        name='messages-view'),
 #   url(r'^report/(\d+)/$',
 #       'messages.views.report',
 #       name='messages-report'),
--- a/messages/views.py	Sun Aug 31 13:51:06 2014 -0500
+++ b/messages/views.py	Sun Aug 31 16:01:00 2014 -0500
@@ -14,7 +14,7 @@
 from django.http import HttpResponseForbidden
 from django.http import HttpResponseNotAllowed
 from django.shortcuts import get_object_or_404
-from django.shortcuts import render
+from django.shortcuts import render, redirect
 
 from messages.models import Message, Options
 from messages.forms import OptionsForm, ComposeForm
@@ -164,32 +164,43 @@
 @login_required
 def view(request, msg_id):
     """
-    This view function retrieves a message and returns it as a JSON object.
+    This view function displays a private message for reading to the user. If
+    the user is a recipient of the message, a reply can be composed and sent.
 
     """
-    if not request.user.is_authenticated():
-        return HttpResponseForbidden()
-    if request.method != 'POST':
-        return HttpResponseNotAllowed(['POST'])
+    if request.method == 'POST':
+        form = ComposeForm(request.user, request.POST)
+        if form.is_valid():
+            form.save()
+            django_messages.success(request, 'Reply sent.')
+            return redirect('messages-inbox')
+    else:
+        msg = get_object_or_404(Message.objects.select_related(), pk=msg_id)
+        if msg.sender != request.user and msg.receiver != request.user:
+            django_messages.error(request,
+                    "You don't have permission to read that message.")
+            return redirect('messages-inbox')
 
-    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()
+        initial_data = {
+            'receiver': msg.sender.username,
+            'subject': reply_subject(msg.subject),
+            'message': quote_message(msg.sender.username, msg.message),
+            'parent_id': msg.pk,
+        }
 
-    if msg.receiver == request.user and msg.read_date is None:
-        msg.read_date = datetime.datetime.now()
-        msg.save()
+        if msg.receiver == request.user:
+            if msg.read_date is None:
+                msg.read_date = datetime.datetime.now()
+                msg.save()
+        else:
+            initial_data['receiver'] = msg.receiver.username
 
-    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.message))
+        form = ComposeForm(request.user, initial=initial_data)
 
-    result = json.dumps(msg_dict, ensure_ascii=False)
-    return HttpResponse(result, content_type='application/json')
+    return render(request, 'messages/view_message.html', {
+        'msg': msg,
+        'form': form,
+        })
 
 
 def _only_integers(slist):
--- a/sg101/templates/messages/inbox.html	Sun Aug 31 13:51:06 2014 -0500
+++ b/sg101/templates/messages/inbox.html	Sun Aug 31 16:01:00 2014 -0500
@@ -20,7 +20,7 @@
       <td><a href="{% url 'bio.views.view_profile' msg.sender.username %}">
          {{ msg.sender.username }}</a></td>
       <td>
-         <a href="#" onclick="msgShow(this, {{ msg.id }}); return false;"
+         <a href="{% url 'messages-view' msg.pk %}"
             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 A" }}</td>
--- a/sg101/templates/messages/outbox.html	Sun Aug 31 13:51:06 2014 -0500
+++ b/sg101/templates/messages/outbox.html	Sun Aug 31 16:01:00 2014 -0500
@@ -21,7 +21,7 @@
       <td><a href="{% url 'bio.views.view_profile' msg.receiver.username %}">
          {{ msg.receiver.username }}</a></td>
       <td>
-         <a href="#" onclick="msgShow(this, {{ msg.id }}); return false;"
+         <a href="{% url 'messages-view' msg.pk %}"
             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 A" }}</td>
--- a/sg101/templates/messages/trash.html	Sun Aug 31 13:51:06 2014 -0500
+++ b/sg101/templates/messages/trash.html	Sun Aug 31 16:01:00 2014 -0500
@@ -27,7 +27,7 @@
       <td><a href="{% url 'bio.views.view_profile' msg.receiver.username %}">
          {{ msg.receiver.username }}</a></td>
       <td>
-         <a href="#" onclick="msgShow(this, {{ msg.id }}); return false;"
+         <a href="{% url 'messages-view' msg.pk %}"
             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>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sg101/templates/messages/view_message.html	Sun Aug 31 16:01:00 2014 -0500
@@ -0,0 +1,40 @@
+{% extends 'messages/messages_base.html' %}
+{% load bio_tags %}
+{% load core_tags %}
+{% load script_tags %}
+{% block custom_js %}
+   {% if form %}
+      {% script_tags 'jquery-ui' %}
+      {% script_tags 'markitup' %}
+   {% endif %}
+{% endblock %}
+{% block messages_content %}
+<h3>View Private Message</h3>
+<table class="pm">
+   <tr><th>From:</th><td>{% profile_link msg.sender.username %}</td></tr>
+   <tr><th>To:</th><td>{% profile_link msg.receiver.username %}</td></tr>
+   <tr><th>Send Date:</th><td>{{ msg.send_date }}</td></tr>
+   <tr><th>Read Date:</th><td>{{ msg.read_date }}</td></tr>
+   <tr><th>Reply Date:</th><td>{{ msg.reply_date }}</td></tr>
+   <tr><th>Subject:</th><td>{{ msg.subject }}</td></tr>
+   <tr><td colspan="2">{{ msg.html|safe }}</td></tr>
+</table>
+
+{% if form %}
+<form action="." method="post" id="msg_compose_form">{% csrf_token %}
+<fieldset>
+<legend>Compose Reply</legend>
+<table>
+{{ form.as_table }}
+<tr>
+   <td>&nbsp;</td>
+   <td>
+      {% comment_dialogs %}
+      <input type="submit" name="submit_button" value="Send Reply" />
+   </td>
+</tr>
+</table>
+</fieldset>
+</form>
+{% endif %}
+{% endblock %}