Initial commit.
[fsij-members-webapp.git] / membership / views.py
1 from django.shortcuts import render_to_response, get_object_or_404
2 from django.contrib.auth.decorators import login_required, user_passes_test
3 from fsij.membership.models import *
4 from django.http import HttpResponseRedirect
5 from django.core.urlresolvers import reverse
6 from fsij.settings import LOGIN_URL
7 import datetime, time, re
8
9 def member_index(request, member):
10     return render_to_response('membership/member_index.html',
11                               {'member': member,
12                                })
13
14 def member_email(request, member, manage=False):
15     errors = None
16     try:
17         email = request.session['member_email_data']
18         del request.session['member_email_data']
19         errors = addr['errors']
20     except (KeyError):
21         email = member.user.email
22     try: referrer = request.META['HTTP_REFERER']
23     except: referrer = None
24     if referrer:
25        parent_is_list = (not re.match(".*/list/$", referrer))
26     else: parent_is_list = None
27     return render_to_response('membership/member_email.html',
28                               {'member': member,
29                                'email': email,
30                                'errors': errors,
31                                'original_path': request.path,
32                                'reload_parent': parent_is_list and manage,
33                                })
34
35 def register_member_email(request, member):
36     email = request.POST['email']
37     errors = []
38     if not email:
39         errors.append('email')
40     if errors:
41         request.session['member_email_data'] = {
42             'email': email,
43             'errors': errors,}
44         return HttpResponseRedirect(request.POST['original_path'])
45     member.user.email = email
46     member.user.save()
47     return HttpResponseRedirect(request.POST['original_path'])
48
49 def member_address(request, member, manage=False):
50     errors = None
51     try:
52         addr = request.session['member_address_data']
53         del request.session['member_address_data']
54         errors = addr['errors']
55     except (KeyError):
56         try:
57             addr = MemberAddress.objects.get(member=member)
58         except (MemberAddress.DoesNotExist):
59             addr = None
60     try: referrer = request.META['HTTP_REFERER']
61     except: referrer = None
62     if referrer:
63        parent_is_list = (not re.match(".*/list/$", referrer))
64     else: parent_is_list = None
65     return render_to_response('membership/member_address.html',
66                               {'member': member,
67                                'addr': addr,
68                                'errors': errors,
69                                'original_path': request.path,
70                                'reload_parent': parent_is_list and manage,
71                                })
72
73 def register_member_address(request, member):
74     try: request.POST['contact_is_home']
75     except: contact_is_home = False
76     else:   contact_is_home = True
77     zip_code = request.POST['zip_code']
78     address_line1 = request.POST['address_line1']
79     address_line2 = request.POST['address_line2']
80     address_line3 = request.POST['address_line3']
81     affiliation = request.POST['affiliation']
82     errors = []
83     if not contact_is_home and not affiliation:
84         errors.append('affiliation')
85     if not zip_code:
86         errors.append('zip_code')
87     if not address_line1:
88         errors.append('address_line1')
89     if errors:
90         request.session['member_address_data'] = {
91             'contact_is_home': contact_is_home,
92             'zip_code': zip_code,
93             'address_line1': address_line1,
94             'address_line2': address_line2,
95             'address_line3': address_line3,
96             'affiliation': affiliation,
97             'errors': errors,}
98         return HttpResponseRedirect(request.POST['original_path'])
99     date_updated = datetime.date.today()
100     try:
101         addr = MemberAddress.objects.get(member=member)
102         addr.contact_is_home = contact_is_home
103         addr.zip_code = zip_code
104         addr.address_line1 = address_line1
105         addr.address_line2 = address_line2
106         addr.address_line3 = address_line3
107         addr.affiliation = affiliation
108         addr.date_updated = date_updated
109     except (MemberAddress.DoesNotExist):
110         addr = MemberAddress(member = member,
111                              contact_is_home = contact_is_home,
112                              zip_code = zip_code,
113                              address_line1 = address_line1,
114                              address_line2 = address_line2,
115                              address_line3 = address_line3,
116                              affiliation = affiliation,
117                              date_updated = date_updated)
118     addr.save()
119     return HttpResponseRedirect(request.POST['original_path'])
120
121 def member_payment(request, member, manage=False):
122     try:
123         payments = member.memberpayment_set.order_by('-fee', '-date')
124     except (MemberPayment.DoesNotExist):
125         payments = None
126     if manage:
127         fee_list = MemberFee.objects.filter(year__gte = (member.date_join.year - 1))
128         if member.member_type.id == 3:
129             fee_list = fee_list.exclude(year = 9999)
130         fee_list_in_payments = map(lambda (p): p.fee, member.memberpayment_set.all())
131         fee_list = filter(lambda (f): not f in fee_list_in_payments, fee_list)
132     else:
133         fee_list = None
134     member_type_list = MemberType.objects.all().exclude(id=3)
135     try: referrer = request.META['HTTP_REFERER']
136     except: referrer = None
137     if referrer:
138        parent_is_list = (not re.match(".*/list/$", referrer))
139     else: parent_is_list = None
140     return render_to_response('membership/member_payment.html',
141                               {'member': member,
142                                'payments': payments,
143                                'reload_parent': parent_is_list and manage,
144                                'manage' : manage,
145                                'fee_list' : fee_list,
146                                'member_type_list': member_type_list })
147
148 def add_member_payment(request, member):
149     p = MemberPayment(member = member,
150                       date = datetime.date(*time.strptime(request.POST['date'], '%Y-%m-%d')[:3]),
151                       member_type = MemberType.objects.get(id=request.POST['member_type']),
152                       fee = MemberFee.objects.get(year = request.POST['fee']),
153                       contribution = request.POST['contribution'])
154     p.save()
155     return HttpResponseRedirect(reverse('fsij.membership.views.manage_member_payment', args=(member.member_id,)))
156
157 def delete_member_payment(request, member, payment_id):
158     p = MemberPayment.objects.get(pk = payment_id)
159     p.delete()
160     return HttpResponseRedirect(reverse('fsij.membership.views.manage_member_payment', args=(member.member_id,)))
161
162 @login_required
163 def index(request):
164     member = request.user.get_profile()
165     return member_index(request, member)
166
167 @login_required
168 def email(request):
169     member = request.user.get_profile()
170     return member_email(request, member)
171
172 @login_required
173 def register_email(request):
174     member = request.user.get_profile()
175     return register_member_email(request, member)
176
177 @login_required
178 def address(request):
179     member = request.user.get_profile()
180     return member_address(request, member)
181
182 @login_required
183 def register_address(request):
184     member = request.user.get_profile()
185     return register_member_address(request, member)
186
187 @login_required
188 def payment(request):
189     member = request.user.get_profile()
190     return member_payment(request, member)
191
192
193 @user_passes_test(lambda u: u.has_perm('membership.can_manage'), LOGIN_URL)
194 def manage_member(request, member_id, new):
195 ### XXX New member!!
196 ### XXX CorporateMember!!
197     errors = None
198     try:
199         member_data = request.session['member_data']
200         del request.session['member_data']
201         errors = member_data['errors']
202         member = member_data
203     except (KeyError):
204         member = Member.objects.get(member_id=member_id)
205     try: referrer = request.META['HTTP_REFERER']
206     except: referrer = None
207     if referrer:
208        parent_is_list = (not re.match(".*/list/$", referrer))
209     else: parent_is_list = None
210     return render_to_response('membership/member_edit.html',
211                               {'member': member,
212                                'member_type_list': MemberType.objects.all(),
213                                'original_path': request.path,
214                                'reload_parent': parent_is_list,
215                                'errors': errors,
216                                })
217
218 @user_passes_test(lambda u: u.has_perm('membership.can_manage'), LOGIN_URL)
219 def manage_register_member(request, member_id):
220     member = Member.objects.get(member_id=member_id)
221     errors = []
222     member_type = MemberType.objects.get(id=request.POST['member_type'])
223     name_in_alphabet = request.POST['name_in_alphabet']
224     date_join = request.POST['date_join']
225     date_quit = request.POST['date_quit']
226     advance_payment = request.POST['advance_payment']
227     if not name_in_alphabet:
228         errors.append('name_in_alphabet')
229     if errors:
230         request.session['member_data'] = {
231             'member_id': member.member_id,
232             'member_type' : member_type,
233             'name_in_alphabet': name_in_alphabet,
234             'date_join': date_join,
235             'date_quit': date_quit,
236             'advance_payment': advance_payment,
237             'errors': errors,
238             }
239         return HttpResponseRedirect(request.POST['original_path'])
240     member.member_type = member_type
241     member.name_in_alphabet = name_in_alphabet
242     if date_join and date_join != "None":
243         member.date_join = datetime.date(*time.strptime(date_join, '%Y-%m-%d')[:3])
244         
245     if date_quit and date_quit != "None":
246         member.date_quit = datetime.date(*time.strptime(date_quit, '%Y-%m-%d')[:3])
247     member.advance_payment = advance_payment
248     member.save()
249     return HttpResponseRedirect(request.POST['original_path'])
250
251 @user_passes_test(lambda u: u.has_perm('membership.can_manage'), LOGIN_URL)
252 def manage_member_email(request, member_id):
253     member = get_object_or_404(Member, member_id=member_id)
254     return member_email(request, member, manage=True)
255
256 @user_passes_test(lambda u: u.has_perm('membership.can_manage'), LOGIN_URL)
257 def manage_register_member_email(request, member_id):
258     member = get_object_or_404(Member, member_id=member_id)
259     return register_member_email(request, member)
260
261 @user_passes_test(lambda u: u.has_perm('membership.can_manage'), LOGIN_URL)
262 def manage_member_address(request, member_id):
263     member = get_object_or_404(Member, member_id=member_id)
264     return member_address(request, member, manage=True)
265
266 @user_passes_test(lambda u: u.has_perm('membership.can_manage'), LOGIN_URL)
267 def manage_register_member_address(request, member_id):
268     member = get_object_or_404(Member, member_id=member_id)
269     return register_member_address(request, member)
270
271 @user_passes_test(lambda u: u.has_perm('membership.can_manage'), LOGIN_URL)
272 def manage_member_payment(request, member_id):
273     member = get_object_or_404(Member, member_id=member_id)
274     return member_payment(request, member, manage=True)
275
276 @user_passes_test(lambda u: u.has_perm('membership.can_manage'), LOGIN_URL)
277 def manage_add_member_payment(request, member_id):
278     member = get_object_or_404(Member, member_id=member_id)
279     return add_member_payment(request, member)
280
281 @user_passes_test(lambda u: u.has_perm('membership.can_manage'), LOGIN_URL)
282 def manage_delete_member_payment(request, member_id, payment_id):
283     member = get_object_or_404(Member, member_id=member_id)
284     return delete_member_payment(request, member, payment_id)
285
286 @user_passes_test(lambda u: u.has_perm('membership.can_manage'), LOGIN_URL)
287 def member_list_top(request):
288     return render_to_response('membership/member_list_top.html',
289                               { })
290
291 @user_passes_test(lambda u: u.has_perm('membership.can_manage'), LOGIN_URL)
292 def member_list(request):
293     member_list = Member.objects.filter(date_quit__isnull=True).order_by('member_id')
294     return render_to_response('membership/member_list.html',
295                               {'members': member_list,
296                                })
297
298 @user_passes_test(lambda u: u.has_perm('membership.can_manage'), LOGIN_URL)
299 def payment_list(request):
300     payments = MemberPayment.objects.all().order_by('date_payment')
301     return render_to_response('membership/payment_list.html',
302                               {'payments': payments,
303                                })
304
305 @user_passes_test(lambda u: u.has_perm('membership.can_manage'), LOGIN_URL)
306 def valid_member_list(request,year):
307     year_value = int(year)
308     member_list = get_valid_member(year_value)
309     member_list_old = get_valid_member(year_value-1)
310     added = []
311     stable = []
312     deleted = []
313     for m in member_list:
314         if not m in member_list_old:
315             added.append(m)
316         else:
317             stable.append(m)
318     for m in member_list_old:
319         if not m in member_list:
320             deleted.append(m)
321     return render_to_response('membership/valid_member_list.html',
322                               {'stable': stable,
323                                'added': added,
324                                'deleted': deleted,
325                                'year': year,
326                                })