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 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 for item in positions: items.append(item) 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" list_template = "partials/position-list.html" page_title = "Live positions from all exchanges" page_subtitle = "Manual trades are editable under 'Bot Trades' tab." 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) annotate_positions(items, request.user, return_order_ids=False) if type == "page": type = "modal" context = { "title": f"Positions ({type})", "unique": unique, "window_content": self.window_content, "list_template": self.list_template, "items": items, "type": type, "page_title": self.page_title, "page_subtitle": self.page_subtitle, } return render(request, template_name, context) class PositionAction(LoginRequiredMixin, OTPRequiredMixin, View): allowed_types = ["modal", "widget", "window", "page"] window_content = "window-content/view-position.html" parser_classes = [FormParser] 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) 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" 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)