Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Runtimejob API ModelView #1191

Merged
merged 3 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions gateway/api/permissions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Permissions."""

from rest_framework import permissions
from api.models import RuntimeJob


class IsOwner(permissions.BasePermission):
Expand All @@ -9,4 +10,6 @@ class IsOwner(permissions.BasePermission):
"""

def has_object_permission(self, request, view, obj):
if isinstance(obj, RuntimeJob):
return obj.job.author == request.user
return obj.author == request.user
5 changes: 5 additions & 0 deletions gateway/api/v1/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,10 @@
router.register(
r"files", v1_views.FilesViewSet, basename=v1_views.FilesViewSet.BASE_NAME
)
router.register(
r"runtime_jobs",
v1_views.RuntimeJobViewSet,
basename=v1_views.RuntimeJobViewSet.BASE_NAME,
)

urlpatterns = router.urls
17 changes: 16 additions & 1 deletion gateway/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


from api import views
from api.models import Program, Job
from api.models import Program, Job, RuntimeJob
from api.permissions import IsOwner
from . import serializers as v1_serializers
from . import services as v1_services
Expand Down Expand Up @@ -68,3 +68,18 @@ class FilesViewSet(views.FilesViewSet):
"""

permission_classes = [permissions.IsAuthenticated, IsOwner]


class RuntimeJobViewSet(views.RuntimeJobViewSet): # pylint: disable=too-many-ancestors
"""
RuntimeJob view set first version. Use RuntomeJobSerializer V1.
"""

serializer_class = v1_serializers.RuntimeJobSerializer
permission_classes = [permissions.IsAuthenticated, IsOwner]

def get_serializer_class(self):
return v1_serializers.RuntimeJobSerializer

def get_queryset(self):
return RuntimeJob.objects.all().filter(job__author=self.request.user)
16 changes: 15 additions & 1 deletion gateway/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from utils import sanitize_file_path

from .exceptions import InternalServerErrorException, ResourceNotFoundException
from .models import Program, Job
from .models import Program, Job, RuntimeJob
from .ray import get_job_handler
from .serializers import JobSerializer, ExistingProgramSerializer, JobConfigSerializer
from .services import JobService, ProgramService, JobConfigService
Expand Down Expand Up @@ -468,3 +468,17 @@ def upload(self, request): # pylint: disable=invalid-name
destination.write(chunk)
return Response({"message": file_path})
return Response("server error", status=status.HTTP_500_INTERNAL_SERVER_ERROR)


class RuntimeJobViewSet(viewsets.ModelViewSet): # pylint: disable=too-many-ancestors
"""
RuntimeJob ViewSet configuration using ModelViewSet.
"""

BASE_NAME = "runtime_jobs"

def get_serializer_class(self):
return self.serializer_class

def get_queryset(self):
return RuntimeJob.objects.all().filter(job__author=self.request.user)
45 changes: 43 additions & 2 deletions gateway/tests/api/test_v1_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ def test_public(self):
"/api/v1/programs/1a7947f9-6ae8-4e3d-ac1e-e7d608deec82/",
format="json",
)
print(programs_response.json())
public = programs_response.json()["public"]
self.assertEqual(public, False)

Expand All @@ -112,6 +111,48 @@ def test_public(self):
"/api/v1/programs/1a7947f9-6ae8-4e3d-ac1e-e7d608deec82/",
format="json",
)
print(programs_response.json())
public = programs_response.json()["public"]
self.assertEqual(public, True)

def test_runtime_job(self):
"""Tests run existing authorized."""
auth = reverse("rest_login")
response = self.client.post(
auth, {"username": "test_user", "password": "123"}, format="json"
)
token = response.data.get("access")
self.client.credentials(HTTP_AUTHORIZATION="Bearer " + token)

programs_response = self.client.get(
"/api/v1/runtime_jobs/",
format="json",
)
self.assertEqual(programs_response.status_code, status.HTTP_200_OK)
self.assertEqual(programs_response.json().get("count"), 3)

programs_response = self.client.delete(
"/api/v1/runtime_jobs/runtime_job_1/",
format="json",
)
self.assertEqual(programs_response.status_code, status.HTTP_204_NO_CONTENT)

programs_response = self.client.get(
"/api/v1/runtime_jobs/",
format="json",
)
self.assertEqual(programs_response.status_code, status.HTTP_200_OK)
self.assertEqual(programs_response.json().get("count"), 2)

auth = reverse("rest_login")
response = self.client.post(
auth, {"username": "test_user_2", "password": "123"}, format="json"
)
token_2 = response.data.get("access")
self.client.credentials(HTTP_AUTHORIZATION="Bearer " + token_2)

programs_response = self.client.get(
"/api/v1/runtime_jobs/",
format="json",
)
self.assertEqual(programs_response.status_code, status.HTTP_200_OK)
self.assertEqual(programs_response.json().get("count"), 1)
2 changes: 1 addition & 1 deletion gateway/tests/api/test_v1_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,5 @@ def test_create_job(self):
)

self.assertIsNotNone(job)
self.assertEqual(Job.objects.count(), 3)
self.assertEqual(Job.objects.count(), 4)
self.assertEqual(job.status, Job.QUEUED)
55 changes: 54 additions & 1 deletion gateway/tests/fixtures/fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
"is_active": true
}
},
{
"model": "auth.user",
"pk": 2,
"fields": {
"email": "[email protected]",
"username": "test_user_2",
"password": "pbkdf2_sha256$390000$kcex1rxhZg6VVJYkx71cBX$e4ns0xDykbO6Dz6j4nZ4uNusqkB9GVpojyegPv5/9KM=",
"is_active": true
}
},
{
"model": "api.program",
"pk": "1a7947f9-6ae8-4e3d-ac1e-e7d608deec82",
Expand Down Expand Up @@ -41,5 +51,48 @@
"status": "QUEUED",
"author": 1
}
},
{
"model": "api.job",
"pk": "1a7947f9-6ae8-4e3d-ac1e-e7d608deec84",
"fields": {
"program": "1a7947f9-6ae8-4e3d-ac1e-e7d608deec82",
"created": "2023-02-01T15:30:43.281796Z",
"result": "{\"somekey\":1}",
"status": "QUEUED",
"author": 2
}
},
{
"model": "api.runtimejob",
"pk": "runtime_job_1",
"fields": {
"job": "1a7947f9-6ae8-4e3d-ac1e-e7d608deec83",
"runtime_job": "runtime_job_1"
}
},
{
"model": "api.runtimejob",
"pk": "runtime_job_2",
"fields": {
"job": "1a7947f9-6ae8-4e3d-ac1e-e7d608deec83",
"runtime_job": "runtime_job_2"
}
},
{
"model": "api.runtimejob",
"pk": "runtime_job_3",
"fields": {
"job": "1a7947f9-6ae8-4e3d-ac1e-e7d608deec82",
"runtime_job": "runtime_job_3"
}
},
{
"model": "api.runtimejob",
"pk": "runtime_job_4",
"fields": {
"job": "1a7947f9-6ae8-4e3d-ac1e-e7d608deec84",
"runtime_job": "runtime_job_5"
}
}
]
]
Loading