fisk/core/views/callbacks.py

59 lines
2.1 KiB
Python
Raw Normal View History

from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponseBadRequest
from mixins.views import ObjectList
2022-11-29 07:20:21 +00:00
from core.models import Callback, Hook, Signal
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")
2022-11-29 07:20:21 +00:00
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,
2022-10-16 13:37:49 +00:00
"type": type,
}
return self.render_to_response(context)
callbacks = self.get_callbacks(self.request.user, hook)
2022-11-29 07:20:21 +00:00
elif object_type == "signal":
try:
signal = Signal.objects.get(id=object_id, user=self.request.user)
2022-11-29 07:20:21 +00:00
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)
2022-10-13 14:26:43 +00:00
else:
return HttpResponseBadRequest("Invalid object type")
return callbacks