You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.7 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/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(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,
"items": callbacks,
"type": type,
}
return render(request, template_name, context)