Skip to content

Commit

Permalink
Merge pull request #259 from SELab-2/backend_extras
Browse files Browse the repository at this point in the history
Backend extras
  • Loading branch information
Bendemeurichy authored May 18, 2024
2 parents 08567d3 + 393826b commit ea5d908
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 8 deletions.
2 changes: 2 additions & 0 deletions api/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from api.models.indiening import Indiening
from api.models.score import Score
from api.models.restrictie import Restrictie
from api.models.template import Template

admin.site.register(Gebruiker)
admin.site.register(Vak)
Expand All @@ -14,3 +15,4 @@
admin.site.register(Indiening)
admin.site.register(Score)
admin.site.register(Restrictie)
admin.site.register(Template)
1 change: 0 additions & 1 deletion api/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ class Project(models.Model):
deadline = models.DateTimeField(null=True, blank=True)
extra_deadline = models.DateTimeField(null=True, blank=True)
max_score = models.IntegerField(default=20)
aantal_groepen = models.IntegerField(default=10)
max_groep_grootte = models.IntegerField(default=1)
student_groep = models.BooleanField(default=False, blank=True)
zichtbaar = models.BooleanField(default=True, blank=True)
Expand Down
2 changes: 1 addition & 1 deletion api/models/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def upload_to(instance, filename):
Returns:
str: Het pad waar het bestand moet worden opgeslagen.
"""
return f"data/templates/gebruiker_{instance.gebruiker.user.id}/{filename}"
return f"data/templates/gebruiker_{instance.user.id}/{filename}"


class Template(models.Model):
Expand Down
3 changes: 1 addition & 2 deletions api/serializers/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ def update(self, instance, validated_data):
"""
validated_data.pop("max_groep_grootte")
validated_data.pop("project_groep")
validated_data.pop("aantal_groepen")

deadline = validated_data.pop("deadline", instance.deadline)
extra_deadline = validated_data.pop("extra_deadline", instance.extra_deadline)
Expand Down Expand Up @@ -104,7 +103,7 @@ def create_groepen(instance):
except Exception:
pass
else:
for _ in range(instance.aantal_groepen):
for _ in range(len(instance.vak.studenten.all())//instance.max_groep_grootte + 1):
try:
serializer = GroepSerializer(
data={"studenten": [], "project": instance.project_id}
Expand Down
14 changes: 14 additions & 0 deletions api/serializers/vak.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from rest_framework import serializers
from api.models.vak import Vak
from api.models.project import Project
from api.models.groep import Groep
from api.serializers.groep import GroepSerializer


Expand Down Expand Up @@ -86,6 +87,19 @@ def add_students_to_group(instance):
except Exception:
pass

else:
groepen = Groep.objects.filter(project=project.project_id)
nieuwe_groepen = len(instance.studenten.all())//project.max_groep_grootte + 1 - len(groepen)
for _ in range(nieuwe_groepen):
try:
serializer = GroepSerializer(
data={"studenten": [], "project": project.project_id}
)
if serializer.is_valid():
serializer.save()
except Exception:
pass


def validate_students_teachers(students_data, teachers_data):
"""
Expand Down
3 changes: 2 additions & 1 deletion api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
)
from .views.score import score_list, score_detail
from .views.groep import groep_list, groep_detail
from .views.template import template_list, template_detail
from .views.template import template_list, template_detail, template_detail_bestand
from .views.restrictie import (
restrictie_list,
restrictie_detail,
Expand Down Expand Up @@ -84,6 +84,7 @@
),
path("api/templates/", template_list, name="template_list"),
path("api/templates/<int:id>/", template_detail, name="template_detail"),
path("api/templates/<int:id>/template/", template_detail_bestand, name="template_detail_bestand")
]

urlpatterns = format_suffix_patterns(urlpatterns)
1 change: 1 addition & 0 deletions api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"scores": "api/scores",
"projecten": "api/projecten",
"restricties": "api/restricties",
"templates": "api/templates"
}


Expand Down
4 changes: 1 addition & 3 deletions api/views/indiening.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from api.models.vak import Vak
from api.models.project import Project
from api.serializers.indiening import IndieningSerializer
from api.utils import has_permissions, contains, is_lesgever
from api.utils import has_permissions, contains

from django.http import FileResponse

Expand Down Expand Up @@ -155,8 +155,6 @@ def indiening_detail_download_artefacten(request, id, format=None):
return Response(status=status.HTTP_404_NOT_FOUND)

if has_permissions(request.user):
return FileResponse(indiening.artefacten.open(), as_attachment=True)
if is_lesgever(request.user):
try:
return FileResponse(indiening.artefacten.open(), as_attachment=True)
except (ValueError, FileNotFoundError):
Expand Down
33 changes: 33 additions & 0 deletions api/views/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from api.serializers.template import TemplateSerializer
from api.utils import has_permissions

from django.http import FileResponse


@api_view(["GET", "POST"])
def template_list(request, format=None):
Expand Down Expand Up @@ -86,3 +88,34 @@ def template_detail(request, id, format=None):
template.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
return Response(status=status.HTTP_403_FORBIDDEN)


@api_view(["GET"])
def template_detail_bestand(request, id, format=None):
"""
Een view om het bestand van een specifieke tempalte te downloaden of de content in JSON-formaat te krijgen.
Args:
id (int): De primaire sleutel van de template.
format (str, optional): Het gewenste formaat voor de respons. Standaard is None.
Returns:
Response: Json met de inhoud van het bestand,
indien de content parameter of true stond
FileResponse: Attachment met het bestand
"""
try:
template = Template.objects.get(pk=id)
except Template.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)

if has_permissions(request.user):
if request.method == "GET":
bestand = template.bestand.open()
if "content" in request.GET and request.GET.get("content").lower() in [
"true",
"false",
]:
return Response({"content": bestand.read()})
return FileResponse(bestand, as_attachment=True)
return Response(status=status.HTTP_403_FORBIDDEN)

0 comments on commit ea5d908

Please sign in to comment.