diff --git a/api/admin.py b/api/admin.py index 2ec92107..bb5da771 100644 --- a/api/admin.py +++ b/api/admin.py @@ -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) @@ -14,3 +15,4 @@ admin.site.register(Indiening) admin.site.register(Score) admin.site.register(Restrictie) +admin.site.register(Template) diff --git a/api/models/project.py b/api/models/project.py index 87ced63d..321b6cb6 100644 --- a/api/models/project.py +++ b/api/models/project.py @@ -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) diff --git a/api/models/template.py b/api/models/template.py index 5e40a42b..4b7aac6e 100644 --- a/api/models/template.py +++ b/api/models/template.py @@ -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): diff --git a/api/serializers/project.py b/api/serializers/project.py index 4966a7ce..8eb98fda 100644 --- a/api/serializers/project.py +++ b/api/serializers/project.py @@ -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) @@ -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} diff --git a/api/serializers/vak.py b/api/serializers/vak.py index fd92d5eb..5dd9bcae 100644 --- a/api/serializers/vak.py +++ b/api/serializers/vak.py @@ -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 @@ -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): """ diff --git a/api/urls.py b/api/urls.py index ba5dfbba..befc1060 100644 --- a/api/urls.py +++ b/api/urls.py @@ -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, @@ -84,6 +84,7 @@ ), path("api/templates/", template_list, name="template_list"), path("api/templates//", template_detail, name="template_detail"), + path("api/templates//template/", template_detail_bestand, name="template_detail_bestand") ] urlpatterns = format_suffix_patterns(urlpatterns) diff --git a/api/utils.py b/api/utils.py index 3b013dc3..e0011b37 100644 --- a/api/utils.py +++ b/api/utils.py @@ -30,6 +30,7 @@ "scores": "api/scores", "projecten": "api/projecten", "restricties": "api/restricties", + "templates": "api/templates" } diff --git a/api/views/indiening.py b/api/views/indiening.py index f1b446ee..3fd65c97 100644 --- a/api/views/indiening.py +++ b/api/views/indiening.py @@ -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 @@ -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): diff --git a/api/views/template.py b/api/views/template.py index e4400c71..6bcb7ca7 100644 --- a/api/views/template.py +++ b/api/views/template.py @@ -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): @@ -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) \ No newline at end of file