|
1 | | -import datetime |
2 | 1 | from django import forms |
3 | | -from django.contrib.auth import authenticate |
| 2 | +from django.conf import settings |
4 | 3 | from django.contrib.auth.forms import UserCreationForm |
| 4 | +from django.db import transaction |
5 | 5 |
|
6 | | -from .models import User, AccountDetails, UserAddress |
| 6 | +from .models import User, BankAccountType, UserBankAccount, UserAddress |
| 7 | +from .constants import GENDER_CHOICE |
7 | 8 |
|
8 | 9 |
|
9 | | -class UserRegistrationForm(UserCreationForm): |
| 10 | +class UserAddressForm(forms.ModelForm): |
10 | 11 |
|
11 | 12 | class Meta: |
12 | | - model = User |
| 13 | + model = UserAddress |
13 | 14 | fields = [ |
14 | | - "first_name", |
15 | | - "last_name", |
16 | | - "email", |
17 | | - "contact_no", |
18 | | - "password1", |
19 | | - "password2" |
| 15 | + 'street_address', |
| 16 | + 'city', |
| 17 | + 'postal_code', |
| 18 | + 'country' |
20 | 19 | ] |
21 | 20 |
|
| 21 | + def __init__(self, *args, **kwargs): |
| 22 | + super().__init__(*args, **kwargs) |
22 | 23 |
|
23 | | -class AccountDetailsForm(forms.ModelForm): |
| 24 | + for field in self.fields: |
| 25 | + self.fields[field].widget.attrs.update({ |
| 26 | + 'class': ( |
| 27 | + 'appearance-none block w-full bg-gray-200 ' |
| 28 | + 'text-gray-700 border border-gray-200 rounded ' |
| 29 | + 'py-3 px-4 leading-tight focus:outline-none ' |
| 30 | + 'focus:bg-white focus:border-gray-500' |
| 31 | + ) |
| 32 | + }) |
24 | 33 |
|
25 | | - class Meta: |
26 | | - model = AccountDetails |
27 | | - fields = [ |
28 | | - 'gender', |
29 | | - 'birth_date', |
30 | | - 'picture' |
31 | | - ] |
32 | 34 |
|
33 | | - |
34 | | -class UserAddressForm(forms.ModelForm): |
| 35 | +class UserRegistrationForm(UserCreationForm): |
| 36 | + account_type = forms.ModelChoiceField( |
| 37 | + queryset=BankAccountType.objects.all() |
| 38 | + ) |
| 39 | + gender = forms.ChoiceField(choices=GENDER_CHOICE) |
| 40 | + birth_date = forms.DateField() |
35 | 41 |
|
36 | 42 | class Meta: |
37 | | - model = UserAddress |
| 43 | + model = User |
38 | 44 | fields = [ |
39 | | - 'street_address', |
40 | | - 'city', |
41 | | - 'postal_code', |
42 | | - 'country' |
| 45 | + 'first_name', |
| 46 | + 'last_name', |
| 47 | + 'email', |
| 48 | + 'password1', |
| 49 | + 'password2', |
43 | 50 | ] |
44 | 51 |
|
| 52 | + def __init__(self, *args, **kwargs): |
| 53 | + super().__init__(*args, **kwargs) |
45 | 54 |
|
46 | | -class UserLoginForm(forms.Form): |
47 | | - account_no = forms.IntegerField(label="Account Number") |
48 | | - password = forms.CharField(widget=forms.PasswordInput) |
49 | | - |
50 | | - def clean(self, *args, **kwargs): |
51 | | - account_no = self.cleaned_data.get("account_no") |
52 | | - password = self.cleaned_data.get("password") |
| 55 | + for field in self.fields: |
| 56 | + self.fields[field].widget.attrs.update({ |
| 57 | + 'class': ( |
| 58 | + 'appearance-none block w-full bg-gray-200 ' |
| 59 | + 'text-gray-700 border border-gray-200 ' |
| 60 | + 'rounded py-3 px-4 leading-tight ' |
| 61 | + 'focus:outline-none focus:bg-white ' |
| 62 | + 'focus:border-gray-500' |
| 63 | + ) |
| 64 | + }) |
53 | 65 |
|
54 | | - if account_no and password: |
55 | | - user = authenticate(account_no=account_no, password=password) |
56 | | - if not user: |
57 | | - raise forms.ValidationError("Account Does Not Exist.") |
58 | | - if not user.check_password(password): |
59 | | - raise forms.ValidationError("Password Does not Match.") |
60 | | - if not user.is_active: |
61 | | - raise forms.ValidationError("Account is not Active.") |
| 66 | + @transaction.atomic |
| 67 | + def save(self, commit=True): |
| 68 | + user = super().save(commit=False) |
| 69 | + user.set_password(self.cleaned_data["password1"]) |
| 70 | + if commit: |
| 71 | + user.save() |
| 72 | + account_type = self.cleaned_data.get('account_type') |
| 73 | + gender = self.cleaned_data.get('gender') |
| 74 | + birth_date = self.cleaned_data.get('birth_date') |
62 | 75 |
|
63 | | - return super(UserLoginForm, self).clean(*args, **kwargs) |
| 76 | + UserBankAccount.objects.create( |
| 77 | + user=user, |
| 78 | + gender=gender, |
| 79 | + birth_date=birth_date, |
| 80 | + account_type=account_type, |
| 81 | + account_no=( |
| 82 | + user.id + |
| 83 | + settings.ACCOUNT_NUMBER_START_FROM |
| 84 | + ) |
| 85 | + ) |
| 86 | + return user |
0 commit comments