fisk/core/views/callbacks.py

53 lines
1.6 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(hook=None, user=None):
if user:
callbacks = Callback.objects.filter(hook__user=user)
elif hook:
callbacks = Callback.objects.filter(hook=hook)
return callbacks
class Callbacks(LoginRequiredMixin, View):
allowed_types = ["modal", "widget", "window", "page"]
window_content = "window-content/callbacks.html"
async def get(self, request, type, hook_id=None):
if type not in self.allowed_types:
return HttpResponseBadRequest
template_name = f"wm/{type}.html"
unique = str(uuid.uuid4())[:8]
if hook_id:
try:
hook = Hook.objects.get(id=hook_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(hook)
else:
callbacks = get_callbacks(user=request.user)
context = {
"title": f"Callbacks ({type})",
"unique": unique,
"window_content": self.window_content,
"items": callbacks,
"type": type,
}
return render(request, template_name, context)