import uuid import orjson from django.contrib.auth.mixins import LoginRequiredMixin from django.http import HttpResponse, HttpResponseBadRequest from django.shortcuts import render from django.views import View from rest_framework.parsers import FormParser, JSONParser from rest_framework.views import APIView from serde import ValidationError from core.forms import AccountForm from core.lib.serde import drakdoo from core.models import Callback, Account from core.util import logs log = logs.get_logger(__name__) def get_accounts(user): accounts = Account.objects.filter(user=user) return accounts class Accounts(LoginRequiredMixin, View): allowed_types = ["modal", "widget", "window", "page"] window_content = "window-content/accounts.html" async def get(self, request, type): if type not in self.allowed_types: return HttpResponseBadRequest template_name = f"wm/{type}.html" unique = str(uuid.uuid4())[:8] accounts = get_accounts(request.user) if type == "page": type = "modal" context = { "title": f"Accounts ({type})", "unique": unique, "window_content": self.window_content, "items": accounts, "type": type, } return render(request, template_name, context) class AccountAction(LoginRequiredMixin, APIView): allowed_types = ["modal", "widget", "window", "page"] window_content = "window-content/add-account.html" parser_classes = [FormParser] def get(self, request, type, account_id=None): """ Get the form for adding or editing a account. :param account_id: The id of the account to edit. Optional. """ if type not in self.allowed_types: return HttpResponseBadRequest template_name = f"wm/{type}.html" unique = str(uuid.uuid4())[:8] if account_id: try: account = Account.objects.get(id=account_id, user=request.user) form = AccountForm(instance=account) except Account.DoesNotExist: message = "Account does not exist" message_class = "danger" context = { "message": message, "message_class": message_class, "window_content": self.window_content, } return render(request, template_name, context) else: form = AccountForm() if type == "page": type = "modal" context = { "form": form, "account_id": account_id, "type": type, "unique": unique, "window_content": self.window_content, } return render(request, template_name, context) def put(self, request, type, account_id=None): """ Add or edit a account. :param account_id: The id of the account to edit. Optional. """ if type not in self.allowed_types: return HttpResponseBadRequest message = None message_class = "success" if account_id: try: form = AccountForm(request.data, instance=account.objects.get(id=account_id)) except account.DoesNotExist: message = "Account does not exist" message_class = "danger" context = { "message": message, "class": message_class, } return render(request, self.template_name, context) else: form = AccountForm(request.data) if form.is_valid(): account = form.save(commit=False) account.user = request.user account.save() if account_id: message = f"Account {account_id} edited successfully" else: message = f"Account {account.id} added successfully" else: message = "Error adding account" message_class = "danger" accounts = get_accounts(request.user) context = { "items": accounts, "type": type, } if message: context["message"] = message context["class"] = message_class template_name = "partials/account-list.html" return render(request, template_name, context) def delete(self, request, type, account_id): """ Delete a account. :param account_id: The id of the account to delete. """ if type not in self.allowed_types: return HttpResponseBadRequest message = None message_class = "success" try: account = Account.objects.get(id=account_id, user=request.user) account.delete() message = "Account deleted successfully" except Account.DoesNotExist: message = "Error deleting account" message_class = "danger" accounts = get_accounts(request.user) context = { "items": accounts, "type": type, } if message: context["message"] = message context["class"] = message_class template_name = "partials/account-list.html" return render(request, template_name, context)