-
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.
Merge pull request #89 from SELab-2/restricties
Restricties
- Loading branch information
Showing
14 changed files
with
202 additions
and
31 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from django.db import models | ||
from api.models.project import Project | ||
|
||
|
||
def upload_to(instance, filename): | ||
""" | ||
Functie om het pad te genereren waar het opgavebestand wordt opgeslagen. | ||
Args: | ||
instance: De huidige instantie van het model. | ||
filename (str): De oorspronkelijke bestandsnaam. | ||
Returns: | ||
str: Het pad waar het opgavebestand moet worden opgeslagen. | ||
""" | ||
project_id = instance.project.project_id | ||
return f"data/restricties/project_{project_id}/{filename}" | ||
|
||
|
||
class Restrictie(models.Model): | ||
""" | ||
TODO | ||
""" | ||
|
||
restrictie_id = models.AutoField(primary_key=True) | ||
project = models.ForeignKey(Project, on_delete=models.CASCADE) | ||
script = models.FileField(upload_to=upload_to) | ||
moet_slagen = models.BooleanField(default=False, blank=True) | ||
|
||
def __str__(self): | ||
return self.project.titel + ', restrictie: ' + str(self.script) |
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from rest_framework import serializers | ||
from api.models.restrictie import Restrictie | ||
|
||
|
||
class RestrictieSerializer(serializers.ModelSerializer): | ||
""" | ||
TODO | ||
""" | ||
|
||
class Meta: | ||
model = Restrictie | ||
fields = "__all__" | ||
|
||
def create(self, validated_data): | ||
""" | ||
TODO | ||
""" | ||
|
||
validate_script(validated_data.get('script')) | ||
return Restrictie.objects.create(**validated_data) | ||
|
||
def update(self, instance, validated_data): | ||
""" | ||
TODO | ||
""" | ||
validate_project(instance, validated_data.get('project')) | ||
validate_script(instance, validated_data.get('script')) | ||
|
||
super().update(instance=instance, validated_data=validated_data) | ||
instance.save() | ||
return instance | ||
|
||
|
||
def validate_script(new_script): | ||
if not str(new_script).endswith('.sh') or not str(new_script).endswith('.py'): | ||
raise serializers.ValidationError('Het restrictie script moet een Python of Shell script zijn') | ||
|
||
|
||
def validate_project(instance, new_project): | ||
""" | ||
TODO | ||
""" | ||
|
||
if instance.project != new_project: | ||
raise serializers.ValidationError('Het project van een restrictie kan niet aangepast worden') |
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from rest_framework.decorators import api_view | ||
from rest_framework.response import Response | ||
from rest_framework import status | ||
|
||
from api.models.restrictie import Restrictie | ||
from api.serializers.restrictie import RestrictieSerializer | ||
from api.utils import is_lesgever | ||
|
||
|
||
@api_view(["GET", "POST"]) | ||
def restrictie_list(request, format=None): | ||
""" | ||
TODO | ||
""" | ||
if is_lesgever(request.user): | ||
if request.method == "GET": | ||
restricties = Restrictie.objects.all() | ||
|
||
if "project" in request.GET: | ||
try: | ||
project = eval(request.GET.get("project")) | ||
restricties = restricties.filter(project=project) | ||
except NameError: | ||
return Response(status=status.HTTP_400_BAD_REQUEST) | ||
|
||
if "moet_slagen" in request.GET and request.GET.get("moet_slagen").lower() in [ | ||
"true", | ||
"false", | ||
]: | ||
restricties = restricties.filter( | ||
moet_slagen=(request.GET.get("moet_slagen").lower() == "true") | ||
) | ||
|
||
serializer = RestrictieSerializer(restricties, many=True) | ||
return Response(serializer.data) | ||
|
||
elif request.method == "POST": | ||
serializer = RestrictieSerializer(data=request.data) | ||
if serializer.is_valid(): | ||
serializer.save() | ||
return Response(serializer.data, status=status.HTTP_201_CREATED) | ||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) | ||
return Response(status=status.HTTP_403_FORBIDDEN) | ||
|
||
|
||
@api_view(["GET", "PUT", "DELETE"]) | ||
def restrictie_detail(request, id, format=None): | ||
""" | ||
TODO | ||
""" | ||
try: | ||
restrictie = Restrictie.objects.get(pk=id) | ||
except Restrictie.DoesNotExist: | ||
return Response(status=status.HTTP_404_NOT_FOUND) | ||
|
||
if is_lesgever(request.user): | ||
if request.method == "GET": | ||
serializer = RestrictieSerializer(restrictie) | ||
return Response(serializer.data) | ||
|
||
if request.method == "PUT": | ||
serializer = RestrictieSerializer(restrictie, data=request.data) | ||
if serializer.is_valid(): | ||
serializer.save() | ||
return Response(serializer.data) | ||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) | ||
|
||
elif request.method == "DELETE": | ||
restrictie.delete() | ||
return Response(status=status.HTTP_204_NO_CONTENT) | ||
return Response(status=status.HTTP_403_FORBIDDEN) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.