Commit 4598264f authored by frekk's avatar frekk
Browse files

added membership renewal form

parent 487c92ce
......@@ -5,11 +5,12 @@ See ../../README.md for details
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
from django.views.generic.edit import UpdateView
from django.utils import timezone
from django import forms
from memberdb.models import Member, Membership, MEMBERSHIP_TYPES_
from memberdb.forms import MyModelForm
from memberdb.views import MyUpdateView
def get_membership_type(member):
best = None
......@@ -37,11 +38,9 @@ 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
"""
class MembershipApprovalForm(forms.ModelForm):
class MembershipApprovalForm(MyModelForm):
payment_confirm = forms.BooleanField(label='Confirm payment', required=False)
# this must be passed by kwargs upon instantiating the form
request = None
class Meta:
model = Membership
......@@ -51,10 +50,6 @@ class MembershipApprovalForm(forms.ModelForm):
'payment_method': forms.RadioSelect(),
}
def __init__(self, *args, **kwargs):
self.request = kwargs.pop("request")
super().__init__(*args, **kwargs)
"""
Called to validate the data on the form.
here we fill out some fields automatically (ie. approver, date paid / approved, etc.)
......@@ -103,7 +98,7 @@ class MembershipApprovalForm(forms.ModelForm):
ms.save()
return ms
class MembershipApprovalAdminView(UpdateView):
class MembershipApprovalAdminView(MyUpdateView):
template_name = 'admin/memberdb/membership_approve.html'
form_class = MembershipApprovalForm
model = Membership
......@@ -124,10 +119,6 @@ class MembershipApprovalAdminView(UpdateView):
})
return context
def get_form_kwargs(self, **kwargs):
kwargs.update(super().get_form_kwargs())
kwargs.update({'request': self.request})
return kwargs
"""
called when the approval form is submitted and valid data (according to the form's field types and defined validators) is given
......
from django import forms
"""
Custom ModelForm class with some extra features
"""
class MyModelForm(forms.ModelForm):
# this must be passed by kwargs upon instantiating the form
request = None
def __init__(self, *args, **kwargs):
self.request = kwargs.pop("request")
super().__init__(*args, **kwargs)
......@@ -6,10 +6,13 @@ from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
from django.views.generic.edit import CreateView, UpdateView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.utils.safestring import mark_safe
from django import forms
from .models import Member, Membership
from .forms import MyModelForm
from .views import MyUpdateView
from .approve import make_pending_membership
"""
......@@ -17,10 +20,10 @@ First step: enter an email address and some details (to fill at least a Member m
see https://docs.djangoproject.com/en/2.1/ref/models/fields/#error-messages
and https://docs.djangoproject.com/en/2.1/ref/forms/fields/#error-messages
"""
class RegisterForm(forms.ModelForm):
class RegisterForm(MyModelForm):
confirm_email = forms.EmailField(label='Confirm your email address', required=False)
agree_tnc = forms.BooleanField(label='I agree to the terms & conditions', required=True, help_text=mark_safe(
"You, by completing this form, agree to abide by the UCC Constitution, rulings of the UCC Committee, UCC and "
"You agree to abide by the UCC Constitution, rulings of the UCC Committee, UCC and "
"UWA’s Network Usage Guidelines and that you will be subscribed to the UCC Mailing List. <br>"
'Policies can be found <a href="https://www.ucc.asn.au/infobase/policies.ucc">here</a>.'))
#membership_type = forms.ChoiceField(label='Select your membership type', required=True, choices=MEMBERSHIP_TYPES)
......@@ -36,13 +39,15 @@ class RegisterForm(forms.ModelForm):
}
def clean(self):
if (self['email_address'].value() != self['confirm_email'].value()):
self.add_error('email_address', 'Email addresses must match.')
print(self.errors.as_data())
try:
if (self['email_address'].value() != self['confirm_email'].value()):
self.add_error('email_address', 'Email addresses must match.')
except:
pass
super().clean();
def save(self, commit=True):
# create a new Member model instance (ie. a record in the Members table) based on submitted form data
# get the Member model instance (ie. a record in the Members table) based on submitted form data
m = super().save(commit=False)
if (m.display_name == ""):
m.display_name = "%s %s" % (m.first_name, m.last_name);
......@@ -53,15 +58,33 @@ class RegisterForm(forms.ModelForm):
ms = make_pending_membership(m)
if (commit):
ms.save();
return (m, ms)
return m, ms
class RenewForm(RegisterForm):
confirm_email = None
class Meta(RegisterForm.Meta):
fields = ['first_name', 'last_name', 'phone_number', 'is_student', 'is_guild', 'id_number', 'email_address']
def save(self, commit=True):
m, ms = super().save(commit=False)
m.username = self.request.user.username
if (commit):
m.save()
ms.save()
return m, ms
"""
simple FormView which displays registration form and handles template rendering & form submission
"""
class RegisterView(CreateView):
class RegisterView(MyUpdateView):
template_name = 'register.html'
form_class = RegisterForm
model = Member
can_create = False
"""
called when valid form data has been POSTed
......@@ -70,11 +93,35 @@ class RegisterView(CreateView):
def form_valid(self, form):
# save the member data and get the Member instance
m, ms = form.save()
return HttpResponseRedirect(reverse("memberdb:info", kwargs={'username': m.username}))
return HttpResponseRedirect(reverse("memberdb:index"))
class RenewView(UpdateView):
class RenewView(LoginRequiredMixin, MyUpdateView):
template_name = 'renew.html'
form_class = RegisterForm
form_class = RenewForm
model = Member
def get_object(self):
obj = Member.objects.filter(username__exact=self.request.user.username).first()
return obj
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'is_new': self.object == None,
})
return context
# get the initial data with which to pre-fill the form
def get_initial(self):
data = super().get_initial()
u = self.request.user
data.update({
'first_name': u.first_name,
'last_name': u.last_name,
'email_address': u.email,
})
return data
def form_valid(self, form):
pass
m, ms = form.save()
return HttpResponseRedirect(reverse("memberdb:index"))
from datetime import date
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
from django.forms import ModelForm
from django.views.generic.edit import UpdateView
from django import forms
from .models import Member, IncAssocMember, Membership
......@@ -13,8 +15,26 @@ def index(request):
}
return render(request, 'index.html', context)
def renew(request, username):
return HttpResponse("Renew your membership now, " + username);
def getactive(request):
actives = Membership.objects.all().select_related('member')
actives.filter(date_submitted__range=(date(2018, 1, 1), date(2018, 12, 31)))
context = {
'member_list': actives,
}
return render(request, 'index.html', context)
"""
Can update and create models.
Also passes the request object to the form via its kwargs.
"""
class MyUpdateView(UpdateView):
can_create = True
object = None
def get_object(self):
return self.object
def info(request, username):
return HttpResponse("Information for user " + username)
\ No newline at end of file
def get_form_kwargs(self, **kwargs):
kwargs.update(super().get_form_kwargs())
kwargs.update({'request': self.request})
return kwargs
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment