Improve navigating trades and positions by cross-linking
This commit is contained in:
@@ -227,7 +227,9 @@ class ObjectCreate(RestrictedViewMixin, ObjectNameMixin, CreateView):
|
||||
context["submit_url"] = submit_url
|
||||
context["list_url"] = list_url
|
||||
context["type"] = type
|
||||
return self.render_to_response(context)
|
||||
response = self.render_to_response(context)
|
||||
# response["HX-Trigger"] = f"{self.context_object_name_singular}Event"
|
||||
return response
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
self.request = request
|
||||
@@ -298,7 +300,9 @@ class ObjectUpdate(RestrictedViewMixin, ObjectNameMixin, UpdateView):
|
||||
context["context_object_name_singular"] = self.context_object_name_singular
|
||||
context["submit_url"] = submit_url
|
||||
context["type"] = type
|
||||
return self.render_to_response(context)
|
||||
response = self.render_to_response(context)
|
||||
# response["HX-Trigger"] = f"{self.context_object_name_singular}Event"
|
||||
return response
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
self.request = request
|
||||
|
||||
@@ -8,7 +8,7 @@ from rest_framework.parsers import FormParser
|
||||
from two_factor.views.mixins import OTPRequiredMixin
|
||||
|
||||
from core.exchanges import GenericAPIError
|
||||
from core.models import Account
|
||||
from core.models import Account, Trade
|
||||
from core.util import logs
|
||||
|
||||
log = logs.get_logger(__name__)
|
||||
@@ -28,6 +28,32 @@ def get_positions(user, account_id=None):
|
||||
return items
|
||||
|
||||
|
||||
def annotate_positions(positions, user, return_order_ids=False):
|
||||
"""
|
||||
Annotate positions with trade information.
|
||||
If return_order_ids is True, yield a list of order_ids instead of a list of trades.
|
||||
:param positions: list of positions
|
||||
:param user: user
|
||||
:param return_order_ids: whether to return a generator of order_ids
|
||||
:return: None or list of order_ids
|
||||
:rtype: None or generator
|
||||
"""
|
||||
for item in positions:
|
||||
try:
|
||||
if "trade_ids" in item:
|
||||
if return_order_ids:
|
||||
for trade_id in Trade.objects.filter(
|
||||
user=user, order_id__in=item["trade_ids"]
|
||||
):
|
||||
yield trade_id.order_id
|
||||
else:
|
||||
item["trades"] = Trade.objects.filter(
|
||||
user=user, order_id__in=item["trade_ids"]
|
||||
)
|
||||
except Trade.DoesNotExist:
|
||||
pass
|
||||
|
||||
|
||||
class Positions(LoginRequiredMixin, OTPRequiredMixin, View):
|
||||
allowed_types = ["modal", "widget", "window", "page"]
|
||||
window_content = "window-content/objects.html"
|
||||
@@ -41,6 +67,7 @@ class Positions(LoginRequiredMixin, OTPRequiredMixin, View):
|
||||
template_name = f"wm/{type}.html"
|
||||
unique = str(uuid.uuid4())[:8]
|
||||
items = get_positions(request.user, account_id)
|
||||
annotate_positions(items, request.user, return_order_ids=False)
|
||||
if type == "page":
|
||||
type = "modal"
|
||||
context = {
|
||||
@@ -66,12 +93,15 @@ class PositionAction(LoginRequiredMixin, OTPRequiredMixin, View):
|
||||
Get live information for a trade.
|
||||
"""
|
||||
if type not in self.allowed_types:
|
||||
return HttpResponseBadRequest
|
||||
return HttpResponseBadRequest()
|
||||
template_name = f"wm/{type}.html"
|
||||
unique = str(uuid.uuid4())[:8]
|
||||
|
||||
account = Account.get_by_id(account_id, request.user)
|
||||
info = account.client.get_position_info(symbol)
|
||||
valid_trade_ids = list(
|
||||
annotate_positions([info], request.user, return_order_ids=True)
|
||||
)
|
||||
|
||||
if type == "page":
|
||||
type = "modal"
|
||||
@@ -81,6 +111,7 @@ class PositionAction(LoginRequiredMixin, OTPRequiredMixin, View):
|
||||
"window_content": self.window_content,
|
||||
"type": type,
|
||||
"items": info,
|
||||
"valid_trade_ids": valid_trade_ids,
|
||||
}
|
||||
|
||||
return render(request, template_name, context)
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
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.exchanges import GenericAPIError
|
||||
from core.forms import TradeForm
|
||||
from core.models import Trade
|
||||
from core.util import logs
|
||||
@@ -17,6 +21,44 @@ from core.views import (
|
||||
log = logs.get_logger(__name__)
|
||||
|
||||
|
||||
class TradeAction(LoginRequiredMixin, OTPRequiredMixin, View):
|
||||
allowed_types = ["modal", "widget", "window", "page"]
|
||||
window_content = "window-content/trade.html"
|
||||
|
||||
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)
|
||||
if db_info is None:
|
||||
return HttpResponseBadRequest("Trade not found.")
|
||||
if db_info.order_id is not None:
|
||||
try:
|
||||
live_info = db_info.account.client.get_trade(db_info.order_id)
|
||||
except GenericAPIError as e:
|
||||
live_info = {"error": e}
|
||||
else:
|
||||
live_info = {}
|
||||
|
||||
if type == "page":
|
||||
type = "modal"
|
||||
context = {
|
||||
"title": f"Trade info ({type})",
|
||||
"unique": unique,
|
||||
"window_content": self.window_content,
|
||||
"type": type,
|
||||
"db_info": db_info.__dict__,
|
||||
"live_info": live_info,
|
||||
}
|
||||
|
||||
return render(request, template_name, context)
|
||||
|
||||
|
||||
class TradeList(LoginRequiredMixin, OTPRequiredMixin, ObjectList):
|
||||
list_template = "partials/trade-list.html"
|
||||
model = Trade
|
||||
|
||||
Reference in New Issue
Block a user