-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
134 lines (102 loc) · 3.75 KB
/
views.py
File metadata and controls
134 lines (102 loc) · 3.75 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from dateutil.relativedelta import relativedelta
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy
from django.utils import timezone
from django.views.generic import CreateView, ListView
from transactions.constants import DEPOSIT, WITHDRAWAL
from transactions.forms import (
DepositForm,
TransactionDateRangeForm,
WithdrawForm,
)
from transactions.models import Transaction
class TransactionRepostView(LoginRequiredMixin, ListView):
template_name = 'transactions/transaction_report.html'
model = Transaction
form_data = {}
def get(self, request, *args, **kwargs):
form = TransactionDateRangeForm(request.GET or None)
if form.is_valid():
self.form_data = form.cleaned_data
return super().get(request, *args, **kwargs)
def get_queryset(self):
queryset = super().get_queryset().filter(
account=self.request.user.account
)
daterange = self.form_data.get("daterange")
if daterange:
queryset = queryset.filter(timestamp__date__range=daterange)
return queryset.distinct()
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'account': self.request.user.account,
'form': TransactionDateRangeForm(self.request.GET or None)
})
return context
class TransactionCreateMixin(LoginRequiredMixin, CreateView):
template_name = 'transactions/transaction_form.html'
model = Transaction
title = ''
success_url = reverse_lazy('transactions:transaction_report')
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs.update({
'account': self.request.user.account
})
return kwargs
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'title': self.title
})
return context
class DepositMoneyView(TransactionCreateMixin):
form_class = DepositForm
title = 'Deposit Money to Your Account'
def get_initial(self):
initial = {'transaction_type': DEPOSIT}
return initial
def form_valid(self, form):
amount = form.cleaned_data.get('amount')
account = self.request.user.account
if not account.initial_deposit_date:
now = timezone.now()
next_interest_month = int(
12 / account.account_type.interest_calculation_per_year
)
account.initial_deposit_date = now
account.interest_start_date = (
now + relativedelta(
months=+next_interest_month
)
)
account.balance += amount
account.save(
update_fields=[
'initial_deposit_date',
'balance',
'interest_start_date'
]
)
messages.success(
self.request,
f'{amount}₹ was deposited to your account successfully'
)
return super().form_valid(form)
class WithdrawMoneyView(TransactionCreateMixin):
form_class = WithdrawForm
title = 'Withdraw Money from Your Account'
def get_initial(self):
initial = {'transaction_type': WITHDRAWAL}
return initial
def form_valid(self, form):
amount = form.cleaned_data.get('amount')
self.request.user.account.balance -= form.cleaned_data.get('amount')
self.request.user.account.save(update_fields=['balance'])
messages.success(
self.request,
f'Successfully withdrawn {amount}₹ from your account'
)
return super().form_valid(form)