2022-10-15 20:51:47 +00:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
|
|
from django.http import HttpResponseBadRequest
|
2023-02-10 20:49:35 +00:00
|
|
|
from mixins.views import ObjectList
|
2022-10-15 20:51:47 +00:00
|
|
|
|
2022-11-29 07:20:21 +00:00
|
|
|
from core.models import Callback, Hook, Signal
|
2022-10-15 20:51:47 +00:00
|
|
|
|
|
|
|
|
2022-12-08 07:20:46 +00:00
|
|
|
class Callbacks(LoginRequiredMixin, ObjectList):
|
2022-10-29 11:43:13 +00:00
|
|
|
list_template = "partials/callback-list.html"
|
|
|
|
page_title = "List of received callbacks"
|
2022-10-15 20:51:47 +00:00
|
|
|
|
2022-12-08 07:20:46 +00:00
|
|
|
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
|
2022-10-15 20:51:47 +00:00
|
|
|
|
2022-12-08 07:20:46 +00:00
|
|
|
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":
|
2022-10-15 20:51:47 +00:00
|
|
|
try:
|
2022-12-08 07:20:46 +00:00
|
|
|
hook = Hook.objects.get(id=object_id, user=self.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-12-08 07:20:46 +00:00
|
|
|
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:
|
2022-12-08 07:20:46 +00:00
|
|
|
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,
|
|
|
|
}
|
2022-12-08 07:20:46 +00:00
|
|
|
return self.render_to_response(context)
|
|
|
|
callbacks = self.get_callbacks(self.request.user, signal=signal)
|
2022-10-13 14:26:43 +00:00
|
|
|
else:
|
2022-12-08 07:20:46 +00:00
|
|
|
return HttpResponseBadRequest("Invalid object type")
|
2022-10-15 20:51:47 +00:00
|
|
|
|
2022-12-08 07:20:46 +00:00
|
|
|
return callbacks
|