175 lines
6.5 KiB
Python
175 lines
6.5 KiB
Python
from django.shortcuts import get_object_or_404, render, redirect
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
from django.urls import reverse_lazy
|
|
from accounts import resource_action
|
|
from django.views import generic
|
|
from django.contrib import messages
|
|
from goodtimes import constants
|
|
from manage_wallets.forms import PrincipalBankAccountVerificationForm
|
|
from .models import PrincipalBankAccount, Wallet, Transaction, WithdrawalRequest
|
|
from django.db import transaction
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
"""Wallet Related VIew"""
|
|
|
|
|
|
class WalletListView(LoginRequiredMixin, generic.TemplateView):
|
|
page_name = resource_action.RESOURCE_MANAGE_WALLET
|
|
template_name = "manage_wallets/wallet_list.html"
|
|
model = Wallet
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
context["page_name"] = self.page_name
|
|
context["wallet_objs"] = Wallet.objects.filter(deleted=False)
|
|
return context
|
|
|
|
|
|
"""Payment Related View"""
|
|
|
|
|
|
class PaymentListView(LoginRequiredMixin, generic.TemplateView):
|
|
page_name = resource_action.RESOURCE_MANAGE_PAYMENT
|
|
template_name = "manage_wallets/payment_list.html"
|
|
model = Transaction
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
context["page_name"] = self.page_name
|
|
context["transaction_objs"] = Transaction.objects.filter(deleted=False)
|
|
return context
|
|
|
|
|
|
"""Withdrawals Related View"""
|
|
|
|
|
|
class WithdrawalListView(LoginRequiredMixin, generic.TemplateView):
|
|
page_name = resource_action.RESOURCE_MANAGE_WITHDRAWALS
|
|
template_name = "manage_wallets/withdrawal_list.html"
|
|
model = WithdrawalRequest
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
context["page_name"] = self.page_name
|
|
context["withdrawal_objs"] = WithdrawalRequest.objects.filter(deleted=False)
|
|
return context
|
|
|
|
|
|
"""Bank Accounts Related View"""
|
|
|
|
|
|
class BankAccountListView(LoginRequiredMixin, generic.TemplateView):
|
|
page_name = resource_action.RESOURCE_MANAGE_BANK_ACCOUNTS
|
|
template_name = "manage_wallets/account_list.html"
|
|
model = PrincipalBankAccount
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
context["page_name"] = self.page_name
|
|
context["account_objs"] = PrincipalBankAccount.objects.filter(deleted=False)
|
|
return context
|
|
|
|
|
|
class StatusUpdateView(LoginRequiredMixin, generic.View):
|
|
page_name = resource_action.RESOURCE_MANAGE_WITHDRAWALS
|
|
model = WithdrawalRequest
|
|
success_message = constants.DATA_SAVED
|
|
success_url = reverse_lazy("manage_wallets:withdrawal_list")
|
|
|
|
def post(self, request):
|
|
id = request.POST.get("id")
|
|
message = request.POST.get("message")
|
|
status = request.POST.get("status")
|
|
|
|
print("Request.POST: ", request.POST)
|
|
if id or message:
|
|
try:
|
|
with transaction.atomic():
|
|
instance = self.model.objects.get(id=id)
|
|
logger.debug("Updating status for ID %s to %s", id, status)
|
|
print("status: ", status)
|
|
if status == "transferred":
|
|
bank_accounts = PrincipalBankAccount.objects.filter(principal=instance.principal)
|
|
print("bank_accounts: ", bank_accounts)
|
|
for account in bank_accounts:
|
|
account.delete()
|
|
print("Deleted all accounts")
|
|
instance.reply = message
|
|
instance.status = status
|
|
instance.save()
|
|
messages.success(request, self.success_message)
|
|
except self.model.DoesNotExist:
|
|
messages.error(request, "Contact Us entry not found")
|
|
except Exception as e:
|
|
messages.error(request, str(e))
|
|
else:
|
|
messages.error(request, "Missing 'id' or 'message' in the request")
|
|
|
|
# Redirect to the desired URL after form submission
|
|
return redirect(self.success_url)
|
|
|
|
|
|
class BankAccountCreateOrUpdateView(LoginRequiredMixin, generic.View):
|
|
# Set the page_name and resource
|
|
page_name = resource_action.RESOURCE_MANAGE_BANK_ACCOUNTS
|
|
resource = resource_action.RESOURCE_MANAGE_BANK_ACCOUNTS
|
|
|
|
# Initialize the action as ACTION_CREATE (can change based on logic)
|
|
action = resource_action.ACTION_CREATE # Default action
|
|
|
|
template_name = "manage_wallets/bank_account_edit.html"
|
|
model = PrincipalBankAccount
|
|
form_class = PrincipalBankAccountVerificationForm
|
|
success_url = reverse_lazy("manage_wallets:account_list")
|
|
error_message = "An error occurred while saving the data."
|
|
|
|
# Determine the success message dynamically based on whether it's an update or create
|
|
def get_success_message(self):
|
|
self.success_message = (
|
|
constants.RECORD_CREATED if not self.object else constants.RECORD_UPDATED
|
|
)
|
|
return self.success_message
|
|
|
|
# Get the object (if exists) based on URL parameter 'pk'
|
|
def get_object(self):
|
|
pk = self.kwargs.get("pk")
|
|
return get_object_or_404(self.model, pk=pk) if pk else None
|
|
|
|
# Add page_name and operation to the context
|
|
def get_context_data(self, **kwargs):
|
|
context = {
|
|
"page_name": self.page_name,
|
|
"operation": "Add" if not self.object else "Edit",
|
|
}
|
|
context.update(kwargs) # Include any additional context data passed to the view
|
|
return context
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
self.object = self.get_object()
|
|
|
|
# If an object is found, change action to ACTION_UPDATE
|
|
if self.object is not None:
|
|
self.action = resource_action.ACTION_UPDATE
|
|
|
|
form = self.form_class(instance=self.object)
|
|
context = self.get_context_data(form=form)
|
|
return render(request, self.template_name, context=context)
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
self.object = self.get_object()
|
|
|
|
# If an object is found, change action to ACTION_UPDATE
|
|
if self.object is not None:
|
|
self.action = resource_action.ACTION_UPDATE
|
|
|
|
form = self.form_class(request.POST, instance=self.object)
|
|
if not form.is_valid():
|
|
print(form.errors)
|
|
context = self.get_context_data(form=form)
|
|
return render(request, self.template_name, context=context)
|
|
|
|
form.save()
|
|
messages.success(self.request, self.get_success_message())
|
|
return redirect(self.success_url) |