Initial commit.
[fsij-members-webapp.git] / meeting_attendance / views.py
1 from django.shortcuts import render_to_response, get_object_or_404
2 from fsij.meeting_attendance.models import Meeting, Attendance, Registration, Result
3 from fsij.membership.models import Member
4 from fsij.settings import LOGIN_URL
5 from django.contrib.auth.forms import AuthenticationForm
6 from django.contrib.auth.decorators import login_required, user_passes_test
7 from django.http import HttpResponseRedirect
8 from django.core.urlresolvers import reverse
9 import datetime
10
11 def detail(request, object_id):
12     m = get_object_or_404(Meeting, pk=object_id)
13     finished = (m.date_time < datetime.datetime.now())
14     if request.user.is_authenticated():
15         member = request.user.get_profile()
16     else:
17         member = None
18     r = None
19     try:
20         if (member):
21             r = m.registration_set.get(member=member)
22     except (KeyError, Registration.DoesNotExist):
23         pass
24     result = None
25     if (finished):
26         try:
27             if (member):
28                 result = m.result_set.get(member=member)
29         except (KeyError, Result.DoesNotExist):
30             pass
31     request.session.set_test_cookie()
32     return render_to_response('meeting_attendance/meeting_detail.html',
33                               {'object': m,
34                                'form': AuthenticationForm(request),
35                                'next' : request.path,
36                                'user' : request.user,
37                                'meeting_finished' : finished,
38                                'attended' : result,
39                                'registration' : r })
40
41 @login_required
42 def register(request, object_id):
43     m = get_object_or_404(Meeting, pk=object_id)
44     try:
45         r = m.registration_set.get(member=request.user.get_profile())
46     except (KeyError, Registration.DoesNotExist):
47         c = m.attendance_set.get(pk=request.POST['choice'])
48         r = Registration(member=request.user.get_profile(), choice=c, meeting=m,
49                          submit_date=datetime.date.today())
50         r.save()
51     else:
52         try:
53             r.choice = m.attendance_set.get(pk=request.POST['choice'])
54         except (KeyError, Attendance.DoesNotExist):
55             r.delete()
56         else:
57             r.submit_date = datetime.date.today()
58             r.save()
59     return HttpResponseRedirect(reverse('fsij.meeting_attendance.views.detail', args=(m.id,)))
60
61 # @permission_required('meeting_attendance.can_list_registration')
62 @user_passes_test(lambda u: u.has_perm('meeting_attendance.can_list_registration'), LOGIN_URL)
63 def registration_list(request, object_id):
64     m = get_object_or_404(Meeting, pk=object_id)
65     l = m.registration_set.all()
66     return render_to_response('meeting_attendance/registration_list.html',
67                               {'object': m,
68                                'list' : l,
69                                })
70
71 # @permission_required('meeting_attendance.can_list_result')
72 @user_passes_test(lambda u: u.has_perm('meeting_attendance.can_list_result'), LOGIN_URL)
73 def attendance_list(request, object_id):
74     m = get_object_or_404(Meeting, pk=object_id)
75     l = m.result_set.all()
76     all_members = Member.objects.all()
77     return render_to_response('meeting_attendance/attendance_list.html',
78                               {'object': m,
79                                'list' : l,
80                                'members' : all_members,
81                                })
82
83 # @permission_required('meeting_attendance.can_change_result')
84 @user_passes_test(lambda u: u.has_perm('meeting_attendance.can_change_results'), LOGIN_URL)
85 def attendance_submit(request, object_id):
86     m = get_object_or_404(Meeting, pk=object_id)
87     member = Member.objects.get(member_id=request.POST['member_id'])
88     attendance = (request.POST['attendance'] == "True")
89     try:
90         reg = m.registration_set.get(member=member)
91     except (KeyError, Registration.DoesNotExist):
92         reg = None
93     try:
94         r = m.result_set.get(member=member)
95     except (KeyError, Result.DoesNotExist):
96         r = Result(member=member, meeting=m,
97                    registration=reg,attended=attendance)
98         r.save()
99     else:
100         r.attended=attendance
101         r.registration=reg
102         r.save()
103     return HttpResponseRedirect(reverse('fsij.meeting_attendance.views.attendance_list', args=(m.id,)))