From d88c180ffa64d4b72c16313003591b05d43bd63c Mon Sep 17 00:00:00 2001 From: David <9059044+Tansito@users.noreply.github.com> Date: Fri, 20 Dec 2024 12:12:31 -0500 Subject: [PATCH] refactor programs references to functions --- .../{programs.py => functions.py} | 30 +++++++++---------- gateway/api/views/files.py | 28 ++++++++--------- gateway/api/views/programs.py | 4 +-- 3 files changed, 29 insertions(+), 33 deletions(-) rename gateway/api/repositories/{programs.py => functions.py} (92%) diff --git a/gateway/api/repositories/programs.py b/gateway/api/repositories/functions.py similarity index 92% rename from gateway/api/repositories/programs.py rename to gateway/api/repositories/functions.py index d15fbb441..e8959307d 100644 --- a/gateway/api/repositories/programs.py +++ b/gateway/api/repositories/functions.py @@ -7,14 +7,14 @@ from django.db.models import Q -from api.models import Program +from api.models import Program as Function from api.repositories.groups import GroupRepository logger = logging.getLogger("gateway") -class ProgramRepository: +class FunctionRepository: """ The main objective of this class is to manage the access to the model """ @@ -24,7 +24,7 @@ class ProgramRepository: # in the meantime group_repository = GroupRepository() - def get_functions_with_view_permissions(self, author) -> List[Program]: + def get_functions_with_view_permissions(self, author) -> List[Function]: """ Returns all the functions available to the user. This means: - User functions where the user is the author @@ -34,7 +34,7 @@ def get_functions_with_view_permissions(self, author) -> List[Program]: author: Django author from who retrieve the functions Returns: - List[Program]: all the functions available to the user + List[Function]: all the functions available to the user """ view_groups = self.group_repository.get_groups_with_view_permissions_from_user( @@ -43,7 +43,7 @@ def get_functions_with_view_permissions(self, author) -> List[Program]: author_groups_with_view_permissions_criteria = Q(instances__in=view_groups) author_criteria = Q(author=author) - result_queryset = Program.objects.filter( + result_queryset = Function.objects.filter( author_criteria | author_groups_with_view_permissions_criteria ).distinct() @@ -52,7 +52,7 @@ def get_functions_with_view_permissions(self, author) -> List[Program]: return result_queryset - def get_user_functions(self, author) -> List[Program]: + def get_user_functions(self, author) -> List[Function]: """ Returns the user functions available to the user. This means: - User functions where the user is the author @@ -68,7 +68,7 @@ def get_user_functions(self, author) -> List[Program]: author_criteria = Q(author=author) provider_criteria = Q(provider=None) - result_queryset = Program.objects.filter( + result_queryset = Function.objects.filter( author_criteria & provider_criteria ).distinct() @@ -77,7 +77,7 @@ def get_user_functions(self, author) -> List[Program]: return result_queryset - def get_provider_functions_with_run_permissions(self, author) -> List[Program]: + def get_provider_functions_with_run_permissions(self, author) -> List[Function]: """ Returns the provider functions available to the user. This means: - Provider functions where the user has run permissions @@ -96,7 +96,7 @@ def get_provider_functions_with_run_permissions(self, author) -> List[Program]: author_groups_with_run_permissions_criteria = Q(instances__in=run_groups) provider_exists_criteria = ~Q(provider=None) - result_queryset = Program.objects.filter( + result_queryset = Function.objects.filter( author_groups_with_run_permissions_criteria & provider_exists_criteria ).distinct() @@ -105,7 +105,7 @@ def get_provider_functions_with_run_permissions(self, author) -> List[Program]: return result_queryset - def get_user_function_by_title(self, author, title: str) -> Program | None: + def get_user_function_by_title(self, author, title: str) -> Function | None: """ Returns the user function associated to a title: @@ -120,7 +120,7 @@ def get_user_function_by_title(self, author, title: str) -> Program | None: author_criteria = Q(author=author) title_criteria = Q(title=title) - result_queryset = Program.objects.filter( + result_queryset = Function.objects.filter( author_criteria & title_criteria ).first() @@ -135,7 +135,7 @@ def get_user_function_by_title(self, author, title: str) -> Program | None: def get_provider_function_by_title_with_view_permissions( self, author, title: str, provider_name: str - ) -> Program | None: + ) -> Function | None: """ Returns the provider function associated to: - A Function title @@ -162,7 +162,7 @@ def get_provider_function_by_title_with_view_permissions( author_criteria = Q(author=author) title_criteria = Q(title=title, provider__name=provider_name) - result_queryset = Program.objects.filter( + result_queryset = Function.objects.filter( (author_criteria | author_groups_with_view_permissions_criteria) & title_criteria ).first() @@ -179,7 +179,7 @@ def get_provider_function_by_title_with_view_permissions( def get_provider_function_by_title_with_run_permissions( self, author, title: str, provider_name: str - ) -> Program | None: + ) -> Function | None: """ Returns the provider function associated to: - A Function title @@ -206,7 +206,7 @@ def get_provider_function_by_title_with_run_permissions( author_criteria = Q(author=author) title_criteria = Q(title=title, provider__name=provider_name) - result_queryset = Program.objects.filter( + result_queryset = Function.objects.filter( (author_criteria | author_groups_with_run_permissions_criteria) & title_criteria ).first() diff --git a/gateway/api/views/files.py b/gateway/api/views/files.py index 88c65bcdc..8bec08080 100644 --- a/gateway/api/views/files.py +++ b/gateway/api/views/files.py @@ -20,7 +20,7 @@ from rest_framework.response import Response from api.access_policies.providers import ProviderAccessPolicy -from api.repositories.programs import ProgramRepository +from api.repositories.functions import FunctionRepository from api.repositories.providers import ProviderRepository from api.services.file_storage import FileStorage, WorkingDir from api.utils import sanitize_file_name, sanitize_name @@ -51,7 +51,7 @@ class FilesViewSet(viewsets.ViewSet): BASE_NAME = "files" - program_repository = ProgramRepository() + function_repository = FunctionRepository() provider_repository = ProviderRepository() def list(self, request): @@ -75,7 +75,7 @@ def list(self, request): ) function = ( - self.program_repository.get_function_by_title_with_run_permissions( + self.function_repository.get_function_by_title_with_run_permissions( user=request.user, function_title=function_title, provider_name=provider_name, @@ -138,7 +138,7 @@ def provider_list(self, request): ) function = ( - self.program_repository.get_function_by_title_with_run_permissions( + self.function_repository.get_function_by_title_with_run_permissions( user=request.user, function_title=function_title, provider_name=provider_name, @@ -187,7 +187,7 @@ def download(self, request): ) function = ( - self.program_repository.get_function_by_title_with_run_permissions( + self.function_repository.get_function_by_title_with_run_permissions( user=request.user, function_title=function_title, provider_name=provider_name, @@ -266,7 +266,7 @@ def provider_download(self, request): ) function = ( - self.program_repository.get_function_by_title_with_run_permissions( + self.function_repository.get_function_by_title_with_run_permissions( user=request.user, function_title=function_title, provider_name=provider_name, @@ -303,12 +303,10 @@ def provider_download(self, request): @action(methods=["DELETE"], detail=False) def delete(self, request): - """Deletes file uploaded or produced by the programs,""" - # default response for file not found, overwritten if file is found + """Deletes file uploaded or produced by the functions""" tracer = trace.get_tracer("gateway.tracer") ctx = TraceContextTextMapPropagator().extract(carrier=request.headers) with tracer.start_as_current_span("gateway.files.delete", context=ctx): - # look for file in user's folder username = request.user.username file_name = sanitize_file_name(request.query_params.get("file", None)) provider_name = sanitize_name(request.query_params.get("provider")) @@ -322,7 +320,7 @@ def delete(self, request): ) function = ( - self.program_repository.get_function_by_title_with_run_permissions( + self.function_repository.get_function_by_title_with_run_permissions( user=request.user, function_title=function_title, provider_name=provider_name, @@ -358,12 +356,10 @@ def delete(self, request): @action(methods=["DELETE"], detail=False, url_path="provider/delete") def provider_delete(self, request): - """Deletes file uploaded or produced by the programs,""" - # default response for file not found, overwritten if file is found + """Deletes file uploaded or produced by the functions""" tracer = trace.get_tracer("gateway.tracer") ctx = TraceContextTextMapPropagator().extract(carrier=request.headers) with tracer.start_as_current_span("gateway.files.delete", context=ctx): - # look for file in user's folder username = request.user.username file_name = sanitize_file_name(request.query_params.get("file")) provider_name = sanitize_name(request.query_params.get("provider")) @@ -391,7 +387,7 @@ def provider_delete(self, request): ) function = ( - self.program_repository.get_function_by_title_with_run_permissions( + self.function_repository.get_function_by_title_with_run_permissions( user=request.user, function_title=function_title, provider_name=provider_name, @@ -446,7 +442,7 @@ def upload(self, request): ) function = ( - self.program_repository.get_function_by_title_with_run_permissions( + self.function_repository.get_function_by_title_with_run_permissions( user=request.user, function_title=function_title, provider_name=provider_name, @@ -511,7 +507,7 @@ def provider_upload(self, request): ) function = ( - self.program_repository.get_function_by_title_with_run_permissions( + self.function_repository.get_function_by_title_with_run_permissions( user=request.user, function_title=function_title, provider_name=provider_name, diff --git a/gateway/api/views/programs.py b/gateway/api/views/programs.py index 2f8d9159b..e2407e6b3 100644 --- a/gateway/api/views/programs.py +++ b/gateway/api/views/programs.py @@ -20,7 +20,7 @@ from rest_framework import viewsets, status from rest_framework.response import Response -from api.repositories.programs import ProgramRepository +from api.repositories.functions import FunctionRepository from api.utils import sanitize_name from api.serializers import ( JobConfigSerializer, @@ -56,7 +56,7 @@ class ProgramViewSet(viewsets.GenericViewSet): BASE_NAME = "programs" - program_repository = ProgramRepository() + program_repository = FunctionRepository() @staticmethod def get_serializer_job_config(*args, **kwargs):