2022-10-17 17:56:16 +00:00
|
|
|
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 TradeForm
|
|
|
|
from core.lib.serde import drakdoo
|
2022-10-17 18:06:09 +00:00
|
|
|
from core.models import Account, Callback, Trade
|
2022-10-17 17:56:16 +00:00
|
|
|
from core.util import logs
|
|
|
|
|
|
|
|
log = logs.get_logger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def get_trades(user, account=None):
|
|
|
|
if user:
|
|
|
|
trades = Trade.objects.filter(account__user=user)
|
|
|
|
elif account:
|
|
|
|
trades = Trade.objects.filter(account=account, account__user=user)
|
|
|
|
return trades
|
|
|
|
|
|
|
|
|
|
|
|
class Trades(LoginRequiredMixin, View):
|
|
|
|
allowed_types = ["modal", "widget", "window", "page"]
|
|
|
|
window_content = "window-content/trades.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]
|
|
|
|
if account_id:
|
|
|
|
try:
|
|
|
|
trades = Account.objects.get(id=account_id, user=request.user)
|
|
|
|
except Account.DoesNotExist:
|
|
|
|
message = "Account does not exist."
|
|
|
|
message_class = "danger"
|
|
|
|
context = {
|
|
|
|
"message": message,
|
|
|
|
"class": message_class,
|
|
|
|
"type": type,
|
|
|
|
}
|
|
|
|
return render(request, template_name, context)
|
|
|
|
trades = get_trades(request.user, account_id)
|
|
|
|
else:
|
|
|
|
trades = get_trades(request.user)
|
|
|
|
if type == "page":
|
|
|
|
type = "modal"
|
|
|
|
context = {
|
|
|
|
"title": f"Trades ({type})",
|
|
|
|
"unique": unique,
|
|
|
|
"window_content": self.window_content,
|
|
|
|
"items": trades,
|
|
|
|
"type": type,
|
|
|
|
}
|
|
|
|
return render(request, template_name, context)
|
|
|
|
|
|
|
|
|
|
|
|
class TradeAction(LoginRequiredMixin, APIView):
|
|
|
|
allowed_types = ["modal", "widget", "window", "page"]
|
|
|
|
window_content = "window-content/add-trade.html"
|
|
|
|
parser_classes = [FormParser]
|
|
|
|
|
|
|
|
def get(self, request, type, trade_id=None):
|
|
|
|
"""
|
|
|
|
Get the form for adding or editing a trade.
|
|
|
|
:param trade_id: The id of the trade to edit. Optional.
|
|
|
|
"""
|
|
|
|
if type not in self.allowed_types:
|
|
|
|
return HttpResponseBadRequest
|
|
|
|
template_name = f"wm/{type}.html"
|
|
|
|
unique = str(uuid.uuid4())[:8]
|
|
|
|
if trade_id:
|
|
|
|
try:
|
|
|
|
trade = Trade.objects.get(id=trade_id, account__user=request.user)
|
|
|
|
form = TradeForm(instance=trade)
|
|
|
|
except Trade.DoesNotExist:
|
|
|
|
message = "Trade does not exist"
|
|
|
|
message_class = "danger"
|
|
|
|
context = {
|
|
|
|
"message": message,
|
|
|
|
"message_class": message_class,
|
|
|
|
"window_content": self.window_content,
|
|
|
|
}
|
|
|
|
return render(request, template_name, context)
|
|
|
|
else:
|
|
|
|
form = TradeForm()
|
|
|
|
if type == "page":
|
|
|
|
type = "modal"
|
|
|
|
context = {
|
|
|
|
"form": form,
|
|
|
|
"trade_id": trade_id,
|
|
|
|
"type": type,
|
|
|
|
"unique": unique,
|
|
|
|
"window_content": self.window_content,
|
|
|
|
}
|
|
|
|
|
|
|
|
return render(request, template_name, context)
|
|
|
|
|
|
|
|
def put(self, request, type, trade_id=None):
|
|
|
|
"""
|
|
|
|
Add or edit a trade.
|
|
|
|
:param trade_id: The id of the trade to edit. Optional.
|
|
|
|
"""
|
|
|
|
if type not in self.allowed_types:
|
|
|
|
return HttpResponseBadRequest
|
|
|
|
message = None
|
|
|
|
message_class = "success"
|
|
|
|
|
|
|
|
if trade_id:
|
|
|
|
try:
|
|
|
|
form = TradeForm(request.data, instance=Trade.objects.get(id=trade_id))
|
|
|
|
except Trade.DoesNotExist:
|
|
|
|
message = "Trade does not exist"
|
|
|
|
message_class = "danger"
|
|
|
|
context = {
|
|
|
|
"message": message,
|
|
|
|
"class": message_class,
|
|
|
|
}
|
|
|
|
return render(request, self.template_name, context)
|
|
|
|
else:
|
|
|
|
form = TradeForm(request.data)
|
|
|
|
if form.is_valid():
|
|
|
|
trade = form.save(commit=False)
|
|
|
|
trade.save()
|
|
|
|
if trade_id:
|
|
|
|
message = f"Trade {trade_id} edited successfully"
|
|
|
|
else:
|
|
|
|
message = f"Trade {trade.id} added successfully"
|
|
|
|
else:
|
|
|
|
message = "Error adding trade"
|
|
|
|
message_class = "danger"
|
|
|
|
|
|
|
|
trades = get_trades(request.user)
|
|
|
|
|
|
|
|
context = {
|
|
|
|
"items": trades,
|
|
|
|
"type": type,
|
|
|
|
}
|
|
|
|
if message:
|
|
|
|
context["message"] = message
|
|
|
|
context["class"] = message_class
|
|
|
|
template_name = "partials/trade-list.html"
|
|
|
|
return render(request, template_name, context)
|
|
|
|
|
|
|
|
def delete(self, request, type, trade_id):
|
|
|
|
"""
|
|
|
|
Delete a trade.
|
|
|
|
:param trade_id: The id of the trade to delete.
|
|
|
|
"""
|
|
|
|
if type not in self.allowed_types:
|
|
|
|
return HttpResponseBadRequest
|
|
|
|
message = None
|
|
|
|
message_class = "success"
|
|
|
|
try:
|
|
|
|
trade = Trade.objects.get(id=trade_id, account__user=request.user)
|
|
|
|
trade.delete()
|
|
|
|
message = "trade deleted successfully"
|
|
|
|
except Trade.DoesNotExist:
|
|
|
|
message = "Error deleting trade"
|
|
|
|
message_class = "danger"
|
|
|
|
|
|
|
|
trades = get_trades(request.user)
|
|
|
|
|
|
|
|
context = {
|
|
|
|
"items": trades,
|
|
|
|
"type": type,
|
|
|
|
}
|
|
|
|
if message:
|
|
|
|
context["message"] = message
|
|
|
|
context["class"] = message_class
|
|
|
|
|
|
|
|
template_name = "partials/trade-list.html"
|
|
|
|
return render(request, template_name, context)
|