fisk/core/views/strategies.py

189 lines
6.1 KiB
Python
Raw Normal View History

import uuid
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponseBadRequest
from django.shortcuts import render
from django.views import View # , CreateView, UpdateView, DeleteView
from rest_framework.parsers import FormParser
from rest_framework.views import APIView
from core.forms import StrategyForm
from core.models import Strategy
from core.util import logs
# from django.urls import reverse
log = logs.get_logger(__name__)
def get_strategies(user):
strategies = Strategy.objects.filter(user=user)
return strategies
class Strategies(LoginRequiredMixin, View):
allowed_types = ["modal", "widget", "window", "page"]
window_content = "window-content/strategies.html"
async def get(self, request, type):
if type not in self.allowed_types:
return HttpResponseBadRequest
template_name = f"wm/{type}.html"
unique = str(uuid.uuid4())[:8]
strategies = get_strategies(request.user)
if type == "page":
type = "modal"
context = {
"title": f"Accounts ({type})",
"unique": unique,
"window_content": self.window_content,
"items": strategies,
"type": type,
}
return render(request, template_name, context)
# class AddStrategy(CreateView):
# model = Strategy
# form_class = StrategyForm
# template_name = "window-content/add-strategy.html"
# success_url = reverse("strategies")
# def form_valid(self, form):
# form.instance.user = self.request.user
# return super().form_valid(form)
# def get_context_data(self, **kwargs):
# context = super().get_context_data(**kwargs)
# context["title"] = "Add Strategy"
# context["window_content"] = "window-content/add_strategy.html"
# return context
class StrategiesAction(LoginRequiredMixin, APIView):
allowed_types = ["modal", "widget", "window", "page"]
window_content = "window-content/add-strategy.html"
parser_classes = [FormParser]
def get(self, request, type, strategy_id=None):
"""
Get the form for adding or editing a strategy.
:param strategy_id: The id of the strategy to edit. Optional.
"""
if type not in self.allowed_types:
return HttpResponseBadRequest
template_name = f"wm/{type}.html"
unique = str(uuid.uuid4())[:8]
if strategy_id:
try:
account = Strategy.objects.get(id=strategy_id)
form = StrategyForm(instance=account)
except Strategy.DoesNotExist:
message = "Strategy 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 = StrategyForm()
if type == "page":
type = "modal"
context = {
"form": form,
"strategy_id": strategy_id,
"type": type,
"unique": unique,
"window_content": self.window_content,
}
return render(request, template_name, context)
def put(self, request, type, strategy_id=None):
"""
Add or edit a account.
:param account_id: The id of the strategy to edit. Optional.
"""
if type not in self.allowed_types:
return HttpResponseBadRequest
message = None
message_class = "success"
if strategy_id:
try:
form = StrategyForm(
request.data,
instance=Strategy.objects.get(
id=strategy_id, account__user=request.user
),
)
except Strategy.DoesNotExist:
message = "Strategy does not exist"
message_class = "danger"
context = {
"message": message,
"class": message_class,
}
return render(request, self.template_name, context)
else:
form = StrategyForm(request.data)
if form.is_valid():
hooks = list(form.cleaned_data.get("hooks"))
strategy = form.save(commit=False)
strategy.user = request.user
strategy.hooks.set(hooks)
strategy.save()
print("HOOKS SET", strategy.hooks)
if strategy_id:
message = f"Strategy {strategy_id} edited successfully"
else:
message = f"Strategy {strategy.id} added successfully"
else:
message = "Error adding strategy"
message_class = "danger"
accounts = get_strategies(request.user)
context = {
"items": accounts,
"type": type,
}
if message:
context["message"] = message
context["class"] = message_class
template_name = "partials/strategy-list.html"
return render(request, template_name, context)
def delete(self, request, type, strategy_id):
"""
Delete a strategy.
:param strategy_id: The id of the strategy to delete.
"""
if type not in self.allowed_types:
return HttpResponseBadRequest
message = None
message_class = "success"
try:
strategy = Strategy.objects.get(id=strategy_id, user=request.user)
strategy.delete()
message = "Strategy deleted successfully"
except Strategy.DoesNotExist:
message = "Error deleting strategy"
message_class = "danger"
strategies = get_strategies(request.user)
context = {
"items": strategies,
"type": strategies,
}
if message:
context["message"] = message
context["class"] = message_class
template_name = "partials/strategy-list.html"
return render(request, template_name, context)