Skip to content

Commit

Permalink
refactor of get_function method
Browse files Browse the repository at this point in the history
  • Loading branch information
Tansito committed Dec 18, 2024
1 parent 2f760ee commit 7b89093
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 55 deletions.
23 changes: 23 additions & 0 deletions gateway/api/repositories/programs.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,26 @@ def get_provider_function_by_title_with_run_permissions(
)

return result_queryset

def get_function_by_title_with_run_permissions(
self, user, function_title: str, provider_name: str | None
) -> None:
"""
This method returns the specified function if the user is
the author of the function or it has run permissions.
Args:
user: Django user of the function that wants to get it
function_title (str): title of the function
provider_name (str | None): name of the provider owner of the function
Returns:
Program | None: returns the function if it exists
"""

if provider_name:
return self.get_provider_function_by_title_with_run_permissions(
author=user, title=function_title, provider_name=provider_name
)

return self.get_user_function_by_title(author=user, title=function_title)
103 changes: 48 additions & 55 deletions gateway/api/views/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,29 +54,6 @@ class FilesViewSet(viewsets.ViewSet):
program_repository = ProgramRepository()
provider_repository = ProviderRepository()

def get_function(
self, user, function_title: str, provider_name: str | None
) -> None:
"""
This method returns the specified function.
Args:
user: Django user of the function that wants to get it
function_title (str): title of the function
provider_name (str | None): name of the provider owner of the function
Returns:
Program | None: returns the function if it exists
"""

if provider_name:
return self.program_repository.get_provider_function_by_title_with_run_permissions(
author=user, title=function_title, provider_name=provider_name
)
return self.program_repository.get_user_function_by_title(
author=user, title=function_title
)

def list(self, request):
"""
It returns a list with the names of available files for the user directory:
Expand All @@ -97,10 +74,12 @@ def list(self, request):
status=status.HTTP_400_BAD_REQUEST,
)

function = self.get_function(
user=request.user,
function_title=function_title,
provider_name=provider_name,
function = (
self.program_repository.get_function_by_title_with_run_permissions(
user=request.user,
function_title=function_title,
provider_name=provider_name,
)
)
if not function:
if provider_name:
Expand Down Expand Up @@ -158,10 +137,12 @@ def provider_list(self, request):
status=status.HTTP_404_NOT_FOUND,
)

function = self.get_function(
user=request.user,
function_title=function_title,
provider_name=provider_name,
function = (
self.program_repository.get_function_by_title_with_run_permissions(
user=request.user,
function_title=function_title,
provider_name=provider_name,
)
)
if not function:
return Response(
Expand Down Expand Up @@ -205,10 +186,12 @@ def download(self, request):
status=status.HTTP_400_BAD_REQUEST,
)

function = self.get_function(
user=request.user,
function_title=function_title,
provider_name=provider_name,
function = (
self.program_repository.get_function_by_title_with_run_permissions(
user=request.user,
function_title=function_title,
provider_name=provider_name,
)
)
if not function:
if provider_name:
Expand Down Expand Up @@ -282,10 +265,12 @@ def provider_download(self, request):
status=status.HTTP_404_NOT_FOUND,
)

function = self.get_function(
user=request.user,
function_title=function_title,
provider_name=provider_name,
function = (
self.program_repository.get_function_by_title_with_run_permissions(
user=request.user,
function_title=function_title,
provider_name=provider_name,
)
)
if not function:
return Response(
Expand Down Expand Up @@ -336,10 +321,12 @@ def delete(self, request):
status=status.HTTP_400_BAD_REQUEST,
)

function = self.get_function(
user=request.user,
function_title=function_title,
provider_name=provider_name,
function = (
self.program_repository.get_function_by_title_with_run_permissions(
user=request.user,
function_title=function_title,
provider_name=provider_name,
)
)

if not function:
Expand Down Expand Up @@ -403,10 +390,12 @@ def provider_delete(self, request):
status=status.HTTP_404_NOT_FOUND,
)

function = self.get_function(
user=request.user,
function_title=function_title,
provider_name=provider_name,
function = (
self.program_repository.get_function_by_title_with_run_permissions(
user=request.user,
function_title=function_title,
provider_name=provider_name,
)
)

if not function:
Expand Down Expand Up @@ -456,10 +445,12 @@ def upload(self, request):
status=status.HTTP_400_BAD_REQUEST,
)

function = self.get_function(
user=request.user,
function_title=function_title,
provider_name=provider_name,
function = (
self.program_repository.get_function_by_title_with_run_permissions(
user=request.user,
function_title=function_title,
provider_name=provider_name,
)
)
if not function:
if provider_name:
Expand Down Expand Up @@ -519,10 +510,12 @@ def provider_upload(self, request):
status=status.HTTP_404_NOT_FOUND,
)

function = self.get_function(
user=request.user,
function_title=function_title,
provider_name=provider_name,
function = (
self.program_repository.get_function_by_title_with_run_permissions(
user=request.user,
function_title=function_title,
provider_name=provider_name,
)
)
if not function:
return Response(
Expand Down

0 comments on commit 7b89093

Please sign in to comment.