Make hooks/callbacks inherit type

This commit is contained in:
2022-10-16 14:14:53 +01:00
parent 3af63c5ef6
commit 97d12b0fab
14 changed files with 102 additions and 64 deletions

View File

@@ -92,19 +92,25 @@ class Hooks(LoginRequiredMixin, View):
"unique": unique,
"window_content": self.window_content,
"items": hooks,
"type": type,
}
return render(request, template_name, context)
class HookAction(LoginRequiredMixin, APIView):
template_name = "modals/add-hook.html"
allowed_types = ["modal", "widget", "window", "page"]
window_content = "window-content/add-hook.html"
parser_classes = [FormParser]
def get(self, request, hook_id=None):
def get(self, request, type, hook_id=None):
"""
Get the form for adding or editing a hook.
:param hook_id: The id of the hook to edit. Optional.
"""
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)
@@ -115,15 +121,22 @@ class HookAction(LoginRequiredMixin, APIView):
context = {
"message": message,
"message_class": message_class,
"window_content": self.window_content,
}
return render(request, self.template_name, context)
return render(request, template_name, context)
else:
form = HookForm()
context = {"form": form, "hook_id": hook_id}
context = {
"form": form,
"hook_id": hook_id,
"type": type,
"unique": unique,
"window_content": self.window_content,
}
return render(request, self.template_name, context)
return render(request, template_name, context)
def put(self, request, hook_id=None):
def put(self, request, type, hook_id=None):
"""
Add or edit a hook.
:param hook_id: The id of the hook to edit. Optional.
@@ -160,6 +173,7 @@ class HookAction(LoginRequiredMixin, APIView):
context = {
"items": hooks,
"type": type,
}
if message:
context["message"] = message
@@ -167,7 +181,7 @@ class HookAction(LoginRequiredMixin, APIView):
template_name = "partials/hook-list.html"
return render(request, template_name, context)
def delete(self, request, hook_id):
def delete(self, request, type, hook_id):
"""
Delete a hook.
:param hook_id: The id of the hook to delete.
@@ -186,6 +200,7 @@ class HookAction(LoginRequiredMixin, APIView):
context = {
"items": hooks,
"type": type,
}
if message:
context["message"] = message