Mercurial > public > sg101
comparison 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 |
comparison
equal
deleted
inserted
replaced
435:b2f02766cc72 | 436:241c80ff16c5 |
---|---|
4 """ | 4 """ |
5 import datetime | 5 import datetime |
6 | 6 |
7 from django.contrib.auth.decorators import login_required | 7 from django.contrib.auth.decorators import login_required |
8 from django.contrib.auth.models import User | 8 from django.contrib.auth.models import User |
9 from django.contrib import messages | 9 from django.contrib import messages as django_messages |
10 from django.core.paginator import Paginator, EmptyPage, InvalidPage | 10 from django.core.paginator import Paginator, EmptyPage, InvalidPage |
11 from django.core.urlresolvers import reverse | 11 from django.core.urlresolvers import reverse |
12 from django.http import HttpResponse | 12 from django.http import HttpResponse |
13 from django.http import HttpResponseForbidden | 13 from django.http import HttpResponseForbidden |
14 from django.http import HttpResponseNotAllowed | 14 from django.http import HttpResponseNotAllowed |
18 import django.utils.simplejson as json | 18 import django.utils.simplejson as json |
19 | 19 |
20 from messages.models import Message, Options | 20 from messages.models import Message, Options |
21 from messages.forms import OptionsForm, ComposeForm | 21 from messages.forms import OptionsForm, ComposeForm |
22 from messages.utils import reply_subject, quote_message | 22 from messages.utils import reply_subject, quote_message |
23 | 23 from messages import MSG_BOX_LIMIT |
24 | 24 |
25 MSGS_PER_PAGE = 20 | 25 |
26 | 26 MSGS_PER_PAGE = 20 # message pagination value |
27 | |
28 # This must match the jQuery UI tab control | |
27 TAB_INDICES = { | 29 TAB_INDICES = { |
28 'inbox': 0, | 30 'inbox': 0, |
29 'compose': 1, | 31 'compose': 1, |
30 'outbox': 2, | 32 'outbox': 2, |
31 'trash': 3, | 33 'trash': 3, |
39 except ValueError: | 41 except ValueError: |
40 n = 1 | 42 n = 1 |
41 return n | 43 return n |
42 | 44 |
43 | 45 |
46 def _quota_check(box_name, count, request): | |
47 """ | |
48 Checks the message box count against MSG_BOX_LIMIT. | |
49 Emits a message to the user if the quota is exceeded. | |
50 | |
51 """ | |
52 if count >= MSG_BOX_LIMIT: | |
53 django_messages.warning(request, | |
54 "Your %s is full. Please delete some messages." % box_name) | |
55 | |
56 | |
44 @login_required | 57 @login_required |
45 def index(request, tab=None): | 58 def index(request, tab=None): |
46 """ | 59 """ |
47 This function displays the base tabbed private messages view. | 60 This function displays the base tabbed private messages view. |
48 | 61 |
78 """ | 91 """ |
79 if not request.user.is_authenticated(): | 92 if not request.user.is_authenticated(): |
80 return HttpResponseForbidden() | 93 return HttpResponseForbidden() |
81 | 94 |
82 msg_list = Message.objects.inbox(request.user) | 95 msg_list = Message.objects.inbox(request.user) |
96 _quota_check('inbox', msg_list.count(), request) | |
97 | |
83 paginator = Paginator(msg_list, MSGS_PER_PAGE) | 98 paginator = Paginator(msg_list, MSGS_PER_PAGE) |
84 try: | 99 try: |
85 msgs = paginator.page(_get_page(request)) | 100 msgs = paginator.page(_get_page(request)) |
86 except EmptyPage, InvalidPage: | 101 except EmptyPage, InvalidPage: |
87 msgs = paginator.page(paginator.num_pages) | 102 msgs = paginator.page(paginator.num_pages) |
99 """ | 114 """ |
100 if not request.user.is_authenticated(): | 115 if not request.user.is_authenticated(): |
101 return HttpResponseForbidden() | 116 return HttpResponseForbidden() |
102 | 117 |
103 msg_list = Message.objects.outbox(request.user) | 118 msg_list = Message.objects.outbox(request.user) |
119 _quota_check('outbox', msg_list.count(), request) | |
120 | |
104 paginator = Paginator(msg_list, MSGS_PER_PAGE) | 121 paginator = Paginator(msg_list, MSGS_PER_PAGE) |
105 try: | 122 try: |
106 msgs = paginator.page(_get_page(request)) | 123 msgs = paginator.page(_get_page(request)) |
107 except EmptyPage, InvalidPage: | 124 except EmptyPage, InvalidPage: |
108 msgs = paginator.page(paginator.num_pages) | 125 msgs = paginator.page(paginator.num_pages) |
176 if request.method == "POST": | 193 if request.method == "POST": |
177 options = Options.objects.for_user(request.user) | 194 options = Options.objects.for_user(request.user) |
178 form = OptionsForm(request.POST, instance=options, prefix='opts') | 195 form = OptionsForm(request.POST, instance=options, prefix='opts') |
179 if form.is_valid(): | 196 if form.is_valid(): |
180 form.save() | 197 form.save() |
181 messages.success(request, 'Options saved.') | 198 django_messages.success(request, 'Options saved.') |
182 else: | 199 else: |
183 options = Options.objects.for_user(request.user) | 200 options = Options.objects.for_user(request.user) |
184 form = OptionsForm(instance=options, prefix='opts') | 201 form = OptionsForm(instance=options, prefix='opts') |
185 | 202 |
186 return render(request, 'messages/options_tab.html', { | 203 return render(request, 'messages/options_tab.html', { |
209 else: | 226 else: |
210 parent_msg = None | 227 parent_msg = None |
211 | 228 |
212 if compose_form.is_valid(): | 229 if compose_form.is_valid(): |
213 compose_form.save(parent_msg=parent_msg) | 230 compose_form.save(parent_msg=parent_msg) |
214 messages.success(request, 'Message sent.') | 231 django_messages.success(request, 'Message sent.') |
215 compose_form = ComposeForm(request.user) | 232 compose_form = ComposeForm(request.user) |
216 else: | 233 else: |
217 if receiver is not None: | 234 if receiver is not None: |
218 form_data = {'receiver': receiver} | 235 form_data = {'receiver': receiver} |
219 compose_form = ComposeForm(request.user, initial=form_data) | 236 compose_form = ComposeForm(request.user, initial=form_data) |
220 else: | 237 else: |
221 compose_form = ComposeForm(request.user) | 238 compose_form = ComposeForm(request.user) |
239 | |
240 _quota_check('outbox', Message.objects.outbox(request.user).count(), request) | |
222 | 241 |
223 return render(request, 'messages/compose_tab.html', { | 242 return render(request, 'messages/compose_tab.html', { |
224 'compose_form': compose_form, | 243 'compose_form': compose_form, |
225 }) | 244 }) |
226 | 245 |