Use ObjectRead helper for all list and detail views

This commit is contained in:
2022-12-08 07:20:46 +00:00
parent 1e85e830b2
commit 8840b04059
12 changed files with 321 additions and 354 deletions

View File

@@ -1,20 +1,21 @@
import uuid
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponseBadRequest
from django.shortcuts import render
from django.views import View
from two_factor.views.mixins import OTPRequiredMixin
from core.forms import AccountForm
from core.models import Account
from core.util import logs
from core.views import ObjectCreate, ObjectDelete, ObjectList, ObjectUpdate
from core.views import ObjectCreate, ObjectDelete, ObjectList, ObjectRead, ObjectUpdate
log = logs.get_logger(__name__)
class AccountInfo(LoginRequiredMixin, OTPRequiredMixin, View):
class AccountInfo(LoginRequiredMixin, OTPRequiredMixin, ObjectRead):
context_object_name_singular = "account"
context_object_name = "accounts"
detail_url_name = "account_info"
detail_url_args = ["type", "pk"]
VIEWABLE_FIELDS_MODEL = [
"name",
"exchange",
@@ -24,20 +25,11 @@ class AccountInfo(LoginRequiredMixin, OTPRequiredMixin, View):
"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]
def get_object(self, **kwargs):
pk = kwargs.get("pk")
try:
account = Account.get_by_id(pk, request.user)
account = Account.get_by_id(pk, self.request.user)
except Account.DoesNotExist:
message = "Account does not exist"
message_class = "danger"
@@ -46,8 +38,7 @@ class AccountInfo(LoginRequiredMixin, OTPRequiredMixin, View):
"message_class": message_class,
"window_content": self.window_content,
}
return render(request, template_name, context)
return self.render_to_response(context)
live_info = account.client.get_account()
live_info = live_info
account_info = account.__dict__
@@ -56,18 +47,8 @@ class AccountInfo(LoginRequiredMixin, OTPRequiredMixin, View):
}
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)
self.extra_context = {"live": live_info}
return account_info
class AccountList(LoginRequiredMixin, OTPRequiredMixin, ObjectList):
@@ -91,19 +72,6 @@ class AccountCreate(LoginRequiredMixin, OTPRequiredMixin, ObjectCreate):
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, OTPRequiredMixin, ObjectUpdate):
model = Account
form_class = AccountForm