fisk/core/views/hooks.py

196 lines
6.1 KiB
Python
Raw Normal View History

2022-10-14 06:20:30 +00:00
import uuid
import orjson
2022-10-14 06:20:30 +00:00
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponse, HttpResponseBadRequest
2022-10-14 06:20:30 +00:00
from django.shortcuts import render
from django.views import View
from rest_framework.parsers import FormParser, JSONParser
2022-10-12 06:22:22 +00:00
from rest_framework.views import APIView
from serde import ValidationError
2022-10-12 06:22:22 +00:00
from core.forms import HookForm
2022-10-15 22:24:03 +00:00
from core.lib.serde import drakdoo
from core.models import Callback, Hook
2022-10-15 22:36:04 +00:00
from core.util import logs
2022-10-15 22:24:03 +00:00
2022-10-15 22:34:55 +00:00
log = logs.get_logger(__name__)
2022-10-12 06:22:22 +00:00
2022-10-12 06:22:22 +00:00
def get_hooks(user):
hooks = Hook.objects.filter(user=user)
return hooks
2022-10-14 06:20:30 +00:00
class HookAPI(APIView):
parser_classes = [JSONParser]
def post(self, request, hook_name):
2022-10-15 22:24:03 +00:00
log.debug(f"HookAPI POST: {request.data}")
# Try loading the JSON
try:
loaded_json = orjson.loads(request.data)
except orjson.JSONDecodeError:
return HttpResponseBadRequest("Invalid JSON")
# Try validating the JSON
try:
hook_resp = drakdoo.BaseDrakdoo.from_dict(loaded_json)
except ValidationError as e:
log.error(f"HookAPI POST: {e}")
return HttpResponseBadRequest(e)
data = {
"title": hook_resp.title,
"message": hook_resp.message,
"period": hook_resp.period,
"market": hook_resp.market,
"timestamp_sent": hook_resp.timestamp.sent,
"timestamp_trade": hook_resp.timestamp.trade,
"market_exchange": hook_resp.market.exchange,
"market_item": hook_resp.market.item,
"market_currency": hook_resp.market.currency,
"market_contract": hook_resp.market.contract,
}
log.debug("HookAPI callback: data: %s", data)
2022-10-15 22:24:03 +00:00
# Try getting the hook
2022-10-15 21:08:13 +00:00
try:
hook = Hook.objects.get(hook=hook_name)
2022-10-15 21:08:13 +00:00
except Hook.DoesNotExist:
return HttpResponseBadRequest("Hook does not exist.")
2022-10-15 22:24:03 +00:00
# Create the callback object
callback = Callback.objects.create(hook=hook, **data)
callback.save()
2022-10-15 22:24:03 +00:00
# Bump received count
hook.received = hook.received + 1
2022-10-15 22:10:18 +00:00
hook.save()
return HttpResponse("OK")
def get(self, request, hook_name):
hook = Hook.objects.get(name=hook_name)
return_data = {"name": hook.name, "hook": hook.hook, "hook_id": hook.id}
return HttpResponse(orjson.dumps(return_data), content_type="application/json")
2022-10-14 06:20:30 +00:00
class Hooks(LoginRequiredMixin, View):
allowed_types = ["modal", "widget", "window", "page"]
window_content = "window-content/hooks.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]
2022-10-12 06:22:22 +00:00
hooks = get_hooks(request.user)
2022-10-14 06:20:30 +00:00
context = {
"title": f"Hooks ({type})",
2022-10-14 06:20:30 +00:00
"unique": unique,
"window_content": self.window_content,
"items": hooks,
2022-10-14 06:20:30 +00:00
}
return render(request, template_name, context)
2022-10-12 06:22:22 +00:00
2022-10-15 17:45:25 +00:00
class HookAction(LoginRequiredMixin, APIView):
2022-10-12 06:22:22 +00:00
template_name = "modals/add-hook.html"
parser_classes = [FormParser]
2022-10-15 17:45:25 +00:00
def get(self, request, hook_id=None):
"""
Get the form for adding or editing a hook.
:param hook_id: The id of the hook to edit. Optional.
"""
if hook_id:
try:
hook = Hook.objects.get(id=hook_id, user=request.user)
form = HookForm(instance=hook)
except Hook.DoesNotExist:
message = "Hook does not exist"
message_class = "danger"
context = {
"message": message,
"message_class": message_class,
}
return render(request, self.template_name, context)
else:
form = HookForm()
context = {"form": form, "hook_id": hook_id}
2022-10-12 06:22:22 +00:00
return render(request, self.template_name, context)
2022-10-15 17:45:25 +00:00
def put(self, request, hook_id=None):
"""
Add or edit a hook.
:param hook_id: The id of the hook to edit. Optional.
"""
2022-10-12 06:22:22 +00:00
message = None
message_class = "success"
2022-10-15 17:45:25 +00:00
if hook_id:
try:
form = HookForm(request.data, instance=Hook.objects.get(id=hook_id))
except Hook.DoesNotExist:
message = "Hook does not exist"
message_class = "danger"
context = {
"message": message,
"class": message_class,
}
return render(request, self.template_name, context)
else:
form = HookForm(request.data)
2022-10-12 06:22:22 +00:00
if form.is_valid():
hook = form.save(commit=False)
hook.user = request.user
hook.save()
2022-10-15 17:45:25 +00:00
if hook_id:
message = f"Hook {hook_id} edited successfully"
else:
message = f"Hook {hook.id} added successfully"
2022-10-12 06:22:22 +00:00
else:
message = "Error adding hook"
message_class = "danger"
hooks = get_hooks(request.user)
context = {
"items": hooks,
2022-10-12 06:22:22 +00:00
}
if message:
context["message"] = message
context["class"] = message_class
template_name = "partials/hook-list.html"
return render(request, template_name, context)
2022-10-15 17:45:25 +00:00
def delete(self, request, hook_id):
"""
Delete a hook.
:param hook_id: The id of the hook to delete.
"""
message = None
message_class = "success"
try:
hook = Hook.objects.get(id=hook_id, user=request.user)
hook.delete()
message = "Hook deleted successfully"
except Hook.DoesNotExist:
message = "Error deleting hook"
message_class = "danger"
hooks = get_hooks(request.user)
context = {
"items": hooks,
2022-10-15 17:45:25 +00:00
}
if message:
context["message"] = message
context["class"] = message_class
template_name = "partials/hook-list.html"
return render(request, template_name, context)