fisk/core/views/callbacks.py

73 lines
2.5 KiB
Python

import uuid
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponseBadRequest
from django.shortcuts import render
from django.views import View
from core.models import Callback, Hook, Signal
def get_callbacks(user, hook=None, signal=None):
if hook:
cast = {"hook": hook, "hook__user": user}
elif signal:
cast = {"signal": signal, "signal__user": user}
callbacks = Callback.objects.filter(**cast)
return callbacks
class Callbacks(LoginRequiredMixin, View):
allowed_types = ["modal", "widget", "window", "page"]
window_content = "window-content/objects.html"
list_template = "partials/callback-list.html"
page_title = "List of received callbacks"
async def get(self, request, type, object_type, object_id):
if type not in self.allowed_types:
return HttpResponseBadRequest
template_name = f"wm/{type}.html"
unique = str(uuid.uuid4())[:8]
if object_type == "hook":
try:
hook = Hook.objects.get(id=object_id, user=request.user)
except Hook.DoesNotExist:
message = "Hook does not exist."
message_class = "danger"
context = {
"message": message,
"class": message_class,
"type": type,
}
return render(request, template_name, context)
callbacks = get_callbacks(request.user, hook)
elif object_type == "signal":
try:
signal = Signal.objects.get(id=object_id, user=request.user)
except Signal.DoesNotExist:
message = "Signal does not exist."
message_class = "danger"
context = {
"message": message,
"class": message_class,
"type": type,
}
return render(request, template_name, context)
callbacks = get_callbacks(request.user, signal=signal)
else:
callbacks = get_callbacks(request.user)
if type == "page":
type = "modal"
context = {
"title": f"Callbacks ({type})",
"unique": unique,
"window_content": self.window_content,
"list_template": self.list_template,
"object_list": callbacks,
"type": type,
"page_title": self.page_title,
}
return render(request, template_name, context)