# HG changeset patch # User Brian Neal # Date 1409511066 18000 # Node ID 95b3d59913ad3a37f46777a1b8be8b28c73ae28d # Parent b3eeaefc39a8d5efe882563cf9a728a70a2211a7 Private messages refactoring: compose functionality. diff -r b3eeaefc39a8 -r 95b3d59913ad messages/forms.py --- 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) diff -r b3eeaefc39a8 -r 95b3d59913ad messages/urls.py --- 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'), diff -r b3eeaefc39a8 -r 95b3d59913ad messages/views.py --- 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 diff -r b3eeaefc39a8 -r 95b3d59913ad sg101/templates/messages/compose.html --- /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 %} +

Compose Private Message

+

Use this form to send a new private message to another user.

+
{% csrf_token %} + +{{ compose_form.as_table }} + + + + +
  + {% comment_dialogs %} + +
+
+{% endblock %} diff -r b3eeaefc39a8 -r 95b3d59913ad sg101/templates/messages/compose_tab.html --- 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 %} - -{% endif %} -
- -{{ compose_form.as_table }} - - - - -
  - {% comment_dialogs %} - -
-
diff -r b3eeaefc39a8 -r 95b3d59913ad sg101/templates/messages/messages_base.html --- 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 @@