from django.contrib.auth.mixins import LoginRequiredMixin from django.http import HttpResponseBadRequest from mixins.views import ObjectList from core.models import Callback, Hook, Signal def get_callbacks(user, hook=None, signal=None): if hook: cast = {"hook": hook, "hook__user": user} elif signal: cast = {"signal": signal, "signal__user": user} callbacks = Callback.objects.filter(**cast) return callbacks class Callbacks(LoginRequiredMixin, ObjectList): list_template = "partials/callback-list.html" page_title = "List of received callbacks" context_object_name_singular = "callback" context_object_name = "callbacks" list_url_name = "callbacks" list_url_args = ["type", "object_type", "object_id"] def get_callbacks(self, user, hook=None, signal=None): if hook: cast = {"hook": hook, "hook__user": user} elif signal: cast = {"signal": signal, "signal__user": user} callbacks = Callback.objects.filter(**cast) return callbacks def get_queryset(self, **kwargs): object_type = kwargs.get("object_type") object_id = kwargs.get("object_id") if object_type == "hook": try: hook = Hook.objects.get(id=object_id, user=self.request.user) except Hook.DoesNotExist: message = "Hook does not exist." message_class = "danger" context = { "message": message, "class": message_class, "type": type, } return self.render_to_response(context) callbacks = self.get_callbacks(self.request.user, hook) elif object_type == "signal": try: signal = Signal.objects.get(id=object_id, user=self.request.user) except Signal.DoesNotExist: message = "Signal does not exist." message_class = "danger" context = { "message": message, "class": message_class, "type": type, } return self.render_to_response(context) callbacks = self.get_callbacks(self.request.user, signal=signal) else: return HttpResponseBadRequest("Invalid object type") return callbacks