-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: download submission * feat: download log files * chore: add translations * chore: I hate polymorphic types * fix: right url to download * chore: linter
- Loading branch information
Showing
8 changed files
with
196 additions
and
40 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
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 |
---|---|---|
|
@@ -8,7 +8,7 @@ msgid "" | |
msgstr "" | ||
"Project-Id-Version: PACKAGE VERSION\n" | ||
"Report-Msgid-Bugs-To: \n" | ||
"POT-Creation-Date: 2024-05-11 15:01+0200\n" | ||
"POT-Creation-Date: 2024-05-11 18:26+0200\n" | ||
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" | ||
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" | ||
"Language-Team: LANGUAGE <[email protected]>\n" | ||
|
@@ -279,3 +279,11 @@ msgstr "The student was successfully added." | |
#: views/student_view.py:45 | ||
msgid "students.success.destroy" | ||
msgstr "The student was successfully destroyed." | ||
|
||
#: views/submission_view.py:28 | ||
msgid "submission.download.zip" | ||
msgstr "No zip file available." | ||
|
||
#: views/submission_view.py:49 | ||
msgid "extra_check_result.download.log" | ||
msgstr "No log file available." |
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 |
---|---|---|
|
@@ -8,7 +8,7 @@ msgid "" | |
msgstr "" | ||
"Project-Id-Version: PACKAGE VERSION\n" | ||
"Report-Msgid-Bugs-To: \n" | ||
"POT-Creation-Date: 2024-05-11 15:01+0200\n" | ||
"POT-Creation-Date: 2024-05-11 18:26+0200\n" | ||
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" | ||
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" | ||
"Language-Team: LANGUAGE <[email protected]>\n" | ||
|
@@ -280,3 +280,11 @@ msgstr "De student is successvol toegevoegd." | |
#: views/student_view.py:45 | ||
msgid "students.success.destroy" | ||
msgstr "De student is successvol verwijderd." | ||
|
||
#: views/submission_view.py:28 | ||
msgid "submission.download.zip" | ||
msgstr "Geen zip bestand beschikbaar." | ||
|
||
#: views/submission_view.py:49 | ||
msgid "extra_check_result.download.log" | ||
msgstr "Geen log bestand beschikbaar." |
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,59 @@ | ||
from typing import cast | ||
|
||
from api.models.submission import (ExtraCheckResult, StructureCheckResult, | ||
Submission) | ||
from api.permissions.role_permissions import (is_assistant, is_student, | ||
is_teacher) | ||
from authentication.models import User | ||
from rest_framework.permissions import SAFE_METHODS, BasePermission | ||
from rest_framework.request import Request | ||
from rest_framework.views import APIView | ||
|
||
|
||
class SubmissionPermission(BasePermission): | ||
def has_permission(self, request: Request, view: APIView) -> bool: | ||
if request.method not in SAFE_METHODS: | ||
return False | ||
|
||
user: User = cast(User, request.user) | ||
|
||
return user.is_staff or is_teacher(user) or is_assistant(user) | ||
|
||
def has_object_permission(self, request: Request, view: APIView, obj: Submission) -> bool: | ||
if request.method not in SAFE_METHODS: | ||
return False | ||
|
||
user: User = cast(User, request.user) | ||
|
||
if user.is_staff: | ||
return True | ||
|
||
if is_teacher(user) or is_assistant(user): | ||
return True | ||
|
||
return obj.group.students.filter(id=user.id).exists() | ||
|
||
|
||
class StructureCheckResultPermission(SubmissionPermission): | ||
def has_object_permission(self, request: Request, view: APIView, obj: StructureCheckResult) -> bool: | ||
return super().has_object_permission(request, view, obj.submission) | ||
|
||
|
||
class ExtraCheckResultPermission(SubmissionPermission): | ||
def has_object_permission(self, request: Request, view: APIView, obj: ExtraCheckResult) -> bool: | ||
return super().has_object_permission(request, view, obj.submission) | ||
|
||
|
||
class ExtraCheckResultLogPermission(ExtraCheckResultPermission): | ||
def has_object_permission(self, request: Request, view: APIView, obj: ExtraCheckResult) -> bool: | ||
result = super().has_object_permission(request, view, obj) | ||
|
||
if not result: | ||
return False | ||
|
||
user: User = cast(User, request.user) | ||
|
||
if is_student(user): | ||
return obj.extra_check.show_log | ||
|
||
return True |
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
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 |
---|---|---|
@@ -1,11 +1,52 @@ | ||
from rest_framework import viewsets | ||
from api.models.submission import (ExtraCheckResult, StructureCheckResult, | ||
Submission) | ||
from api.permissions.submission_permissions import ( | ||
ExtraCheckResultLogPermission, ExtraCheckResultPermission, | ||
StructureCheckResultPermission, SubmissionPermission) | ||
from api.serializers.submission_serializer import ( | ||
ExtraCheckResultSerializer, StructureCheckResultSerializer, | ||
SubmissionSerializer) | ||
from django.http import FileResponse | ||
from django.utils.translation import gettext as _ | ||
from rest_framework.decorators import action | ||
from rest_framework.mixins import RetrieveModelMixin | ||
|
||
from ..models.submission import Submission | ||
from ..serializers.submission_serializer import SubmissionSerializer | ||
from rest_framework.permissions import IsAdminUser | ||
from rest_framework.response import Response | ||
from rest_framework.viewsets import GenericViewSet | ||
|
||
|
||
# TODO: Permission to ask for logs | ||
class SubmissionViewSet(RetrieveModelMixin, viewsets.GenericViewSet): | ||
class SubmissionViewSet(RetrieveModelMixin, GenericViewSet): | ||
queryset = Submission.objects.all() | ||
serializer_class = SubmissionSerializer | ||
permission_classes = [SubmissionPermission] | ||
|
||
@action(detail=True) | ||
def zip(self, request, **__): | ||
submission: Submission = self.get_object() | ||
|
||
if not submission.zip: | ||
return Response({"message": _("submission.download.zip")}, status=404) | ||
|
||
return FileResponse(open(submission.zip.path, "rb"), as_attachment=True) | ||
|
||
|
||
class StructureCheckResultViewSet(RetrieveModelMixin, GenericViewSet): | ||
queryset = StructureCheckResult.objects.all() | ||
serializer_class = StructureCheckResultSerializer | ||
permission_classes = [StructureCheckResultPermission] | ||
|
||
|
||
class ExtraCheckResultViewSet(RetrieveModelMixin, GenericViewSet): | ||
queryset = ExtraCheckResult.objects.all() | ||
serializer_class = ExtraCheckResultSerializer | ||
permission_classes = [ExtraCheckResultPermission] | ||
|
||
@action(detail=True, permission_classes=[IsAdminUser | ExtraCheckResultLogPermission]) | ||
def log(self, request, **__): | ||
extra_check_result: ExtraCheckResult = self.get_object() | ||
|
||
if not extra_check_result.log_file: | ||
return Response({"message": _("extra_check_result.download.log")}, status=404) | ||
|
||
return FileResponse(open(extra_check_result.log_file.path, "rb"), as_attachment=True) |