2022-10-17 17:56:16 +00:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2022-11-29 07:20:39 +00:00
|
|
|
from django.http import HttpResponseBadRequest
|
2022-10-30 10:57:41 +00:00
|
|
|
from django.shortcuts import render
|
|
|
|
from django.views import View
|
2022-11-28 19:45:22 +00:00
|
|
|
from two_factor.views.mixins import OTPRequiredMixin
|
2022-10-17 17:56:16 +00:00
|
|
|
|
2022-11-29 07:20:39 +00:00
|
|
|
from core.exchanges import GenericAPIError
|
2022-10-17 17:56:16 +00:00
|
|
|
from core.forms import TradeForm
|
2022-10-29 11:43:13 +00:00
|
|
|
from core.models import Trade
|
2022-10-17 17:56:16 +00:00
|
|
|
from core.util import logs
|
2022-11-04 07:20:42 +00:00
|
|
|
from core.views import (
|
|
|
|
ObjectCreate,
|
|
|
|
ObjectDelete,
|
|
|
|
ObjectList,
|
|
|
|
ObjectNameMixin,
|
2022-12-08 07:20:46 +00:00
|
|
|
ObjectRead,
|
2022-11-04 07:20:42 +00:00
|
|
|
ObjectUpdate,
|
|
|
|
)
|
2022-10-17 17:56:16 +00:00
|
|
|
|
|
|
|
log = logs.get_logger(__name__)
|
|
|
|
|
|
|
|
|
2022-12-08 07:20:46 +00:00
|
|
|
class TradeAction(LoginRequiredMixin, OTPRequiredMixin, ObjectRead):
|
|
|
|
context_object_name_singular = "position"
|
|
|
|
context_object_name = "positions"
|
|
|
|
|
|
|
|
detail_url_name = "trade_action"
|
|
|
|
detail_url_args = ["type", "trade_id"]
|
2022-11-29 07:20:39 +00:00
|
|
|
|
2022-12-08 07:20:46 +00:00
|
|
|
def get_object(self, **kwargs):
|
|
|
|
trade_id = kwargs.get("trade_id")
|
|
|
|
db_info = Trade.get_by_id_or_order(trade_id, self.request.user)
|
2022-11-29 07:20:39 +00:00
|
|
|
if db_info is None:
|
|
|
|
return HttpResponseBadRequest("Trade not found.")
|
|
|
|
if db_info.order_id is not None:
|
|
|
|
try:
|
2022-11-29 07:20:39 +00:00
|
|
|
# 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()
|
2022-11-29 07:20:39 +00:00
|
|
|
live_info = db_info.account.client.get_trade(db_info.order_id)
|
|
|
|
except GenericAPIError as e:
|
|
|
|
live_info = {"error": e}
|
|
|
|
else:
|
|
|
|
live_info = {}
|
|
|
|
|
2022-12-02 07:20:37 +00:00
|
|
|
db_info = db_info.__dict__
|
|
|
|
del db_info["_state"]
|
|
|
|
del db_info["_original"]
|
2022-12-08 07:20:46 +00:00
|
|
|
|
|
|
|
self.extra_context = {"live": live_info, "pretty": ["response"]}
|
|
|
|
return db_info
|
2022-11-29 07:20:39 +00:00
|
|
|
|
|
|
|
|
2022-11-28 19:45:22 +00:00
|
|
|
class TradeList(LoginRequiredMixin, OTPRequiredMixin, ObjectList):
|
2022-10-29 11:43:13 +00:00
|
|
|
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."
|
2022-10-17 17:56:16 +00:00
|
|
|
|
2022-10-29 13:05:01 +00:00
|
|
|
list_url_name = "trades"
|
|
|
|
list_url_args = ["type"]
|
|
|
|
|
2022-10-29 11:43:13 +00:00
|
|
|
submit_url_name = "trade_create"
|
2022-10-17 17:56:16 +00:00
|
|
|
|
2022-10-30 10:57:41 +00:00
|
|
|
delete_all_url_name = "trade_delete_all"
|
|
|
|
|
2022-10-17 17:56:16 +00:00
|
|
|
|
2022-11-28 19:45:22 +00:00
|
|
|
class TradeCreate(LoginRequiredMixin, OTPRequiredMixin, ObjectCreate):
|
2022-10-29 11:43:13 +00:00
|
|
|
model = Trade
|
|
|
|
form_class = TradeForm
|
2022-10-29 13:05:01 +00:00
|
|
|
list_url_name = "trades"
|
|
|
|
list_url_args = ["type"]
|
|
|
|
|
2022-10-29 11:43:13 +00:00
|
|
|
submit_url_name = "trade_create"
|
2022-10-17 17:56:16 +00:00
|
|
|
|
2022-10-30 10:57:41 +00:00
|
|
|
def post_save(self, obj):
|
|
|
|
obj.post()
|
|
|
|
log.debug(f"Posting trade {obj}")
|
|
|
|
|
2022-10-17 17:56:16 +00:00
|
|
|
|
2022-11-28 19:45:22 +00:00
|
|
|
class TradeUpdate(LoginRequiredMixin, OTPRequiredMixin, ObjectUpdate):
|
2022-10-29 11:43:13 +00:00
|
|
|
model = Trade
|
|
|
|
form_class = TradeForm
|
2022-10-29 13:05:01 +00:00
|
|
|
list_url_name = "trades"
|
|
|
|
list_url_args = ["type"]
|
|
|
|
|
2022-10-29 11:43:13 +00:00
|
|
|
submit_url_name = "trade_update"
|
2022-10-17 17:56:16 +00:00
|
|
|
|
|
|
|
|
2022-11-28 19:45:22 +00:00
|
|
|
class TradeDelete(LoginRequiredMixin, OTPRequiredMixin, ObjectDelete):
|
2022-10-29 11:43:13 +00:00
|
|
|
model = Trade
|
2022-10-29 13:05:01 +00:00
|
|
|
|
|
|
|
list_url_name = "trades"
|
|
|
|
list_url_args = ["type"]
|
2022-10-30 10:57:41 +00:00
|
|
|
|
|
|
|
|
2022-11-28 19:45:22 +00:00
|
|
|
class TradeDeleteAll(LoginRequiredMixin, OTPRequiredMixin, ObjectNameMixin, View):
|
2022-10-30 10:57:41 +00:00
|
|
|
template_name = "partials/notify.html"
|
2022-11-04 07:20:42 +00:00
|
|
|
model = Trade
|
2022-10-30 10:57:41 +00:00
|
|
|
|
|
|
|
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
|