account.py 4.07 KB
Newer Older
Zack Wong's avatar
Zack Wong committed
1
2
3
4
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
from django.utils import timezone
5
from django.contrib import messages
Zack Wong's avatar
Zack Wong committed
6
from django import forms
7
from formtools.wizard.views import SessionWizardView
Zack Wong's avatar
Zack Wong committed
8
9

from .models import Member
10
11
from .forms import MyModelForm, MyForm
from .views import MyUpdateView, MyWizardView
12
from memberdb.account_backend import validate_username, create_ad_user
Zack Wong's avatar
Zack Wong committed
13

14
15


Zack Wong's avatar
Zack Wong committed
16
17
class AccountForm(MyModelForm):
	# form fields
18
19
20
	username = forms.SlugField(
		validators=[validate_username],
		max_length=19,
Zack Wong's avatar
Zack Wong committed
21
22
	)
	password = forms.CharField(
23
24
25
		min_length=10,
		max_length=127,
		widget=forms.PasswordInput,
Zack Wong's avatar
Zack Wong committed
26
		strip=False,
27
		help_text="Password must be between 10 and 127 characters long"
28
	)
Zack Wong's avatar
Zack Wong committed
29
	confirm_password = forms.CharField(
30
31
		min_length=10,
		max_length=127,
Zack Wong's avatar
Zack Wong committed
32
33
34
35
36
37
		widget=forms.PasswordInput,
		strip=False,
	)

	class Meta:
		model = Member
38
39
		fields = ['username']

Zack Wong's avatar
Zack Wong committed
40
41
42
43
44
45
46
47
	def clean(self):
		try:
			if (self['password'].value() != self['confirm_password'].value()):
				self.add_error('confirm_password', 'Passwords must match.')
		except:
			pass
		super().clean();

Zack Wong's avatar
Zack Wong committed
48

49

50
51
52
class EmailForm(MyModelForm):
	forward = forms.BooleanField(required=False)
	email_address  = forms.EmailField(
53
		label='Forwarding address (optional)',
54
55
56
57
58
59
60
		required=False,
		help_text="Your club email will be forwarded to this address. Leave blank if email forwarding is not required"
	)

	class Meta:
		model = Member
		fields = ['forward', 'email_address']
61

62
63
64
	def clean(self):
		if self['forward'].value() == True:
			try:
Zack Wong's avatar
Zack Wong committed
65
				if (len(self['email_address'].value()) == 0):
Zack Wong's avatar
Zack Wong committed
66
					self.add_error('email_address', 'Email field cannot be left blank.')
67
68
69
70
71
72
73
74
				if (self['email_address'].value().split('@')[1] in ["ucc.asn.au", "ucc.gu.uwa.edu.au"]):
					self.add_error('email_address', 'Forwarding address cannot be the same as your account address.')
			except:
				pass
		super().clean();

class DispenseForm(MyForm):
	pin = forms.CharField(
75
76
77
		min_length=0,
		max_length=4,
		widget=forms.PasswordInput,
78
		strip=False,
Zack Wong's avatar
Zack Wong committed
79
		required=False,
80
		help_text="PIN must be 4 digits long")
81
82

	confirm_pin = forms.CharField(
83
84
		min_length=0,
		max_length=4,
85
		widget=forms.PasswordInput,
Zack Wong's avatar
Zack Wong committed
86
		required=False,
87
88
89
90
91
92
93
94
95
96
97
98
99
		strip=False,
	)
	def clean(self):
		try:
			if len(self['pin'].value()) != 4 :
				self.add_error('pin', 'PIN must be excatly 4 digits.')
			if not self['pin'].value().isdigit():
				self.add_error('pin', 'PIN can only contain numbers.')
			if (self['pin'].value() != self['confirm_pin'].value()):
				self.add_error('confirm_pin', 'PINs must match.')
		except:
			pass
		super().clean();
100

Zack Wong's avatar
Zack Wong committed
101

102
103
class AccountView(MyWizardView):
	form_list = [AccountForm,EmailForm,DispenseForm]
Zack Wong's avatar
Zack Wong committed
104
105
106
	template_name = 'admin/memberdb/account_create.html'
	admin = None

107
108
109
	def get_form_instance(self, step):
		return self.object

Zack Wong's avatar
Zack Wong committed
110
	def get_context_data(self, **kwargs):
111
		m = self.object
Zack Wong's avatar
Zack Wong committed
112
113
114
115
116
117
118
119
		context = super().get_context_data(**kwargs)
		context.update(self.admin.admin_site.each_context(self.request))
		context.update({
			'opts': self.admin.model._meta,
			'member': m,
		})
		return context

120
121

	def done(self, form_list, form_dict, **kwargs):
122
123


Zack Wong's avatar
Zack Wong committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
		# create the user and save their username if successfull
		try:
			if create_ad_user(self.get_cleaned_data_for_step('0'), self.object):
				form_dict['0'].save()

			make_home(self.get_cleaned_data_for_step('1'), self.object)
			make_dispense_account(self.object.username, self.get_cleaned_data_for_step('2')['pin'])
			subscribe_to_list(self.object)
		except Exception as e:
			messages.error(self.request,'Account creation failed for %s', self.object)
			messages.error(self.request, e)
			raise #DEBUG

		else:
			messages.success(self.request, 'An account has been successfully created for %s.' % self.object)
139
140
141
142
143
144
145
146
		return HttpResponseRedirect(reverse("admin:memberdb_membership_changelist"))

		#return accountProgressView(self.request, m)


def accountProgressView(request, member):
	return

147
148


149
150

def accountFinalView():
151
	return render(request, 'accountfinal.html', context)