# HG changeset patch # User Brian Neal # Date 1409518860 18000 # Node ID e966d55539551398b585d904ad46e58f64e46f4b # Parent 95b3d59913ad3a37f46777a1b8be8b28c73ae28d Private message refactor: view & reply to existing PMs. diff -r 95b3d59913ad -r e966d5553955 messages/static/css/messages.css --- 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; +} diff -r 95b3d59913ad -r e966d5553955 messages/urls.py --- 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'), diff -r 95b3d59913ad -r e966d5553955 messages/views.py --- 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): diff -r 95b3d59913ad -r e966d5553955 sg101/templates/messages/inbox.html --- 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 @@ {{ msg.sender.username }} - {{ msg.subject }} {{ msg.send_date|date:"M j, Y g:i A" }} diff -r 95b3d59913ad -r e966d5553955 sg101/templates/messages/outbox.html --- 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 @@ {{ msg.receiver.username }} - {{ msg.subject }} {{ msg.send_date|date:"M j, Y g:i A" }} diff -r 95b3d59913ad -r e966d5553955 sg101/templates/messages/trash.html --- 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 @@ {{ msg.receiver.username }} - {{ msg.subject }} {{ msg.send_date|date:"M j, Y g:i:s A T" }} diff -r 95b3d59913ad -r e966d5553955 sg101/templates/messages/view_message.html --- /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 %} +

View Private Message

+ + + + + + + + +
From:{% profile_link msg.sender.username %}
To:{% profile_link msg.receiver.username %}
Send Date:{{ msg.send_date }}
Read Date:{{ msg.read_date }}
Reply Date:{{ msg.reply_date }}
Subject:{{ msg.subject }}
{{ msg.html|safe }}
+ +{% if form %} +
{% csrf_token %} +
+Compose Reply + +{{ form.as_table }} + + + + +
  + {% comment_dialogs %} + +
+
+
+{% endif %} +{% endblock %}