dev: webhooks restructuring

This commit is contained in:
pablohashescobar 2023-11-23 13:02:17 +05:30
parent bc0c9404a4
commit 5186990426
3 changed files with 34 additions and 12 deletions

View File

@ -38,6 +38,7 @@ class TimezoneMixin:
class WebhookMixin: class WebhookMixin:
webhook_event = None webhook_event = None
bulk = False
def finalize_response(self, request, response, *args, **kwargs): def finalize_response(self, request, response, *args, **kwargs):
response = super().finalize_response(request, response, *args, **kwargs) response = super().finalize_response(request, response, *args, **kwargs)
@ -45,17 +46,17 @@ class WebhookMixin:
# Check for the case should webhook be sent # Check for the case should webhook be sent
if ( if (
self.webhook_event self.webhook_event
and self.request.method in ["DELETE"] and self.request.method in ["POST", "PATCH", "DELETE"]
and response.status_code in [204] and response.status_code in [200, 201, 204]
): ):
# Get the id
object_id = self.kwargs.get("pk")
# Push the object to delay # Push the object to delay
send_webhook.delay( send_webhook.delay(
event=self.webhook_event, event=self.webhook_event,
event_id=object_id, event_data=response.data,
kw=self.kwargs,
action=self.request.method, action=self.request.method,
slug=self.workspace_slug, slug=self.workspace_slug,
bulk=self.bulk,
) )
return response return response

View File

@ -195,6 +195,7 @@ class ModuleIssueAPIEndpoint(WebhookMixin, BaseAPIView):
serializer_class = ModuleIssueSerializer serializer_class = ModuleIssueSerializer
model = ModuleIssue model = ModuleIssue
webhook_event = "module_issue" webhook_event = "module_issue"
bulk = True
permission_classes = [ permission_classes = [
ProjectEntityPermission, ProjectEntityPermission,

View File

@ -54,11 +54,16 @@ MODEL_MAPPER = {
} }
def get_model_data(event, event_id): def get_model_data(event, event_id, many=False):
model = MODEL_MAPPER.get(event) model = MODEL_MAPPER.get(event)
queryset = model.objects.get(pk=event_id) if many:
queryset = model.objects.get(pk__in=event_id)
else:
queryset = model.objects.get(pk__in=event_id)
serializer = SERIALIZER_MAPPER.get(event) serializer = SERIALIZER_MAPPER.get(event)
return serializer(queryset).data data = serializer(queryset, many=many).data
return data
@shared_task( @shared_task(
@ -79,7 +84,10 @@ def webhook_task(self, webhook, slug, event, event_id, action):
"X-Plane-Event": event, "X-Plane-Event": event,
} }
event_data = get_model_data(event=event, event_id=event_id) if action == "DELETE":
event_data = {"id": str(event_id)}
else:
event_data = get_model_data(event=event, event_id=event_id, many=isinstance(event_id, list))
# # Your secret key # # Your secret key
event_data = ( event_data = (
@ -166,7 +174,7 @@ def webhook_task(self, webhook, slug, event, event_id, action):
@shared_task() @shared_task()
def send_webhook(event, event_id, action, slug): def send_webhook(event, event_data, kw, action, slug, bulk):
try: try:
webhooks = Webhook.objects.filter(workspace__slug=slug, is_active=True) webhooks = Webhook.objects.filter(workspace__slug=slug, is_active=True)
@ -185,6 +193,18 @@ def send_webhook(event, event_id, action, slug):
if event == "issue_comment": if event == "issue_comment":
webhooks = webhooks.filter(issue_comment=True) webhooks = webhooks.filter(issue_comment=True)
if webhooks:
if action in ["POST", "PATCH"]:
if bulk:
event_id = []
else:
event_id = event_data.get("id") if isinstance(event_data, dict) else None
if action == "DELETE":
event_id = kw.get("pk")
for webhook in webhooks: for webhook in webhooks:
webhook_task.delay(webhook.id, slug, event, event_id, action) webhook_task.delay(webhook.id, slug, event, event_id, action)