Mercurial > public > sg101
comparison messages/views.py @ 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 |
comparison
equal
deleted
inserted
replaced
804:95b3d59913ad | 805:e966d5553955 |
---|---|
12 from django.core.urlresolvers import reverse | 12 from django.core.urlresolvers import reverse |
13 from django.http import HttpResponse | 13 from django.http import HttpResponse |
14 from django.http import HttpResponseForbidden | 14 from django.http import HttpResponseForbidden |
15 from django.http import HttpResponseNotAllowed | 15 from django.http import HttpResponseNotAllowed |
16 from django.shortcuts import get_object_or_404 | 16 from django.shortcuts import get_object_or_404 |
17 from django.shortcuts import render | 17 from django.shortcuts import render, redirect |
18 | 18 |
19 from messages.models import Message, Options | 19 from messages.models import Message, Options |
20 from messages.forms import OptionsForm, ComposeForm | 20 from messages.forms import OptionsForm, ComposeForm |
21 from messages.utils import reply_subject | 21 from messages.utils import reply_subject |
22 from messages import MSG_BOX_LIMIT | 22 from messages import MSG_BOX_LIMIT |
162 | 162 |
163 | 163 |
164 @login_required | 164 @login_required |
165 def view(request, msg_id): | 165 def view(request, msg_id): |
166 """ | 166 """ |
167 This view function retrieves a message and returns it as a JSON object. | 167 This view function displays a private message for reading to the user. If |
168 | 168 the user is a recipient of the message, a reply can be composed and sent. |
169 """ | 169 |
170 if not request.user.is_authenticated(): | 170 """ |
171 return HttpResponseForbidden() | 171 if request.method == 'POST': |
172 if request.method != 'POST': | 172 form = ComposeForm(request.user, request.POST) |
173 return HttpResponseNotAllowed(['POST']) | 173 if form.is_valid(): |
174 | 174 form.save() |
175 msg_id = request.POST.get('msg_id') | 175 django_messages.success(request, 'Reply sent.') |
176 msg = get_object_or_404(Message.objects.select_related(), pk=msg_id) | 176 return redirect('messages-inbox') |
177 if msg.sender != request.user and msg.receiver != request.user: | 177 else: |
178 return HttpResponseForbidden() | 178 msg = get_object_or_404(Message.objects.select_related(), pk=msg_id) |
179 | 179 if msg.sender != request.user and msg.receiver != request.user: |
180 if msg.receiver == request.user and msg.read_date is None: | 180 django_messages.error(request, |
181 msg.read_date = datetime.datetime.now() | 181 "You don't have permission to read that message.") |
182 msg.save() | 182 return redirect('messages-inbox') |
183 | 183 |
184 msg_dict = dict(subject=msg.subject, | 184 initial_data = { |
185 sender=msg.sender.username, | 185 'receiver': msg.sender.username, |
186 receiver=msg.receiver.username, | 186 'subject': reply_subject(msg.subject), |
187 content=msg.html, | 187 'message': quote_message(msg.sender.username, msg.message), |
188 re_subject=reply_subject(msg.subject), | 188 'parent_id': msg.pk, |
189 re_content=quote_message(msg.sender.username, msg.message)) | 189 } |
190 | 190 |
191 result = json.dumps(msg_dict, ensure_ascii=False) | 191 if msg.receiver == request.user: |
192 return HttpResponse(result, content_type='application/json') | 192 if msg.read_date is None: |
193 msg.read_date = datetime.datetime.now() | |
194 msg.save() | |
195 else: | |
196 initial_data['receiver'] = msg.receiver.username | |
197 | |
198 form = ComposeForm(request.user, initial=initial_data) | |
199 | |
200 return render(request, 'messages/view_message.html', { | |
201 'msg': msg, | |
202 'form': form, | |
203 }) | |
193 | 204 |
194 | 205 |
195 def _only_integers(slist): | 206 def _only_integers(slist): |
196 """ | 207 """ |
197 Accepts a list of strings. Returns a list of integers consisting of only | 208 Accepts a list of strings. Returns a list of integers consisting of only |