fisk/core/views/callbacks.py

59 lines
1.9 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
def get_callbacks(user, hook=None):
if hook:
callbacks = Callback.objects.filter(hook=hook, hook__user=user)
else:
callbacks = Callback.objects.filter(hook__user=user)
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, pk=None):
if type not in self.allowed_types:
return HttpResponseBadRequest
template_name = f"wm/{type}.html"
unique = str(uuid.uuid4())[:8]
if pk:
try:
hook = Hook.objects.get(id=pk, 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)
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)