From e09e8c7fa62c1195191b3b4acc8b3875a3ad06a2 Mon Sep 17 00:00:00 2001 From: Jono Yang Date: Thu, 25 Jul 2024 17:03:57 -0700 Subject: [PATCH] Add code to create a webhook url #504 Signed-off-by: Jono Yang --- minecode/api.py | 7 +++++++ minecode/utils.py | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/minecode/api.py b/minecode/api.py index 99c2c74a..f2b1c151 100644 --- a/minecode/api.py +++ b/minecode/api.py @@ -24,6 +24,7 @@ from minecode.models import PriorityResourceURI, ResourceURI, ScannableURI from minecode.permissions import IsScanQueueWorkerAPIUser from minecode.utils import get_temp_file +from minecode.utils import get_webhook_url class ResourceURISerializer(serializers.ModelSerializer): @@ -109,10 +110,15 @@ def get_next_download_url(self, request, *args, **kwargs): with transaction.atomic(): scannable_uri = ScannableURI.objects.get_next_scannable() if scannable_uri: + user = self.request.user + # TODO: Create scan index API view + # TODO: Create custom User class and use UUID as id field + webhook_url = get_webhook_url("notifications:send_scan_notification", user.id) response = { 'scannable_uri_uuid': scannable_uri.uuid, 'download_url': scannable_uri.uri, 'pipelines': scannable_uri.pipelines, + 'webhook_url': webhook_url, } scannable_uri.scan_status = ScannableURI.SCAN_SUBMITTED scannable_uri.scan_date = timezone.now() @@ -122,6 +128,7 @@ def get_next_download_url(self, request, *args, **kwargs): 'scannable_uri_uuid': '', 'download_url': '', 'pipelines': [], + 'webhook_url': '', } return Response(response) diff --git a/minecode/utils.py b/minecode/utils.py index 51dd20fa..7935bfa9 100644 --- a/minecode/utils.py +++ b/minecode/utils.py @@ -15,6 +15,8 @@ import uuid from django.conf import settings +from django.core import signing +from django.urls import reverse from django.utils.encoding import force_str import arrow @@ -415,3 +417,15 @@ def __iter__(self): def next(self): return self._generator.next() + + +def get_webhook_url(view_name, user_uuid): + """ + Return a Webhook target URL based on the `user_uuid`. + This URL is used to create notifications for this user. + """ + user_key = signing.dumps(str(user_uuid)) + target_url = reverse(view_name, args=[user_key]) + site_url = settings.SITE_URL.rstrip("/") + webhook_url = site_url + target_url + return webhook_url