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,16 +1,11 @@
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 rest_framework.parsers import FormParser
from two_factor.views.mixins import OTPRequiredMixin
from core.exchanges import GenericAPIError
from core.models import Account, Trade
from core.util import logs
from core.views import ObjectList
from core.views import ObjectList, ObjectRead
log = logs.get_logger(__name__)
@@ -71,42 +66,27 @@ class Positions(LoginRequiredMixin, OTPRequiredMixin, ObjectList):
return items
class PositionAction(LoginRequiredMixin, OTPRequiredMixin, View):
allowed_types = ["modal", "widget", "window", "page"]
window_content = "window-content/view-position.html"
parser_classes = [FormParser]
class PositionAction(LoginRequiredMixin, OTPRequiredMixin, ObjectRead):
detail_template = "partials/position-detail.html"
def get(self, request, type, account_id, symbol):
"""
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]
context_object_name_singular = "position"
context_object_name = "positions"
account = Account.get_by_id(account_id, request.user)
detail_url_name = "position_action"
detail_url_args = ["type", "account_id", "symbol"]
def get_object(self, **kwargs):
account_id = kwargs.get("account_id")
symbol = kwargs.get("symbol")
account = Account.get_by_id(account_id, self.request.user)
info = account.client.get_position_info(symbol)
valid_trade_ids = list(
annotate_positions([info], request.user, return_order_ids=True)
)
# Remove some fields from the info dict
del info["long"]
del info["short"]
if type == "page":
type = "modal"
context = {
"title": f"Position info ({type})",
"unique": unique,
"window_content": self.window_content,
"type": type,
"items": info,
"valid_trade_ids": valid_trade_ids,
}
return render(request, template_name, context)
valid_trade_ids = list(
annotate_positions([info], self.request.user, return_order_ids=True)
)
self.extra_context = {"valid_trade_ids": valid_trade_ids}
return info
def delete(self, request, account_id, side, symbol):
"""