Implement profit view and fix auto refresh
This commit is contained in:
@@ -153,7 +153,9 @@ class ObjectList(RestrictedViewMixin, ObjectNameMixin, ListView):
|
||||
|
||||
# Return partials for HTMX
|
||||
if self.request.htmx:
|
||||
if orig_type == "page":
|
||||
if request.headers["HX-Target"] == self.context_object_name + "-table":
|
||||
self.template_name = self.list_template
|
||||
elif orig_type == "page":
|
||||
self.template_name = self.list_template
|
||||
else:
|
||||
context["window_content"] = self.list_template
|
||||
|
||||
@@ -22,7 +22,7 @@ class AccountInfo(LoginRequiredMixin, OTPRequiredMixin, View):
|
||||
"api_key",
|
||||
"sandbox",
|
||||
"supported_symbols",
|
||||
"instruments",
|
||||
# "instruments",
|
||||
]
|
||||
allowed_types = ["modal", "widget", "window", "page"]
|
||||
window_content = "window-content/account-info.html"
|
||||
|
||||
@@ -61,6 +61,8 @@ class Positions(LoginRequiredMixin, OTPRequiredMixin, View):
|
||||
list_template = "partials/position-list.html"
|
||||
page_title = "Live positions from all exchanges"
|
||||
page_subtitle = "Manual trades are editable under 'Bot Trades' tab."
|
||||
context_object_name_singular = "position"
|
||||
context_object_name = "positions"
|
||||
|
||||
def get(self, request, type, account_id=None):
|
||||
if type not in self.allowed_types:
|
||||
@@ -89,12 +91,14 @@ class Positions(LoginRequiredMixin, OTPRequiredMixin, View):
|
||||
"page_title": self.page_title,
|
||||
"page_subtitle": self.page_subtitle,
|
||||
"list_url": list_url,
|
||||
"context_object_name_singular": "position",
|
||||
"context_object_name": "positions",
|
||||
"context_object_name_singular": self.context_object_name_singular,
|
||||
"context_object_name": self.context_object_name,
|
||||
}
|
||||
# Return partials for HTMX
|
||||
if self.request.htmx:
|
||||
if orig_type == "page":
|
||||
if request.headers["HX-Target"] == self.context_object_name + "-table":
|
||||
self.template_name = self.list_template
|
||||
elif orig_type == "page":
|
||||
self.template_name = self.list_template
|
||||
else:
|
||||
context["window_content"] = self.list_template
|
||||
|
||||
74
core/views/profit.py
Normal file
74
core/views/profit.py
Normal file
@@ -0,0 +1,74 @@
|
||||
import uuid
|
||||
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.http import HttpResponseBadRequest
|
||||
from django.shortcuts import render
|
||||
from django.urls import reverse
|
||||
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_profit(user):
|
||||
items = []
|
||||
accounts = Account.objects.filter(user=user)
|
||||
for account in accounts:
|
||||
try:
|
||||
details = account.client.get_account()
|
||||
pl = details["pl"]
|
||||
item = {"account": account, "pl": float(pl), "balance": details["balance"], "currency": details["currency"]}
|
||||
items.append(item)
|
||||
except GenericAPIError:
|
||||
continue
|
||||
return items
|
||||
|
||||
class Profit(LoginRequiredMixin, OTPRequiredMixin, View):
|
||||
allowed_types = ["modal", "widget", "window", "page"]
|
||||
window_content = "window-content/objects.html"
|
||||
list_template = "partials/profit-list.html"
|
||||
page_title = "Profit by account"
|
||||
page_subtitle = None
|
||||
context_object_name_singular = "profit"
|
||||
context_object_name = "profit"
|
||||
|
||||
def get(self, request, type):
|
||||
if type not in self.allowed_types:
|
||||
return HttpResponseBadRequest
|
||||
self.template_name = f"wm/{type}.html"
|
||||
unique = str(uuid.uuid4())[:8]
|
||||
items = get_profit(request.user)
|
||||
|
||||
orig_type = type
|
||||
if type == "page":
|
||||
type = "modal"
|
||||
cast = {
|
||||
"type": orig_type,
|
||||
}
|
||||
list_url = reverse("profit", kwargs={**cast})
|
||||
context = {
|
||||
"title": f"Profit ({type})",
|
||||
"unique": unique,
|
||||
"window_content": self.window_content,
|
||||
"list_template": self.list_template,
|
||||
"object_list": items,
|
||||
"type": type,
|
||||
"page_title": self.page_title,
|
||||
"page_subtitle": self.page_subtitle,
|
||||
"list_url": list_url,
|
||||
"context_object_name_singular": self.context_object_name_singular,
|
||||
"context_object_name": self.context_object_name,
|
||||
}
|
||||
# Return partials for HTMX
|
||||
if self.request.htmx:
|
||||
if request.headers["HX-Target"] == self.context_object_name+"-table":
|
||||
self.template_name = self.list_template
|
||||
elif orig_type == "page":
|
||||
self.template_name = self.list_template
|
||||
else:
|
||||
context["window_content"] = self.list_template
|
||||
return render(request, self.template_name, context)
|
||||
Reference in New Issue
Block a user