Skip to content

Commit

Permalink
adapt get_functions methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Tansito committed Dec 20, 2024
1 parent 1869aca commit 82aef0d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
19 changes: 10 additions & 9 deletions gateway/api/repositories/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@

from django.db.models import Q

from api.models import (
RUN_PROGRAM_PERMISSION,
VIEW_PROGRAM_PERMISSION,
Program as Function,
)
from api.models import Program as Function

from api.repositories.users import UserRepository


Expand All @@ -28,7 +25,9 @@ class FunctionRepository:
# in the meantime
user_repository = UserRepository()

def get_functions_with_view_permissions(self, author) -> List[Function]:
def get_functions_by_permission(
self, author, permission_name: str
) -> List[Function]:
"""
Returns all the functions available to the user. This means:
- User functions where the user is the author
Expand All @@ -42,7 +41,7 @@ def get_functions_with_view_permissions(self, author) -> List[Function]:
"""

view_groups = self.user_repository.get_groups_by_permissions(
user=author, permission_name=VIEW_PROGRAM_PERMISSION
user=author, permission_name=permission_name
)
author_groups_with_view_permissions_criteria = Q(instances__in=view_groups)
author_criteria = Q(author=author)
Expand Down Expand Up @@ -81,7 +80,9 @@ def get_user_functions(self, author) -> List[Function]:

return result_queryset

def get_provider_functions_with_run_permissions(self, author) -> List[Function]:
def get_provider_functions_by_permission(
self, author, permission_name: str
) -> List[Function]:
"""
Returns the provider functions available to the user. This means:
- Provider functions where the user has run permissions
Expand All @@ -95,7 +96,7 @@ def get_provider_functions_with_run_permissions(self, author) -> List[Function]:
"""

run_groups = self.user_repository.get_groups_by_permissions(
user=author, permission_name=RUN_PROGRAM_PERMISSION
user=author, permission_name=permission_name
)
author_groups_with_run_permissions_criteria = Q(instances__in=run_groups)
provider_exists_criteria = ~Q(provider=None)
Expand Down
8 changes: 4 additions & 4 deletions gateway/api/views/programs.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,14 @@ def list(self, request):
# Catalog filter only returns providers functions that user has access:
# author has view permissions and the function has a provider assigned
functions = (
self.program_repository.get_provider_functions_with_run_permissions(
author
self.program_repository.get_provider_functions_by_permission(
author, permission_name=RUN_PROGRAM_PERMISSION
)
)
else:
# If filter is not applied we return author and providers functions together
functions = self.program_repository.get_functions_with_view_permissions(
author
functions = self.program_repository.get_functions_by_permission(
author, permission_name=VIEW_PROGRAM_PERMISSION
)

serializer = self.get_serializer(functions, many=True)
Expand Down

0 comments on commit 82aef0d

Please sign in to comment.