import uuid import orjson from django.contrib.auth.mixins import LoginRequiredMixin from django.http import HttpResponseBadRequest from django.shortcuts import render from django.views import View from core.forms import AccountForm from core.models import Account from core.util import logs from core.views import ObjectCreate, ObjectDelete, ObjectList, ObjectUpdate log = logs.get_logger(__name__) class AccountInfo(LoginRequiredMixin, View): VIEWABLE_FIELDS_MODEL = [ "name", "exchange", "currency", "api_key", "sandbox", "supported_symbols", "instruments", ] allowed_types = ["modal", "widget", "window", "page"] window_content = "window-content/account-info.html" def get(self, request, type, pk): """ Get the account details. :param account_id: The id of the account. """ if type not in self.allowed_types: return HttpResponseBadRequest template_name = f"wm/{type}.html" unique = str(uuid.uuid4())[:8] try: account = Account.get_by_id(pk, request.user) 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) live_info = account.client.get_account() live_info = live_info account_info = account.__dict__ account_info = { k: v for k, v in account_info.items() if k in self.VIEWABLE_FIELDS_MODEL } account_info["supported_symbols"] = ", ".join(account_info["supported_symbols"]) if type == "page": type = "modal" context = { "db_info": account_info, "live_info": live_info, "pk": pk, "type": type, "unique": unique, "window_content": self.window_content, } return render(request, template_name, context) class AccountList(LoginRequiredMixin, ObjectList): list_template = "partials/account-list.html" model = Account page_title = "List of accounts" list_url_name = "accounts" list_url_args = ["type"] submit_url_name = "account_create" class AccountCreate(LoginRequiredMixin, ObjectCreate): model = Account form_class = AccountForm list_url_name = "accounts" list_url_args = ["type"] submit_url_name = "account_create" # class AccountRead(LoginRequiredMixin, ObjectRead): # model = Account # context_object_name = "accounts" # submit_url_name = "account_read" # fields = ( # "name", # "exchange", # "api_key", # "api_secret", # "sandbox", # ) class AccountUpdate(LoginRequiredMixin, ObjectUpdate): model = Account form_class = AccountForm list_url_name = "accounts" list_url_args = ["type"] submit_url_name = "account_update" class AccountDelete(LoginRequiredMixin, ObjectDelete): model = Account list_url_name = "accounts" list_url_args = ["type"]