approve.py 4.13 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_type
frekk's avatar
frekk committed
12
13
from memberdb.forms import MyModelForm
from memberdb.views import MyUpdateView
14
15
16
17
18
19

"""
inline admin change list action buttons
see https://medium.com/@hakibenita/how-to-add-custom-action-buttons-to-django-admin-8d266f5b0d41
and have a look at .admin.MembershipAdmin
"""
frekk's avatar
frekk committed
20
class MembershipApprovalForm(MyModelForm):
frekk's avatar
frekk committed
21
    payment_confirm = forms.BooleanField(label='Confirm payment', required=False)
frekk's avatar
frekk committed
22

23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
    class Meta:
        model = Membership
        fields = ['membership_type', 'payment_method']
        widgets = {
            'membership_type': forms.RadioSelect(),
            'payment_method': forms.RadioSelect(),
        }

    """
    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.
    """
    def clean(self):
        # get the cleaned data from the form API and do something with it
        data = super().clean()
frekk's avatar
frekk committed
39
40
        now = timezone.now()
        #breakpoint()
41
        # find a Member matching our current username
frekk's avatar
frekk committed
42
43
44
45
46
47
48
49
        approver = Member.objects.filter(username__exact=self.request.user.username).first()
        if (approver == None):
            self.add_error(None, 'Cannot set approver: no Member record with username %s' % self.request.user.username)
        data['approver'] = approver
        data['approved'] = True
        data['date_approved'] = now

        if (data['payment_confirm'] == True):
frekk's avatar
frekk committed
50
51
            if (data['payment_method'] == ''):
                self.add_error('payment_method', 'Please select a payment method')
frekk's avatar
frekk committed
52
            data['date_paid'] = now
frekk's avatar
frekk committed
53
54
        else:
            data['date_paid'] = None
frekk's avatar
frekk committed
55
56
57
58
59
60
61
62
        
        # make sure "no payment" is recorded for Life Members.
        # XXX this might not actually be the case, since some life members may want to also be financial members (ie. for constitutional voting rights)
        #     and so this is probably more annoying than helpful
        if (data['membership_type'] == ''):
            if (data['payment_method'] != ''):
                self.add_error('payment_method', 'Life members shall not pay membership fees!')
            data['payment_method'] = ''
63
64
65
66
67
68
69
70

        return data

    """
    do the stuff, approve the things
    """
    def save(self, commit=True):
        ms = super().save(commit=False)
frekk's avatar
frekk committed
71
72
73
74
75
76
77
78

        # copy attributes not specified in 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']

        # do something
79
80
81
82
        if (commit):
            ms.save()
        return ms

frekk's avatar
frekk committed
83
class MembershipApprovalAdminView(MyUpdateView):
84
85
86
87
88
89
90
91
    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):
frekk's avatar
frekk committed
92
        ms = self.get_object()
93
94
95
96
        context = super().get_context_data(**kwargs)
        context.update(self.admin.admin_site.each_context(self.request))
        context.update({
            'opts': self.admin.model._meta,
frekk's avatar
frekk committed
97
98
99
            'ms': ms,
            'member': ms.member,
            'show_member_summary': True,
100
101
102
103
104
105
106
107
108
109
110
        })
        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(
111
112
            'admin:memberdb_membership_changelist',
            args=[],
113
114
115
116
117
            current_app=self.admin.admin_site.name,
        )
        return HttpResponseRedirect(url)