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,5 +1,3 @@
import uuid
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponseBadRequest
from django.shortcuts import render
@@ -15,25 +13,23 @@ from core.views import (
ObjectDelete,
ObjectList,
ObjectNameMixin,
ObjectRead,
ObjectUpdate,
)
log = logs.get_logger(__name__)
class TradeAction(LoginRequiredMixin, OTPRequiredMixin, View):
allowed_types = ["modal", "widget", "window", "page"]
window_content = "window-content/trade.html"
class TradeAction(LoginRequiredMixin, OTPRequiredMixin, ObjectRead):
context_object_name_singular = "position"
context_object_name = "positions"
def get(self, request, type, trade_id):
"""
Get live information for a trade.
"""
if type not in self.allowed_types:
return HttpResponseBadRequest()
template_name = f"wm/{type}.html"
unique = str(uuid.uuid4())[:8]
db_info = Trade.get_by_id_or_order(trade_id, request.user)
detail_url_name = "trade_action"
detail_url_args = ["type", "trade_id"]
def get_object(self, **kwargs):
trade_id = kwargs.get("trade_id")
db_info = Trade.get_by_id_or_order(trade_id, self.request.user)
if db_info is None:
return HttpResponseBadRequest("Trade not found.")
if db_info.order_id is not None:
@@ -50,21 +46,12 @@ class TradeAction(LoginRequiredMixin, OTPRequiredMixin, View):
else:
live_info = {}
if type == "page":
type = "modal"
db_info = db_info.__dict__
del db_info["_state"]
del db_info["_original"]
context = {
"title": f"Trade info ({type})",
"unique": unique,
"window_content": self.window_content,
"type": type,
"db_info": db_info,
"live_info": live_info,
}
return render(request, template_name, context)
self.extra_context = {"live": live_info, "pretty": ["response"]}
return db_info
class TradeList(LoginRequiredMixin, OTPRequiredMixin, ObjectList):