১৪.৫ : ডিপোজিট ভিউ
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy
from django.views.generic import CreateView, ListView
from transactions.constants import DEPOSIT, WITHDRAWAL,LOAN, LOAN_PAID
from transactions.forms import (
DepositForm,
WithdrawForm,
LoanRequestForm,
)
from transactions.models import Transaction
class TransactionCreateMixin(LoginRequiredMixin, CreateView):
template_name = 'transactions/transaction_form.html'
model = Transaction
title = ''
success_url = reverse_lazy('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) # template e context data pass kora
context.update({
'title': self.title
})
return context
class DepositMoneyView(TransactionCreateMixin):
form_class = DepositForm
title = 'Deposit Form'
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
account.balance += amount # amount = 200, tar ager balance = 0 taka new balance = 0+200 = 200
account.save(
update_fields=[
'balance'
]
)
messages.success(
self.request,
f'{"{:,.2f}".format(float(amount))}$ was deposited to your account successfully'
)
return super().form_valid(form)Last updated