62 lines
2.2 KiB
Python
62 lines
2.2 KiB
Python
|
import uuid
|
||
|
|
||
|
import orjson
|
||
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
||
|
from django.http import HttpResponse, HttpResponseBadRequest
|
||
|
from django.shortcuts import render
|
||
|
from django.views import View
|
||
|
from rest_framework.parsers import FormParser, JSONParser
|
||
|
from rest_framework.views import APIView
|
||
|
from serde import ValidationError
|
||
|
|
||
|
from core.forms import HookForm
|
||
|
from core.lib.serde import drakdoo_s
|
||
|
from core.models import Callback, Hook, Account
|
||
|
from core.util import logs
|
||
|
import ccxt
|
||
|
from ccxt.base.errors import NotSupported
|
||
|
log = logs.get_logger(__name__)
|
||
|
|
||
|
|
||
|
def get_positions(user, account_id=None):
|
||
|
items = []
|
||
|
accounts = Account.objects.filter(user=user)
|
||
|
for account in accounts:
|
||
|
if hasattr(ccxt, account.exchange):
|
||
|
instance = getattr(ccxt, account.exchange)({"apiKey": account.api_key, "secret": account.api_secret})
|
||
|
if account.sandbox:
|
||
|
instance.set_sandbox_mode(True)
|
||
|
try:
|
||
|
positions = instance.fetch_positions()
|
||
|
except NotSupported:
|
||
|
positions = [{"account": account.exchange, "error": "Not supported"}]
|
||
|
print("POSITIONS", positions)
|
||
|
# try:
|
||
|
# parsed = ccxt_s.CCXTRoot.from_dict(order)
|
||
|
# except ValidationError as e:
|
||
|
# log.error(f"Error creating trade: {e}")
|
||
|
# return False
|
||
|
# self.status = parsed.status
|
||
|
# self.response = order
|
||
|
|
||
|
|
||
|
class Positions(LoginRequiredMixin, View):
|
||
|
allowed_types = ["modal", "widget", "window", "page"]
|
||
|
window_content = "window-content/positions.html"
|
||
|
|
||
|
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"Hooks ({type})",
|
||
|
"unique": unique,
|
||
|
"window_content": self.window_content,
|
||
|
"items": items,
|
||
|
"type": type,
|
||
|
}
|
||
|
return render(request, template_name, context)
|