fisk/core/views/positions.py

86 lines
2.6 KiB
Python
Raw Normal View History

2022-10-17 06:20:30 +00:00
import uuid
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponseBadRequest
2022-10-17 06:20:30 +00:00
from django.shortcuts import render
from django.views import View
from rest_framework.parsers import FormParser
2022-10-17 06:20:30 +00:00
from core.exchanges import GenericAPIError
from core.models import Account
2022-10-17 06:20:30 +00:00
from core.util import logs
2022-10-21 23:15:27 +00:00
2022-10-17 06:20:30 +00:00
log = logs.get_logger(__name__)
def get_positions(user, account_id=None):
items = []
accounts = Account.objects.filter(user=user)
for account in accounts:
try:
positions = account.client.get_all_positions()
except GenericAPIError:
continue
2022-10-21 23:15:27 +00:00
for item in positions:
items.append(item)
2022-10-21 23:15:27 +00:00
return items
2022-10-17 06:20:30 +00:00
class Positions(LoginRequiredMixin, View):
allowed_types = ["modal", "widget", "window", "page"]
window_content = "window-content/objects.html"
list_template = "partials/position-list.html"
page_title = "Live positions from all exchanges"
page_subtitle = "Manual trades are editable under 'Bot Trades' tab."
2022-10-17 06:20:30 +00:00
async def get(self, request, type, account_id=None):
if type not in self.allowed_types:
return HttpResponseBadRequest
template_name = f"wm/{type}.html"
unique = str(uuid.uuid4())[:8]
items = get_positions(request.user, account_id)
if type == "page":
type = "modal"
context = {
"title": f"Positions ({type})",
2022-10-17 06:20:30 +00:00
"unique": unique,
"window_content": self.window_content,
"list_template": self.list_template,
2022-10-17 06:20:30 +00:00
"items": items,
"type": type,
"page_title": self.page_title,
"page_subtitle": self.page_subtitle,
2022-10-17 06:20:30 +00:00
}
2022-10-21 23:15:27 +00:00
return render(request, template_name, context)
class PositionAction(LoginRequiredMixin, View):
allowed_types = ["modal", "widget", "window", "page"]
window_content = "window-content/view-position.html"
parser_classes = [FormParser]
2022-11-02 18:24:56 +00:00
async 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]
account = Account.get_by_id(account_id, request.user)
2022-11-04 07:20:55 +00:00
info = account.client.get_position_info(symbol)
if type == "page":
type = "modal"
context = {
"title": f"Position info ({type})",
"unique": unique,
"window_content": self.window_content,
"type": type,
"items": info,
}
2022-11-04 07:20:55 +00:00
return render(request, template_name, context)