annotate messages/views.py @ 802:dbc389a409f5

Work in progress: rework private messages. Add trash view.
author Brian Neal <bgneal@gmail.com>
date Sat, 30 Aug 2014 19:11:32 -0500
parents 6bbd1473d48e
children b3eeaefc39a8
rev   line source
bgneal@425 1 """
bgneal@425 2 Views for the messages application.
bgneal@425 3
bgneal@425 4 """
bgneal@425 5 import datetime
bgneal@679 6 import json
bgneal@425 7
bgneal@425 8 from django.contrib.auth.decorators import login_required
bgneal@428 9 from django.contrib.auth.models import User
bgneal@436 10 from django.contrib import messages as django_messages
bgneal@801 11 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
bgneal@425 12 from django.core.urlresolvers import reverse
bgneal@425 13 from django.http import HttpResponse
bgneal@425 14 from django.http import HttpResponseForbidden
bgneal@425 15 from django.http import HttpResponseNotAllowed
bgneal@425 16 from django.shortcuts import get_object_or_404
bgneal@425 17 from django.shortcuts import render
bgneal@425 18
bgneal@425 19 from messages.models import Message, Options
bgneal@425 20 from messages.forms import OptionsForm, ComposeForm
bgneal@566 21 from messages.utils import reply_subject
bgneal@436 22 from messages import MSG_BOX_LIMIT
bgneal@566 23 from core.functions import quote_message
bgneal@425 24
bgneal@425 25
bgneal@436 26 MSGS_PER_PAGE = 20 # message pagination value
bgneal@425 27
bgneal@436 28 # This must match the jQuery UI tab control
bgneal@425 29 TAB_INDICES = {
bgneal@425 30 'inbox': 0,
bgneal@425 31 'compose': 1,
bgneal@425 32 'outbox': 2,
bgneal@425 33 'trash': 3,
bgneal@425 34 'options': 4,
bgneal@425 35 }
bgneal@425 36
bgneal@425 37
bgneal@436 38 def _quota_check(box_name, count, request):
bgneal@436 39 """
bgneal@436 40 Checks the message box count against MSG_BOX_LIMIT.
bgneal@436 41 Emits a message to the user if the quota is exceeded.
bgneal@441 42
bgneal@568 43 Returns the percent used as an integer between 0-100.
bgneal@568 44
bgneal@436 45 """
bgneal@436 46 if count >= MSG_BOX_LIMIT:
bgneal@436 47 django_messages.warning(request,
bgneal@436 48 "Your %s is full. Please delete some messages." % box_name)
bgneal@436 49
bgneal@568 50 return 100 * count / MSG_BOX_LIMIT
bgneal@568 51
bgneal@436 52
bgneal@801 53 def _get_page(request, qs):
bgneal@801 54 """Paginates the given queryset and returns a page object"""
bgneal@801 55 paginator = Paginator(qs, MSGS_PER_PAGE)
bgneal@801 56 try:
bgneal@801 57 page = paginator.page(request.GET.get('page', '1'))
bgneal@801 58 except PageNotAnInteger:
bgneal@801 59 page = paginator.page(1)
bgneal@801 60 except EmptyPage:
bgneal@801 61 page = paginator.page(paginator.num_pages)
bgneal@801 62 return page
bgneal@801 63
bgneal@801 64
bgneal@425 65 @login_required
bgneal@801 66 def inbox(request):
bgneal@425 67
bgneal@801 68 msg_list = Message.objects.inbox(request.user)
bgneal@801 69 msg_count = msg_list.count()
bgneal@801 70 pct_used = _quota_check('inbox', msg_count, request)
bgneal@801 71
bgneal@801 72 page = _get_page(request, msg_list)
bgneal@801 73
bgneal@801 74 return render(request, 'messages/inbox.html', {
bgneal@801 75 'tab': 'inbox',
bgneal@801 76 'page': page,
bgneal@801 77 'inbox_pct': pct_used,
bgneal@801 78 'outbox_pct': None,
bgneal@801 79 })
bgneal@801 80
bgneal@801 81
bgneal@801 82 @login_required
bgneal@801 83 def outbox(request):
bgneal@801 84
bgneal@801 85 msg_list = Message.objects.outbox(request.user)
bgneal@801 86 msg_count = msg_list.count()
bgneal@801 87 pct_used = _quota_check('outbox', msg_count, request)
bgneal@801 88
bgneal@801 89 page = _get_page(request, msg_list)
bgneal@801 90
bgneal@801 91 return render(request, 'messages/outbox.html', {
bgneal@801 92 'tab': 'outbox',
bgneal@801 93 'page': page,
bgneal@801 94 'inbox_pct': None,
bgneal@801 95 'outbox_pct': pct_used,
bgneal@425 96 })
bgneal@425 97
bgneal@425 98
bgneal@428 99 @login_required
bgneal@802 100 def trash(request):
bgneal@802 101
bgneal@802 102 msg_list = Message.objects.trash(request.user)
bgneal@802 103
bgneal@802 104 page = _get_page(request, msg_list)
bgneal@802 105
bgneal@802 106 return render(request, 'messages/trash.html', {
bgneal@802 107 'tab': 'trash',
bgneal@802 108 'page': page,
bgneal@802 109 'inbox_pct': None,
bgneal@802 110 'outbox_pct': None,
bgneal@802 111 })
bgneal@802 112
bgneal@802 113
bgneal@802 114 @login_required
bgneal@428 115 def compose_to(request, receiver):
bgneal@428 116 """
bgneal@428 117 This function displays the base tabbed private messages view,
bgneal@428 118 and configures it to display the compose PM tab for the given
bgneal@428 119 receiver.
bgneal@428 120
bgneal@428 121 """
bgneal@428 122 user = get_object_or_404(User, username=receiver)
bgneal@428 123 tab_index = TAB_INDICES['compose']
bgneal@428 124 return render(request, 'messages/tabbed_base.html', {
bgneal@428 125 'tab': tab_index,
bgneal@428 126 'receiver': receiver,
bgneal@429 127 'unread_count': Message.objects.unread_count(request.user),
bgneal@428 128 })
bgneal@428 129
bgneal@428 130
bgneal@425 131 def message(request):
bgneal@425 132 """
bgneal@425 133 This view function retrieves a message and returns it as a JSON object.
bgneal@425 134
bgneal@425 135 """
bgneal@425 136 if not request.user.is_authenticated():
bgneal@425 137 return HttpResponseForbidden()
bgneal@425 138 if request.method != 'POST':
bgneal@425 139 return HttpResponseNotAllowed(['POST'])
bgneal@425 140
bgneal@425 141 msg_id = request.POST.get('msg_id')
bgneal@425 142 msg = get_object_or_404(Message.objects.select_related(), pk=msg_id)
bgneal@425 143 if msg.sender != request.user and msg.receiver != request.user:
bgneal@425 144 return HttpResponseForbidden()
bgneal@425 145
bgneal@425 146 if msg.receiver == request.user and msg.read_date is None:
bgneal@425 147 msg.read_date = datetime.datetime.now()
bgneal@425 148 msg.save()
bgneal@425 149
bgneal@425 150 msg_dict = dict(subject=msg.subject,
bgneal@425 151 sender=msg.sender.username,
bgneal@425 152 receiver=msg.receiver.username,
bgneal@425 153 content=msg.html,
bgneal@425 154 re_subject=reply_subject(msg.subject),
bgneal@566 155 re_content=quote_message(msg.sender.username, msg.message))
bgneal@425 156
bgneal@425 157 result = json.dumps(msg_dict, ensure_ascii=False)
bgneal@425 158 return HttpResponse(result, content_type='application/json')
bgneal@425 159
bgneal@425 160
bgneal@425 161 def options(request):
bgneal@425 162 """
bgneal@425 163 This view handles the displaying and changing of private message options.
bgneal@425 164
bgneal@425 165 """
bgneal@425 166 if not request.user.is_authenticated():
bgneal@425 167 return HttpResponseForbidden()
bgneal@425 168
bgneal@425 169 if request.method == "POST":
bgneal@425 170 options = Options.objects.for_user(request.user)
bgneal@425 171 form = OptionsForm(request.POST, instance=options, prefix='opts')
bgneal@425 172 if form.is_valid():
bgneal@425 173 form.save()
bgneal@436 174 django_messages.success(request, 'Options saved.')
bgneal@425 175 else:
bgneal@425 176 options = Options.objects.for_user(request.user)
bgneal@425 177 form = OptionsForm(instance=options, prefix='opts')
bgneal@425 178
bgneal@425 179 return render(request, 'messages/options_tab.html', {
bgneal@425 180 'form': form,
bgneal@425 181 })
bgneal@425 182
bgneal@425 183
bgneal@425 184 def compose(request, receiver=None):
bgneal@425 185 """
bgneal@425 186 Process or prepare the compose form to create a new private message.
bgneal@425 187
bgneal@425 188 """
bgneal@425 189 if not request.user.is_authenticated():
bgneal@425 190 return HttpResponseForbidden()
bgneal@425 191
bgneal@425 192 if request.method == "POST":
bgneal@425 193 compose_form = ComposeForm(request.user, request.POST)
bgneal@430 194
bgneal@430 195 # Is this a reply to another message?
bgneal@430 196 parent_msg_id = request.POST.get('reply_to')
bgneal@430 197 if parent_msg_id:
bgneal@430 198 parent_msg = get_object_or_404(Message, id=parent_msg_id)
bgneal@430 199 if (request.user != parent_msg.receiver and
bgneal@430 200 request.user != parent_msg.sender):
bgneal@430 201 return HttpResponseForbidden()
bgneal@430 202 else:
bgneal@430 203 parent_msg = None
bgneal@430 204
bgneal@425 205 if compose_form.is_valid():
bgneal@430 206 compose_form.save(parent_msg=parent_msg)
bgneal@436 207 django_messages.success(request, 'Message sent.')
bgneal@431 208 compose_form = ComposeForm(request.user)
bgneal@425 209 else:
bgneal@425 210 if receiver is not None:
bgneal@425 211 form_data = {'receiver': receiver}
bgneal@425 212 compose_form = ComposeForm(request.user, initial=form_data)
bgneal@425 213 else:
bgneal@425 214 compose_form = ComposeForm(request.user)
bgneal@425 215
bgneal@436 216 _quota_check('outbox', Message.objects.outbox(request.user).count(), request)
bgneal@436 217
bgneal@425 218 return render(request, 'messages/compose_tab.html', {
bgneal@425 219 'compose_form': compose_form,
bgneal@425 220 })
bgneal@425 221
bgneal@425 222
bgneal@425 223 def _only_integers(slist):
bgneal@425 224 """
bgneal@425 225 Accepts a list of strings. Returns a list of integers consisting of only
bgneal@425 226 those elements from the original list that could be converted to integers
bgneal@425 227
bgneal@425 228 """
bgneal@425 229 result = []
bgneal@425 230 for s in slist:
bgneal@425 231 try:
bgneal@425 232 n = int(s)
bgneal@425 233 except ValueError:
bgneal@425 234 pass
bgneal@425 235 else:
bgneal@425 236 result.append(n)
bgneal@425 237 return result
bgneal@425 238
bgneal@425 239
bgneal@425 240 def _delete_msgs(user, msg_ids):
bgneal@425 241 """
bgneal@425 242 Deletes the messages given by the list of msg_ids. For this to succeed, the
bgneal@425 243 user has to be either the sender or receiver on each message.
bgneal@425 244
bgneal@425 245 """
bgneal@425 246 msg_ids = _only_integers(msg_ids)
bgneal@425 247 msgs = Message.objects.filter(id__in=msg_ids)
bgneal@425 248
bgneal@425 249 for msg in msgs:
bgneal@425 250 if msg.sender == user:
bgneal@425 251 if (msg.receiver_delete_date is not None or
bgneal@425 252 msg.read_date is None):
bgneal@425 253 # Both parties deleted the message or receiver hasn't read it
bgneal@425 254 # yet, we can delete it now
bgneal@425 255 msg.delete()
bgneal@425 256 else:
bgneal@425 257 # receiver still has it in inbox
bgneal@425 258 msg.sender_delete_date = datetime.datetime.now()
bgneal@425 259 msg.save()
bgneal@425 260
bgneal@425 261 elif msg.receiver == user:
bgneal@425 262 if msg.sender_delete_date is not None:
bgneal@425 263 # both parties deleted the message, we can delete it now
bgneal@425 264 msg.delete()
bgneal@425 265 else:
bgneal@425 266 # sender still has it in the outbox
bgneal@425 267 msg.receiver_delete_date = datetime.datetime.now()
bgneal@425 268 msg.save()
bgneal@425 269
bgneal@425 270
bgneal@425 271 def _undelete_msgs(user, msg_ids):
bgneal@425 272 """
bgneal@425 273 Attempts to "undelete" the messages given by the msg_ids list.
bgneal@425 274 This will only succeed if the user is either the sender or receiver.
bgneal@425 275
bgneal@425 276 """
bgneal@425 277 msg_ids = _only_integers(msg_ids)
bgneal@425 278 msgs = Message.objects.filter(id__in=msg_ids)
bgneal@425 279 for msg in msgs:
bgneal@425 280 if msg.sender == user:
bgneal@425 281 msg.sender_delete_date = None
bgneal@425 282 msg.save()
bgneal@425 283 elif msg.receiver == user:
bgneal@425 284 msg.receiver_delete_date = None
bgneal@425 285 msg.save()
bgneal@425 286
bgneal@425 287
bgneal@425 288 def bulk(request):
bgneal@425 289 """
bgneal@425 290 This view processes messages in bulk. Arrays of message ids are expected in
bgneal@425 291 the POST query dict: inbox_ids and outbox_ids will be deleted; trash_ids will
bgneal@425 292 be undeleted.
bgneal@425 293
bgneal@425 294 """
bgneal@425 295 if not request.user.is_authenticated():
bgneal@425 296 return HttpResponseForbidden()
bgneal@425 297 if request.method != 'POST':
bgneal@425 298 return HttpResponseNotAllowed(['POST'])
bgneal@425 299
bgneal@425 300 delete_ids = []
bgneal@425 301 if 'inbox_ids' in request.POST:
bgneal@425 302 delete_ids.extend(request.POST.getlist('inbox_ids'))
bgneal@425 303 if 'outbox_ids' in request.POST:
bgneal@425 304 delete_ids.extend(request.POST.getlist('outbox_ids'))
bgneal@425 305
bgneal@425 306 if len(delete_ids):
bgneal@425 307 _delete_msgs(request.user, delete_ids)
bgneal@425 308
bgneal@425 309 if 'trash_ids' in request.POST:
bgneal@425 310 _undelete_msgs(request.user, request.POST.getlist('trash_ids'))
bgneal@425 311
bgneal@425 312 return HttpResponse('');