From 387a9c5e72d9b29321c586a9eb4ae88a4f9b4830 Mon Sep 17 00:00:00 2001 From: Eric Park Date: Tue, 29 Jun 2021 14:43:05 +0900 Subject: [PATCH] shipper: add a maintainer API endpoint to enable/disable builds --- shipper/urls.py | 2 ++ shipper/views.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/shipper/urls.py b/shipper/urls.py index ad428dc9..10f784e9 100644 --- a/shipper/urls.py +++ b/shipper/urls.py @@ -16,4 +16,6 @@ path('maintainers/api/token_check/', maintainer_api_token_check, name='maintainer_api_token_check'), path('maintainers/api/chunked_upload/', ChunkedBuildUpload.as_view(), name='chunked_build_upload'), path('maintainers/api/chunked_upload//', ChunkedBuildUpload.as_view(), name='chunkedupload-detail'), + path('maintainers/api/build/enabled_status_modify/', maintainer_api_build_enabled_status_modify, + name='maintainer_api_build_enabled_status_modify'), ] diff --git a/shipper/views.py b/shipper/views.py index 08f94742..1c504722 100644 --- a/shipper/views.py +++ b/shipper/views.py @@ -278,6 +278,53 @@ def maintainer_api_token_check(request): ) +@csrf_exempt +@api_view(["GET"]) +def maintainer_api_build_enabled_status_modify(request): + build_id = request.data.get("build_id") + enable = bool(request.data.get("enable")) + if build_id is None or enable is None: + return Response( + { + 'error': 'missing_parameters', + 'message': 'One or more of the required parameters is blank! Required parameters: build ID, ' + 'enabled flag' + }, + status=HTTP_400_BAD_REQUEST + ) + + build = get_object_or_404(Build, pk=build_id) + + # Check if maintainer has permission to modify this build/device + if request.user not in build.device.maintainers.all(): + return Response( + { + 'error': 'insufficient_permissions', + 'message': 'You are not authorized to modify builds associated with this device!' + }, + status=HTTP_401_UNAUTHORIZED + ) + + # Switch build status + build.enabled = enable + build.save() + + if enable: + return Response( + { + 'message': 'Successfully enabled the build!' + }, + status=HTTP_200_OK + ) + else: + return Response( + { + 'message': 'Successfully disabled the build!' + }, + status=HTTP_200_OK + ) + + def get_codename_from_filename(filename): fields = os.path.splitext(filename)[0].split('-') # Check field count