128 lines
3.7 KiB
Python
128 lines
3.7 KiB
Python
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
|
|
from core.views import (
|
|
ObjectCreate,
|
|
ObjectDelete,
|
|
ObjectList,
|
|
ObjectNameMixin,
|
|
ObjectUpdate,
|
|
)
|
|
|
|
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:
|
|
# Fix old data
|
|
if "id" in db_info.order_id:
|
|
db_response_id = db_info.order_id["id"]
|
|
if db_info.order_id == db_response_id:
|
|
db_info.order_id = str(int(db_info.order_id) + 1)
|
|
db_info.save()
|
|
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"
|
|
db_info = db_info.__dict__
|
|
del db_info["_state"]
|
|
del db_info["_original"]
|
|
context = {
|
|
"title": f"Trade info ({type})",
|
|
"unique": unique,
|
|
"window_content": self.window_content,
|
|
"type": type,
|
|
"db_info": db_info,
|
|
"live_info": live_info,
|
|
}
|
|
|
|
return render(request, template_name, context)
|
|
|
|
|
|
class TradeList(LoginRequiredMixin, OTPRequiredMixin, ObjectList):
|
|
list_template = "partials/trade-list.html"
|
|
model = Trade
|
|
page_title = (
|
|
"List of bot and manual trades. This may not reflect actual live trades."
|
|
)
|
|
page_subtitle = "Trades deleted here will not be closed on the exchange."
|
|
|
|
list_url_name = "trades"
|
|
list_url_args = ["type"]
|
|
|
|
submit_url_name = "trade_create"
|
|
|
|
delete_all_url_name = "trade_delete_all"
|
|
|
|
|
|
class TradeCreate(LoginRequiredMixin, OTPRequiredMixin, ObjectCreate):
|
|
model = Trade
|
|
form_class = TradeForm
|
|
list_url_name = "trades"
|
|
list_url_args = ["type"]
|
|
|
|
submit_url_name = "trade_create"
|
|
|
|
def post_save(self, obj):
|
|
obj.post()
|
|
log.debug(f"Posting trade {obj}")
|
|
|
|
|
|
class TradeUpdate(LoginRequiredMixin, OTPRequiredMixin, ObjectUpdate):
|
|
model = Trade
|
|
form_class = TradeForm
|
|
list_url_name = "trades"
|
|
list_url_args = ["type"]
|
|
|
|
submit_url_name = "trade_update"
|
|
|
|
|
|
class TradeDelete(LoginRequiredMixin, OTPRequiredMixin, ObjectDelete):
|
|
model = Trade
|
|
|
|
list_url_name = "trades"
|
|
list_url_args = ["type"]
|
|
|
|
|
|
class TradeDeleteAll(LoginRequiredMixin, OTPRequiredMixin, ObjectNameMixin, View):
|
|
template_name = "partials/notify.html"
|
|
model = Trade
|
|
|
|
def delete(self, request):
|
|
"""
|
|
Delete all trades by the current user
|
|
"""
|
|
Trade.objects.filter(user=request.user).delete()
|
|
context = {"message": "All trades deleted", "class": "success"}
|
|
response = render(request, self.template_name, context)
|
|
response["HX-Trigger"] = f"{self.context_object_name_singular}Event"
|
|
return response
|