approve.py 3.12 KB
Newer Older
1
2
3
4
5
6
7
"""
This file implements the door/committee/admin facing membership approval (and payment) interface
See ../../README.md for details
"""
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
frekk's avatar
frekk committed
8
from django.utils import timezone
9
10
from django import forms

11
from memberdb.models import Member, Membership, get_membership_choices
frekk's avatar
frekk committed
12
13
from memberdb.forms import MyModelForm
from memberdb.views import MyUpdateView
14

15

frekk's avatar
frekk committed
16
class MembershipApprovalForm(MyModelForm):
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
	payment_confirm = forms.BooleanField(
		label = 'Confirm payment',
		required = False
	)

	class Meta:
		model = Membership
		fields = ['membership_type', 'payment_method']
		widgets = {
			'membership_type': forms.RadioSelect(),
			'payment_method': forms.RadioSelect(),
		}

	def __init__(self, *args, **kwargs):
		super().__init__(*args, **kwargs)

		# override the model membership_type field so we display all the options with prices
		self.fields['membership_type'].choices = get_membership_choices()

	def clean(self):
		"""
		Called to validate the data on the form.
		here we fill out some fields automatically (ie. approver, date paid / approved, etc.)
		TODO: deal with account activation/creation, etc.
		"""
		data = super().clean()
		now = timezone.now()
		approver = self.request.member
		if (approver == None):
			self.add_error(None, 'Cannot set approver: no Member record associated with current session. (username %s)' % self.request.user.username)

		data['approver'] = approver
		data['approved'] = True
		data['date_approved'] = now

		if (data['payment_confirm'] == True):
			if (data['payment_method'] == ''):
				self.add_error('payment_method', 'Please select a payment method')
			data['date_paid'] = now
		else:
			data['date_paid'] = None

		return data

	def save(self, commit=True):
		""" save the data into a Membership object """
		ms = super().save(commit=False)

		# copy attributes not specified in editable form fields
		ms.approver = self.cleaned_data['approver']
		ms.approved = self.cleaned_data['approved']
		ms.date_approved = self.cleaned_data['date_approved']
		ms.date_paid = self.cleaned_data['date_paid']

		if (commit):
			ms.save()
		return ms
74

frekk's avatar
frekk committed
75
class MembershipApprovalAdminView(MyUpdateView):
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
	template_name = 'admin/memberdb/membership_approve.html'
	form_class = MembershipApprovalForm
	model = Membership
	pk_url_kwarg = 'object_id'
	# override with the instance of ModelAdmin
	admin = None

	def get_context_data(self, **kwargs):
		ms = self.get_object()
		context = super().get_context_data(**kwargs)
		context.update(self.admin.admin_site.each_context(self.request))
		context.update({
			'opts': self.admin.model._meta,
			'ms': ms,
			'member': ms.member,
			'show_member_summary': True,
		})
		return context

	"""
	called when the approval form is submitted and valid data (according to the form's field types and defined validators) is given
	"""
	def form_valid(self, form):
		ms = form.save()

		self.admin.message_user(self.request, 'Approve success')
		url = reverse(
			'admin:memberdb_membership_changelist',
			args=[],
			current_app=self.admin.admin_site.name,
		)
		return HttpResponseRedirect(url)
108
109