changeset 804:95b3d59913ad

Private messages refactoring: compose functionality.
author Brian Neal <bgneal@gmail.com>
date Sun, 31 Aug 2014 13:51:06 -0500
parents b3eeaefc39a8
children e966d5553955
files messages/forms.py messages/urls.py messages/views.py sg101/templates/messages/compose.html sg101/templates/messages/compose_tab.html sg101/templates/messages/messages_base.html
diffstat 6 files changed, 64 insertions(+), 81 deletions(-) [+]
line wrap: on
line diff
--- a/messages/forms.py	Sun Aug 31 12:46:21 2014 -0500
+++ b/messages/forms.py	Sun Aug 31 13:51:06 2014 -0500
@@ -1,7 +1,7 @@
 """
 Forms for the messages application.
+
 """
-
 from django import forms
 from django.contrib.auth.models import User
 from django.conf import settings
@@ -30,6 +30,7 @@
     subject = forms.CharField(max_length=120, widget=forms.TextInput(attrs={'size': 52}))
     message = forms.CharField(widget=forms.Textarea(attrs={'class': 'markItUp smileyTarget'}))
     attach_signature = forms.BooleanField(label='Attach Signature?', required=False)
+    parent_id = forms.IntegerField(required=False, widget=forms.HiddenInput)
 
     def __init__(self, user, *args, **kwargs):
         forms.Form.__init__(self, *args, **kwargs)
@@ -90,10 +91,20 @@
             signature_attached=attach_signature,
         )
         new_msg.save()
-        if parent_msg is not None:
-            parent_msg.reply_date = new_msg.send_date
-            parent_msg.save()
 
+        # Update the parent message (if there is one)
+        parent_id = self.cleaned_data['parent_id']
+        if parent_id:
+            try:
+                parent_msg = Message.objects.get(pk=parent_id)
+            except Message.DoesNotExist:
+                parent_msg = None
+
+            if parent_msg and parent_msg.receiver == self.user:
+                parent_msg.reply_date = new_msg.send_date
+                parent_msg.save()
+
+        # Notify recipient
         receiver_opts = Options.objects.for_user(receiver)
         if receiver_opts.notify_email:
             notify_receiver(new_msg)
--- a/messages/urls.py	Sun Aug 31 12:46:21 2014 -0500
+++ b/messages/urls.py	Sun Aug 31 13:51:06 2014 -0500
@@ -9,9 +9,9 @@
     url(r'^inbox/$',
         'messages.views.inbox',
         name='messages-inbox'),
-#   url(r'^compose/$',
-#       'messages.views.compose',
-#       name='messages-compose'),
+    url(r'^compose/$',
+        'messages.views.compose',
+        name='messages-compose'),
     url(r'^outbox/$',
         'messages.views.outbox',
         name='messages-outbox'),
--- a/messages/views.py	Sun Aug 31 12:46:21 2014 -0500
+++ b/messages/views.py	Sun Aug 31 13:51:06 2014 -0500
@@ -134,23 +134,35 @@
 
 
 @login_required
-def compose_to(request, receiver):
+def compose(request):
     """
-    This function displays the base tabbed private messages view,
-    and configures it to display the compose PM tab for the given
-    receiver.
+    Process or prepare the compose form to create a new private message.
 
     """
-    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),
+    if request.method == 'POST':
+        compose_form = ComposeForm(request.user, request.POST)
+        if compose_form.is_valid():
+            compose_form.save()
+            django_messages.success(request, 'Message sent.')
+            compose_form = ComposeForm(request.user)
+    else:
+        receiver = request.GET.get('to')
+        if receiver:
+            form_data = {'receiver': receiver}
+            compose_form = ComposeForm(request.user, initial=form_data)
+        else:
+            compose_form = ComposeForm(request.user)
+
+        _quota_check('outbox', Message.objects.outbox(request.user).count(), request)
+
+    return render(request, 'messages/compose.html', {
+        'tab': 'compose',
+        'compose_form': compose_form,
         })
 
 
-def message(request):
+@login_required
+def view(request, msg_id):
     """
     This view function retrieves a message and returns it as a JSON object.
 
@@ -180,45 +192,6 @@
     return HttpResponse(result, content_type='application/json')
 
 
-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)
-
-        # Is this a reply to another message?
-        parent_msg_id = request.POST.get('reply_to')
-        if parent_msg_id:
-            parent_msg = get_object_or_404(Message, id=parent_msg_id)
-            if (request.user != parent_msg.receiver and
-                request.user != parent_msg.sender):
-                return HttpResponseForbidden()
-        else:
-            parent_msg = None
-
-        if compose_form.is_valid():
-            compose_form.save(parent_msg=parent_msg)
-            django_messages.success(request, 'Message sent.')
-            compose_form = ComposeForm(request.user)
-    else:
-        if receiver is not None:
-            form_data = {'receiver': receiver}
-            compose_form = ComposeForm(request.user, initial=form_data)
-        else:
-            compose_form = ComposeForm(request.user)
-
-        _quota_check('outbox', Message.objects.outbox(request.user).count(), request)
-
-    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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sg101/templates/messages/compose.html	Sun Aug 31 13:51:06 2014 -0500
@@ -0,0 +1,23 @@
+{% extends 'messages/messages_base.html' %}
+{% load core_tags %}
+{% load script_tags %}
+{% block custom_js %}
+{% script_tags 'jquery-ui' %}
+{% script_tags 'markitup' %}
+{% endblock %}
+{% block messages_content %}
+<h3>Compose Private Message</h3>
+<p>Use this form to send a new private message to another user.</p>
+<form action="." method="post" id="msg_compose_form">{% csrf_token %}
+<table>
+{{ compose_form.as_table }}
+<tr>
+   <td>&nbsp;</td>
+   <td>
+      {% comment_dialogs %}
+      <input type="submit" name="submit_button" value="Send" />
+   </td>
+</tr>
+</table>
+</form>
+{% endblock %}
--- a/sg101/templates/messages/compose_tab.html	Sun Aug 31 12:46:21 2014 -0500
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,22 +0,0 @@
-{% load core_tags %}
-{% load script_tags %}
-{% script_tags 'markitup' %}
-{% if messages %}
-<ul class="user-messages">
-   {% for msg in messages %}
-      <li{% if msg.tags %} class="{{ msg.tags }}"{% endif %}>{{ msg }}</li>
-   {% endfor %}
-</ul>
-{% endif %}
-<form action="." method="post" id="msg_compose_form" onsubmit="return messageSubmit(this);" >
-<table>
-{{ compose_form.as_table }}
-<tr>
-   <td>&nbsp;</td>
-   <td>
-      {% comment_dialogs %}
-      <input type="submit" name="submit_button" value="Send" />
-   </td>
-</tr>
-</table>
-</form>
--- a/sg101/templates/messages/messages_base.html	Sun Aug 31 12:46:21 2014 -0500
+++ b/sg101/templates/messages/messages_base.html	Sun Aug 31 13:51:06 2014 -0500
@@ -18,9 +18,7 @@
 <ul>
    <li>{% if tab != 'inbox' %}<a href="{% url 'messages-inbox' %}">{% endif %}Inbox{% if tab != 'inbox' %}</a>{% endif %}
       {% if inbox_pct %}({{ inbox_pct }}% used){% endif %}</li>
-{% comment %}
    <li>{% if tab != 'compose' %}<a href="{% url 'messages-compose' %}">{% endif %}Compose new message{% if tab != 'compose' %}</a>{% endif %}</li>
-{% endcomment %}
    <li>{% if tab != 'outbox' %}<a href="{% url 'messages-outbox' %}">{% endif %}Outbox{% if tab != 'outbox' %}</a>{% endif %}
       {% if outbox_pct %}({{ outbox_pct }}% used){% endif %}</li>
    <li>{% if tab != 'trash' %}<a href="{% url 'messages-trash' %}">{% endif %}Trash{% if tab != 'trash' %}</a>{% endif %}</li>