fisk/core/views/trades.py

78 lines
2.0 KiB
Python
Raw Normal View History

2022-10-17 17:56:16 +00:00
from django.contrib.auth.mixins import LoginRequiredMixin
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
from core.forms import TradeForm
from core.models import Trade
2022-10-17 17:56:16 +00:00
from core.util import logs
from core.views import (
ObjectCreate,
ObjectDelete,
ObjectList,
ObjectNameMixin,
ObjectUpdate,
)
2022-10-17 17:56:16 +00:00
log = logs.get_logger(__name__)
2022-11-28 19:45:22 +00:00
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."
2022-10-17 17:56:16 +00:00
2022-10-29 13:05:01 +00:00
list_url_name = "trades"
list_url_args = ["type"]
submit_url_name = "trade_create"
2022-10-17 17:56:16 +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):
model = Trade
form_class = TradeForm
2022-10-29 13:05:01 +00:00
list_url_name = "trades"
list_url_args = ["type"]
submit_url_name = "trade_create"
2022-10-17 17:56:16 +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):
model = Trade
form_class = TradeForm
2022-10-29 13:05:01 +00:00
list_url_name = "trades"
list_url_args = ["type"]
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):
model = Trade
2022-10-29 13:05:01 +00:00
list_url_name = "trades"
list_url_args = ["type"]
2022-11-28 19:45:22 +00:00
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