comparison messages/views.py @ 801:6bbd1473d48e

Work in progress: rework private messages.
author Brian Neal <bgneal@gmail.com>
date Sat, 30 Aug 2014 17:25:38 -0500
parents 89b240fe9297
children dbc389a409f5
comparison
equal deleted inserted replaced
800:689db80068c8 801:6bbd1473d48e
6 import json 6 import json
7 7
8 from django.contrib.auth.decorators import login_required 8 from django.contrib.auth.decorators import login_required
9 from django.contrib.auth.models import User 9 from django.contrib.auth.models import User
10 from django.contrib import messages as django_messages 10 from django.contrib import messages as django_messages
11 from django.core.paginator import Paginator, EmptyPage, InvalidPage 11 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
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
33 'trash': 3, 33 'trash': 3,
34 'options': 4, 34 'options': 4,
35 } 35 }
36 36
37 37
38 def _get_page(request):
39 try:
40 n = int(request.GET.get('page', '1'))
41 except ValueError:
42 n = 1
43 return n
44
45
46 def _quota_check(box_name, count, request): 38 def _quota_check(box_name, count, request):
47 """ 39 """
48 Checks the message box count against MSG_BOX_LIMIT. 40 Checks the message box count against MSG_BOX_LIMIT.
49 Emits a message to the user if the quota is exceeded. 41 Emits a message to the user if the quota is exceeded.
50 42
56 "Your %s is full. Please delete some messages." % box_name) 48 "Your %s is full. Please delete some messages." % box_name)
57 49
58 return 100 * count / MSG_BOX_LIMIT 50 return 100 * count / MSG_BOX_LIMIT
59 51
60 52
53 def _get_page(request, qs):
54 """Paginates the given queryset and returns a page object"""
55 paginator = Paginator(qs, MSGS_PER_PAGE)
56 try:
57 page = paginator.page(request.GET.get('page', '1'))
58 except PageNotAnInteger:
59 page = paginator.page(1)
60 except EmptyPage:
61 page = paginator.page(paginator.num_pages)
62 return page
63
64
61 @login_required 65 @login_required
62 def index(request, tab=None): 66 def inbox(request):
63 """ 67
64 This function displays the base tabbed private messages view. 68 msg_list = Message.objects.inbox(request.user)
65 69 msg_count = msg_list.count()
66 """ 70 pct_used = _quota_check('inbox', msg_count, request)
67 tab_index = TAB_INDICES[tab] if tab else 0 71
68 return render(request, 'messages/tabbed_base.html', { 72 page = _get_page(request, msg_list)
69 'tab': tab_index, 73
70 'unread_count': Message.objects.unread_count(request.user), 74 return render(request, 'messages/inbox.html', {
75 'tab': 'inbox',
76 'page': page,
77 'inbox_pct': pct_used,
78 'outbox_pct': None,
79 })
80
81
82 @login_required
83 def outbox(request):
84
85 msg_list = Message.objects.outbox(request.user)
86 msg_count = msg_list.count()
87 pct_used = _quota_check('outbox', msg_count, request)
88
89 page = _get_page(request, msg_list)
90
91 return render(request, 'messages/outbox.html', {
92 'tab': 'outbox',
93 'page': page,
94 'inbox_pct': None,
95 'outbox_pct': pct_used,
71 }) 96 })
72 97
73 98
74 @login_required 99 @login_required
75 def compose_to(request, receiver): 100 def compose_to(request, receiver):
83 tab_index = TAB_INDICES['compose'] 108 tab_index = TAB_INDICES['compose']
84 return render(request, 'messages/tabbed_base.html', { 109 return render(request, 'messages/tabbed_base.html', {
85 'tab': tab_index, 110 'tab': tab_index,
86 'receiver': receiver, 111 'receiver': receiver,
87 'unread_count': Message.objects.unread_count(request.user), 112 'unread_count': Message.objects.unread_count(request.user),
88 })
89
90
91 def inbox(request):
92 """
93 Returns the inbox for the user.
94
95 """
96 if not request.user.is_authenticated():
97 return HttpResponseForbidden()
98
99 msg_list = Message.objects.inbox(request.user)
100 msg_count = msg_list.count()
101 pct_used = _quota_check('inbox', msg_count, request)
102
103 paginator = Paginator(msg_list, MSGS_PER_PAGE)
104 try:
105 msgs = paginator.page(_get_page(request))
106 except (EmptyPage, InvalidPage):
107 msgs = paginator.page(paginator.num_pages)
108
109 return render(request, 'messages/inbox_tab.html', {
110 'msgs': msgs,
111 'url': reverse('messages-inbox'),
112 'pct_used': pct_used,
113 })
114
115
116 def outbox(request):
117 """
118 Returns the outbox for the user.
119
120 """
121 if not request.user.is_authenticated():
122 return HttpResponseForbidden()
123
124 msg_list = Message.objects.outbox(request.user)
125 msg_count = msg_list.count()
126 pct_used = _quota_check('outbox', msg_count, request)
127
128 paginator = Paginator(msg_list, MSGS_PER_PAGE)
129 try:
130 msgs = paginator.page(_get_page(request))
131 except (EmptyPage, InvalidPage):
132 msgs = paginator.page(paginator.num_pages)
133
134 return render(request, 'messages/outbox_tab.html', {
135 'msgs': msgs,
136 'url': reverse('messages-outbox'),
137 'pct_used': pct_used,
138 }) 113 })
139 114
140 115
141 def trash(request): 116 def trash(request):
142 """ 117 """