160 lines
4.8 KiB
Python
160 lines
4.8 KiB
Python
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 core.forms import HookForm
|
|
from core.models import Callback, Hook
|
|
|
|
|
|
def get_hooks(user):
|
|
hooks = Hook.objects.filter(user=user)
|
|
return hooks
|
|
|
|
|
|
class HookAPI(APIView):
|
|
parser_classes = [JSONParser]
|
|
|
|
def post(self, request, hook_name):
|
|
try:
|
|
hook = Hook.objects.get(name=hook_name)
|
|
except Hook.DoesNotExist:
|
|
return HttpResponseBadRequest("Hook does not exist.")
|
|
callback = Callback.objects.create(
|
|
hook=hook,
|
|
data=request.data,
|
|
)
|
|
callback.save()
|
|
print("SAVED")
|
|
|
|
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")
|
|
|
|
|
|
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]
|
|
hooks = get_hooks(request.user)
|
|
context = {
|
|
"title": f"Hooks ({type})",
|
|
"unique": unique,
|
|
"window_content": self.window_content,
|
|
"items": hooks,
|
|
}
|
|
return render(request, template_name, context)
|
|
|
|
|
|
class HookAction(LoginRequiredMixin, APIView):
|
|
template_name = "modals/add-hook.html"
|
|
parser_classes = [FormParser]
|
|
|
|
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}
|
|
|
|
return render(request, self.template_name, context)
|
|
|
|
def put(self, request, hook_id=None):
|
|
"""
|
|
Add or edit a hook.
|
|
:param hook_id: The id of the hook to edit. Optional.
|
|
"""
|
|
message = None
|
|
message_class = "success"
|
|
|
|
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)
|
|
if form.is_valid():
|
|
hook = form.save(commit=False)
|
|
hook.user = request.user
|
|
hook.save()
|
|
if hook_id:
|
|
message = f"Hook {hook_id} edited successfully"
|
|
else:
|
|
message = f"Hook {hook.id} added successfully"
|
|
else:
|
|
message = "Error adding hook"
|
|
message_class = "danger"
|
|
|
|
hooks = get_hooks(request.user)
|
|
|
|
context = {
|
|
"items": hooks,
|
|
}
|
|
if message:
|
|
context["message"] = message
|
|
context["class"] = message_class
|
|
template_name = "partials/hook-list.html"
|
|
return render(request, template_name, context)
|
|
|
|
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,
|
|
}
|
|
if message:
|
|
context["message"] = message
|
|
context["class"] = message_class
|
|
|
|
template_name = "partials/hook-list.html"
|
|
return render(request, template_name, context)
|