-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforms.py
More file actions
93 lines (68 loc) · 2.61 KB
/
forms.py
File metadata and controls
93 lines (68 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import datetime
from django import forms
from django.conf import settings
from .models import Transaction
class TransactionForm(forms.ModelForm):
class Meta:
model = Transaction
fields = [
'amount',
'transaction_type'
]
def __init__(self, *args, **kwargs):
self.account = kwargs.pop('account')
super().__init__(*args, **kwargs)
self.fields['transaction_type'].disabled = True
self.fields['transaction_type'].widget = forms.HiddenInput()
def save(self, commit=True):
self.instance.account = self.account
self.instance.balance_after_transaction = self.account.balance
return super().save()
class DepositForm(TransactionForm):
def clean_amount(self):
min_deposit_amount = settings.MINIMUM_DEPOSIT_AMOUNT
amount = self.cleaned_data.get('amount')
if amount < min_deposit_amount:
raise forms.ValidationError(
f'You need to deposit at least {min_deposit_amount} ₹'
)
return amount
class WithdrawForm(TransactionForm):
def clean_amount(self):
account = self.account
min_withdraw_amount = settings.MINIMUM_WITHDRAWAL_AMOUNT
max_withdraw_amount = (
account.account_type.maximum_withdrawal_amount
)
balance = account.balance
amount = self.cleaned_data.get('amount')
if amount < min_withdraw_amount:
raise forms.ValidationError(
f'You can withdraw at least {min_withdraw_amount} ₹'
)
if amount > max_withdraw_amount:
raise forms.ValidationError(
f'You can withdraw at most {max_withdraw_amount} ₹'
)
if amount > balance:
raise forms.ValidationError(
f'You have {balance} ₹ in your account. '
'You can not withdraw more than your account balance'
)
return amount
class TransactionDateRangeForm(forms.Form):
daterange = forms.CharField(required=False)
def clean_daterange(self):
daterange = self.cleaned_data.get("daterange")
print(daterange)
try:
daterange = daterange.split(' - ')
print(daterange)
if len(daterange) == 2:
for date in daterange:
datetime.datetime.strptime(date, '%Y-%m-%d')
return daterange
else:
raise forms.ValidationError("Please select a date range.")
except (ValueError, AttributeError):
raise forms.ValidationError("Invalid date range")