Skip to content

Commit

Permalink
refactor programs references to functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Tansito committed Dec 20, 2024
1 parent 22ef963 commit d88c180
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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()

Expand All @@ -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
Expand All @@ -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()

Expand All @@ -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
Expand All @@ -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()

Expand All @@ -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:
Expand All @@ -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()

Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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()
Expand Down
28 changes: 12 additions & 16 deletions gateway/api/views/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -51,7 +51,7 @@ class FilesViewSet(viewsets.ViewSet):

BASE_NAME = "files"

program_repository = ProgramRepository()
function_repository = FunctionRepository()
provider_repository = ProviderRepository()

def list(self, request):
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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"))
Expand All @@ -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,
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions gateway/api/views/programs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit d88c180

Please sign in to comment.