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 rest_framework.parsers import FormParser from rest_framework.views import APIView from core.forms import HookForm from core.models import Hook def get_hooks(user): hooks = Hook.objects.filter(user=user) return hooks class Hooks(LoginRequiredMixin, View): allowed_types = ["modal", "widget", "window", "page"] window_content = "window-content/hooks.html" async def get(self, request, type): if type not in self.allowed_types: return HttpResponseBadRequest template_name = f"wm/{type}.html" unique = str(uuid.uuid4())[:8] hooks = get_hooks(request.user) context = { "title": f"{type} Demo", "unique": unique, "window_content": self.window_content, "hooks": hooks, } return render(request, template_name, context) class AddHook(LoginRequiredMixin, APIView): template_name = "modals/add-hook.html" parser_classes = [FormParser] def get(self, request): form = HookForm() context = {"form": form} return render(request, self.template_name, context) def put(self, request): message = None message_class = "success" # data = request.data form = HookForm(request.data) if form.is_valid(): hook = form.save(commit=False) hook.user = request.user hook.save() message = "Hook added successfully" else: message = "Error adding hook" message_class = "danger" hooks = get_hooks(request.user) context = { "hooks": hooks, } if message: context["message"] = message context["class"] = message_class template_name = "partials/hook-list.html" return render(request, template_name, context)