-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathviews.py
78 lines (61 loc) · 2.65 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python
# vim: ai ts=4 sts=4 et sw=4
# maintainer: dgelvin
'''
Views for logger_ng
'''
import re
from django.db.models import Q
from django.shortcuts import get_object_or_404, redirect
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.contrib.auth.decorators import login_required, permission_required
from rapidsms.webui.utils import render_to_response
from logger_ng.models import LoggedMessage
from logger_ng.utils import respond_to_msg
@login_required
@permission_required('logger_ng.can_view')
def index(request):
'''
Index view
'''
MESSAGES_PER_PAGE = 30
# If it is a POST then they are responding to a message.
# Don't allow sending if the user doesn't have the can_respond permission
if request.method == 'POST' and\
request.user.has_perm('logger_ng.can_respond'):
for field, value in request.POST.iteritems():
match = re.match(r'^respond_(?P<id>\d+)$', field)
if match and len(value) > 1:
pk = match.groupdict()['id']
msg = get_object_or_404(LoggedMessage, pk=pk)
respond_to_msg(msg, value)
return redirect(index)
# Don't exclude outgoing messages that are a response to another,
# because they will be shown threaded beneath the original message
msgs = LoggedMessage.objects.exclude(
direction=LoggedMessage.DIRECTION_OUTGOING,
response_to__isnull=False)
# filter from form
if request.method == 'GET':
search = request.GET.get('logger_ng_search_box', '')
msgs = msgs.filter(Q(identity__icontains=search) | \
Q(text__icontains=search) | \
Q(reporter__first_name__icontains=search) | \
Q(reporter__last_name__icontains=search))
msgs = msgs.order_by('-date', 'direction')
paginator = Paginator(msgs, MESSAGES_PER_PAGE)
# Set the pagination page. Go to page 1 in the event there is no page
# get variable or if it's something other than a number.
try:
page = int(request.GET.get('page', '1'))
except ValueError:
page = 1
# Try to set the paginator object to the correct page. Set it to the
# last page if there is some problem with it (too high, etc...)
try:
msgs = paginator.page(page)
except (EmptyPage, InvalidPage):
msgs = paginator.page(paginator.num_pages)
ctx = locals()
ctx['request'] = request
return render_to_response(request, "logger_ng/index.html", ctx)