fisk/core/views/trades.py

188 lines
6.1 KiB
Python
Raw Normal View History

2022-10-17 17:56:16 +00:00
import uuid
from django.contrib.auth.mixins import LoginRequiredMixin
2022-10-17 06:20:30 +00:00
from django.http import HttpResponseBadRequest
2022-10-17 17:56:16 +00:00
from django.shortcuts import render
from django.views import View
2022-10-17 06:20:30 +00:00
from rest_framework.parsers import FormParser
2022-10-17 17:56:16 +00:00
from rest_framework.views import APIView
from core.forms import TradeForm
2022-10-17 06:20:30 +00:00
from core.models import Account, 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, account__user=request.user),
)
2022-10-17 17:56:16 +00:00
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)
2022-10-17 06:20:30 +00:00
print("PRESAVE TRADE", trade)
trade.user = request.user
2022-10-17 17:56:16 +00:00
trade.save()
success, returned = trade.post()
if success:
print("SAVED TRADE", trade)
if trade_id:
message = f"Trade {trade_id} edited successfully"
else:
message = f"Trade {trade.id} added successfully"
2022-10-17 17:56:16 +00:00
else:
message = f"Error adding trade: {returned}"
message_class = "danger"
2022-10-17 17:56:16 +00:00
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"
2022-10-17 17:56:16 +00:00
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)