changeset 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 b2f02766cc72
children a8ac4dd3bf03
files gpp/messages/__init__.py gpp/messages/forms.py gpp/messages/views.py gpp/settings.py
diffstat 4 files changed, 49 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/gpp/messages/__init__.py	Fri May 13 00:12:53 2011 +0000
+++ b/gpp/messages/__init__.py	Fri May 13 02:06:53 2011 +0000
@@ -0,0 +1,1 @@
+MSG_BOX_LIMIT = 30      # hard limit on # of msgs per box
--- a/gpp/messages/forms.py	Fri May 13 00:12:53 2011 +0000
+++ b/gpp/messages/forms.py	Fri May 13 02:06:53 2011 +0000
@@ -14,6 +14,7 @@
 from core.widgets import AutoCompleteUserInput
 from messages.models import Message
 from messages.models import Options
+from messages import MSG_BOX_LIMIT
 
 
 # Maximum size of a private message in characters
@@ -53,6 +54,21 @@
             raise forms.ValidationError("Your message is too long. Please trim some text.")
         return msg
 
+    def clean(self):
+        # Can we send a message? Is our outbox full?
+
+        count = Message.objects.outbox(self.user).count()
+        if count >= MSG_BOX_LIMIT:
+            raise forms.ValidationError("Your outbox is full. Please delete some messages.")
+
+        # Is the receiver's inbox full?
+        count = Message.objects.inbox(self.rcvr_user).count()
+        if count >= MSG_BOX_LIMIT:
+            raise forms.ValidationError(
+                "Sorry, %s's inbox is full. This message cannot be sent." % self.rcvr_user.username)
+
+        return self.cleaned_data
+
     def save(self, parent_msg=None):
         sender = self.user
         receiver = self.rcvr_user
--- 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,
         })
--- a/gpp/settings.py	Fri May 13 00:12:53 2011 +0000
+++ b/gpp/settings.py	Fri May 13 02:06:53 2011 +0000
@@ -4,6 +4,8 @@
 import platform
 from decimal import Decimal
 
+from django.contrib.messages import constants as message_constants
+
 import local_settings
 
 project_path = os.path.abspath(os.path.split(__file__)[0])
@@ -175,6 +177,13 @@
 # Messages
 #######################################################################
 MESSAGE_STORAGE = 'django.contrib.messages.storage.session.SessionStorage'
+MESSAGE_TAGS = {
+    message_constants.DEBUG: 'notice',
+    message_constants.INFO: 'info',
+    message_constants.SUCCESS: 'success',
+    message_constants.WARNING: 'alert',
+    message_constants.ERROR: 'error',
+}
 
 #######################################################################
 # Email