2022-10-15 20:51:47 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2022-10-17 17:56:16 +00:00
|
|
|
def get_callbacks(user, hook=None):
|
|
|
|
if hook:
|
|
|
|
callbacks = Callback.objects.filter(hook=hook, hook__user=user)
|
|
|
|
else:
|
2022-10-15 20:51:47 +00:00
|
|
|
callbacks = Callback.objects.filter(hook__user=user)
|
|
|
|
return callbacks
|
|
|
|
|
|
|
|
|
|
|
|
class Callbacks(LoginRequiredMixin, View):
|
|
|
|
allowed_types = ["modal", "widget", "window", "page"]
|
2022-10-29 11:43:13 +00:00
|
|
|
window_content = "window-content/objects.html"
|
|
|
|
list_template = "partials/callback-list.html"
|
|
|
|
page_title = "List of received callbacks"
|
2022-10-15 20:51:47 +00:00
|
|
|
|
2022-10-29 11:43:13 +00:00
|
|
|
async def get(self, request, type, pk=None):
|
2022-10-15 20:51:47 +00:00
|
|
|
if type not in self.allowed_types:
|
|
|
|
return HttpResponseBadRequest
|
|
|
|
template_name = f"wm/{type}.html"
|
|
|
|
unique = str(uuid.uuid4())[:8]
|
|
|
|
|
2022-10-29 11:43:13 +00:00
|
|
|
if pk:
|
2022-10-15 20:51:47 +00:00
|
|
|
try:
|
2022-10-29 11:43:13 +00:00
|
|
|
hook = Hook.objects.get(id=pk, user=request.user)
|
2022-10-15 20:51:47 +00:00
|
|
|
except Hook.DoesNotExist:
|
|
|
|
message = "Hook does not exist."
|
|
|
|
message_class = "danger"
|
|
|
|
context = {
|
|
|
|
"message": message,
|
|
|
|
"class": message_class,
|
2022-10-16 13:37:49 +00:00
|
|
|
"type": type,
|
2022-10-15 20:51:47 +00:00
|
|
|
}
|
2022-10-16 13:37:49 +00:00
|
|
|
return render(request, template_name, context)
|
2022-10-17 17:56:16 +00:00
|
|
|
callbacks = get_callbacks(request.user, hook)
|
2022-10-13 14:26:43 +00:00
|
|
|
else:
|
2022-10-17 17:56:16 +00:00
|
|
|
callbacks = get_callbacks(request.user)
|
2022-10-17 06:20:30 +00:00
|
|
|
if type == "page":
|
|
|
|
type = "modal"
|
2022-10-15 20:51:47 +00:00
|
|
|
|
|
|
|
context = {
|
|
|
|
"title": f"Callbacks ({type})",
|
|
|
|
"unique": unique,
|
|
|
|
"window_content": self.window_content,
|
2022-10-29 11:43:13 +00:00
|
|
|
"list_template": self.list_template,
|
|
|
|
"object_list": callbacks,
|
2022-10-16 13:37:49 +00:00
|
|
|
"type": type,
|
2022-10-29 11:43:13 +00:00
|
|
|
"page_title": self.page_title,
|
2022-10-15 20:51:47 +00:00
|
|
|
}
|
|
|
|
return render(request, template_name, context)
|