diff gpp/messages/views.py @ 436:241c80ff16c5

For #211, added message quotas; can't send or receive private messages if your outbox/inbox quota has been exceeded.
author Brian Neal <bgneal@gmail.com>
date Fri, 13 May 2011 02:06:53 +0000
parents 0d91176cf9b3
children 33d0c55e57a9
line wrap: on
line diff
--- a/gpp/messages/views.py	Fri May 13 00:12:53 2011 +0000
+++ b/gpp/messages/views.py	Fri May 13 02:06:53 2011 +0000
@@ -6,7 +6,7 @@
 
 from django.contrib.auth.decorators import login_required
 from django.contrib.auth.models import User
-from django.contrib import messages
+from django.contrib import messages as django_messages
 from django.core.paginator import Paginator, EmptyPage, InvalidPage
 from django.core.urlresolvers import reverse
 from django.http import HttpResponse
@@ -20,10 +20,12 @@
 from messages.models import Message, Options
 from messages.forms import OptionsForm, ComposeForm
 from messages.utils import reply_subject, quote_message
+from messages import MSG_BOX_LIMIT
 
 
-MSGS_PER_PAGE = 20
+MSGS_PER_PAGE = 20      # message pagination value
 
+# This must match the jQuery UI tab control
 TAB_INDICES = {
     'inbox': 0,
     'compose': 1,
@@ -41,6 +43,17 @@
     return n
 
 
+def _quota_check(box_name, count, request):
+    """
+    Checks the message box count against MSG_BOX_LIMIT.
+    Emits a message to the user if the quota is exceeded.
+    
+    """
+    if count >= MSG_BOX_LIMIT:
+        django_messages.warning(request,
+            "Your %s is full. Please delete some messages." % box_name)
+
+
 @login_required
 def index(request, tab=None):
     """
@@ -80,6 +93,8 @@
         return HttpResponseForbidden()
 
     msg_list = Message.objects.inbox(request.user)
+    _quota_check('inbox', msg_list.count(), request)
+
     paginator = Paginator(msg_list, MSGS_PER_PAGE)
     try:
         msgs = paginator.page(_get_page(request))
@@ -101,6 +116,8 @@
         return HttpResponseForbidden()
 
     msg_list = Message.objects.outbox(request.user)
+    _quota_check('outbox', msg_list.count(), request)
+
     paginator = Paginator(msg_list, MSGS_PER_PAGE)
     try:
         msgs = paginator.page(_get_page(request))
@@ -178,7 +195,7 @@
         form = OptionsForm(request.POST, instance=options, prefix='opts')
         if form.is_valid():
             form.save()
-            messages.success(request, 'Options saved.')
+            django_messages.success(request, 'Options saved.')
     else:
         options = Options.objects.for_user(request.user)
         form = OptionsForm(instance=options, prefix='opts')
@@ -211,7 +228,7 @@
 
         if compose_form.is_valid():
             compose_form.save(parent_msg=parent_msg)
-            messages.success(request, 'Message sent.')
+            django_messages.success(request, 'Message sent.')
             compose_form = ComposeForm(request.user)
     else:
         if receiver is not None:
@@ -220,6 +237,8 @@
         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,
         })