Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
paaragon committed Jan 29, 2025
1 parent df4df17 commit 1cd0302
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 20 deletions.
12 changes: 8 additions & 4 deletions gateway/api/access_policies/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
Access policies implementation for Job access
"""
import logging
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from api.models import Job

User = get_user_model()


logger = logging.getLogger("gateway")

Expand All @@ -14,6 +16,7 @@ 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:
"""
Expand All @@ -31,8 +34,7 @@ def can_access(user: User, job: Job) -> bool:
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)
has_access = any(group in provider_groups for group in author_groups)
else:
has_access = user.id == job.author.id

Expand All @@ -58,6 +60,8 @@ def can_save_result(user: User, job: Job) -> bool:
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
"User [%s] has no access to save the result of the job [%s].",
user.username,
job.author,
)
return has_access
1 change: 0 additions & 1 deletion gateway/api/repositories/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Repository implementation for Job model
"""
import logging
from django.db.models import Q
from api.models import Job

logger = logging.getLogger("gateway")
Expand Down
4 changes: 1 addition & 3 deletions gateway/api/services/result_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
"""
import os
import logging
import mimetypes
from typing import Optional, Tuple
from wsgiref.util import FileWrapper
from typing import Optional
from django.conf import settings

logger = logging.getLogger("gateway")
Expand Down
3 changes: 1 addition & 2 deletions gateway/api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ def validate(self, attrs): # pylint: disable=too-many-branches
title = attrs.get("title")
provider = attrs.get("provider", None)
if provider and "/" in title:
raise ValidationError(
"Provider defined in title and in provider fields.")
raise ValidationError("Provider defined in title and in provider fields.")

title_split = title.split("/")
if len(title_split) > 2:
Expand Down
31 changes: 23 additions & 8 deletions gateway/api/views/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,12 @@
endpoint=os.environ.get(
"OTEL_EXPORTER_OTLP_TRACES_ENDPOINT", "http://otel-collector:4317"
),
insecure=bool(
int(os.environ.get("OTEL_EXPORTER_OTLP_TRACES_INSECURE", "0"))),
insecure=bool(int(os.environ.get("OTEL_EXPORTER_OTLP_TRACES_INSECURE", "0"))),
)
)
provider.add_span_processor(otel_exporter)
if bool(int(os.environ.get("OTEL_ENABLED", "0"))):
trace._set_tracer_provider(
provider, log=False) # pylint: disable=protected-access
trace._set_tracer_provider(provider, log=False) # pylint: disable=protected-access


class JobViewSet(viewsets.GenericViewSet):
Expand All @@ -58,17 +56,36 @@ class JobViewSet(viewsets.GenericViewSet):
jobs_repository = JobsRepository()

def get_serializer_class(self):
"""
Returns the default serializer class for the view.
"""
return self.serializer_class

@staticmethod
def get_serializer_job(*args, **kwargs):
"""
Returns a `JobSerializer` instance
"""
return v1_serializers.JobSerializer(*args, **kwargs)

@staticmethod
def get_serializer_job_without_result(*args, **kwargs):
"""
Returns a `JobSerializerWithoutResult` instance
"""
return v1_serializers.JobSerializerWithoutResult(*args, **kwargs)

def get_queryset(self):
"""
Returns a filtered queryset of `Job` objects based on the `filter` query parameter.
- If `filter=catalog`, returns jobs authored by the user with an existing provider.
- If `filter=serverless`, returns jobs authored by the user without a provider.
- Otherwise, returns all jobs authored by the user.
Returns:
QuerySet: A filtered queryset of `Job` objects ordered by creation date (descending).
"""
type_filter = self.request.query_params.get("filter")
if type_filter:
if type_filter == TypeFilter.CATALOG:
Expand Down Expand Up @@ -182,8 +199,7 @@ def logs(self, request, pk=None): # pylint: disable=invalid-name,unused-argumen
if job.program and job.program.provider:
provider_groups = job.program.provider.admin_groups.all()
author_groups = author.groups.all()
has_access = any(
group in provider_groups for group in author_groups)
has_access = any(group in provider_groups for group in author_groups)
if has_access:
return Response({"logs": logs})
return Response({"logs": "No available logs"})
Expand Down Expand Up @@ -216,8 +232,7 @@ def stop(self, request, pk=None): # pylint: disable=invalid-name,unused-argumen
]
)
for runtime_job_entry in runtime_jobs:
jobinstance = service.job(
runtime_job_entry.runtime_job)
jobinstance = service.job(runtime_job_entry.runtime_job)
if jobinstance:
try:
logger.info(
Expand Down
8 changes: 6 additions & 2 deletions gateway/tests/api/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ def test_job_detail(self):
self._authorize()

jobs_response = self.client.get(
reverse("v1:jobs-detail", args=["8317718f-5c0d-4fb6-9947-72e480b8a348"]),
reverse(
"v1:jobs-detail", args=["8317718f-5c0d-4fb6-9947-72e480b8a348"]
),
format="json",
)
self.assertEqual(jobs_response.status_code, status.HTTP_200_OK)
Expand All @@ -107,7 +109,9 @@ def test_job_detail_without_result_file(self):
self._authorize()

jobs_response = self.client.get(
reverse("v1:jobs-detail", args=["57fc2e4d-267f-40c6-91a3-38153272e764"]),
reverse(
"v1:jobs-detail", args=["57fc2e4d-267f-40c6-91a3-38153272e764"]
),
format="json",
)
self.assertEqual(jobs_response.status_code, status.HTTP_200_OK)
Expand Down

0 comments on commit 1cd0302

Please sign in to comment.