-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat/save result into result store (#1570)
* add job access polocies * fix lint * apply review suggestions * fix: create job repository * fix job id * fix result asignment * fix test and lint * feat: retrieve job results from result storage * remove result file * retrieve job results * apply review suggestions * fix typo * add test fake file * fix test media path for retrieve result * remove prints * fix lint * remove unnecesary file * revert file deletion
- Loading branch information
Showing
7 changed files
with
277 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
""" | ||
Access policies implementation for Job access | ||
""" | ||
import logging | ||
from django.contrib.auth import get_user_model | ||
from api.models import Job | ||
|
||
User = get_user_model() | ||
|
||
|
||
logger = logging.getLogger("gateway") | ||
|
||
|
||
class JobAccessPolocies: # pylint: disable=too-few-public-methods | ||
""" | ||
The main objective of this class is to manage the access for the user | ||
to the Job entities. | ||
""" | ||
|
||
@staticmethod | ||
def can_access(user: User, job: Job) -> bool: | ||
""" | ||
Checks if the user has access to save the result of a Job: | ||
Args: | ||
user: Django user from the request | ||
job: Job instance against to check the access | ||
Returns: | ||
bool: True or False in case the user has access | ||
""" | ||
|
||
is_provider_job = job.program and job.program.provider | ||
if is_provider_job: | ||
provider_groups = job.program.provider.admin_groups.all() | ||
author_groups = user.groups.all() | ||
has_access = any(group in provider_groups for group in author_groups) | ||
else: | ||
has_access = user.id == job.author.id | ||
|
||
if not has_access: | ||
logger.warning( | ||
"User [%s] has no access to job [%s].", user.username, job.author | ||
) | ||
return has_access | ||
|
||
@staticmethod | ||
def can_save_result(user: User, job: Job) -> bool: | ||
""" | ||
Checks if the user has permissions to save the result of a job: | ||
Args: | ||
user: Django user from the request | ||
job: Job instance against to check the permission | ||
Returns: | ||
bool: True or False in case the user has permissions | ||
""" | ||
|
||
has_access = user.id == job.author.id | ||
if not has_access: | ||
logger.warning( | ||
"User [%s] has no access to save the result of the job [%s].", | ||
user.username, | ||
job.author, | ||
) | ||
return has_access |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
""" | ||
Repository implementation for Job model | ||
""" | ||
import logging | ||
from api.models import Job | ||
|
||
logger = logging.getLogger("gateway") | ||
|
||
|
||
class JobsRepository: # pylint: disable=too-few-public-methods | ||
""" | ||
The main objective of this class is to manage the access to the Job model | ||
""" | ||
|
||
def get_job_by_id(self, job_id: str) -> Job: | ||
""" | ||
Returns the job for the given id: | ||
Args: | ||
id (str): id of the job | ||
Returns: | ||
Job | None: job with the requested id | ||
""" | ||
|
||
result_queryset = Job.objects.filter(id=job_id).first() | ||
|
||
if result_queryset is None: | ||
logger.warning("Job [%s] was not found", id) | ||
|
||
return result_queryset |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.