diff --git a/api/models/gebruiker.py b/api/models/gebruiker.py index 7efb7601..c2345671 100644 --- a/api/models/gebruiker.py +++ b/api/models/gebruiker.py @@ -4,7 +4,7 @@ class Gebruiker(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True) - subjects = models.ManyToManyField('Vak', related_name='students_enrolled', blank=True) + subjects = models.ManyToManyField('Vak', related_name='gebruiker_vakken', blank=True) is_lesgever = models.BooleanField(default=False) def __str__(self): diff --git a/api/models/project.py b/api/models/project.py index 010e92e6..45eb6ee8 100644 --- a/api/models/project.py +++ b/api/models/project.py @@ -6,10 +6,11 @@ class Project(models.Model): project_id = models.AutoField(primary_key=True) titel = models.CharField(max_length=100) description = models.TextField() - opgavebestanden = models.FileField(upload_to='opgave/') + opgavebestanden = models.FileField(upload_to='data/opgaves/') vak = models.ForeignKey(Vak, on_delete=models.CASCADE) deadline = models.DateTimeField(null=True) # indiening restricties def __str__(self): return self.titel + \ No newline at end of file diff --git a/api/models/vak.py b/api/models/vak.py index e3d96d6d..47b56752 100644 --- a/api/models/vak.py +++ b/api/models/vak.py @@ -1,12 +1,22 @@ from django.db import models +from django.core.exceptions import ValidationError class Vak(models.Model): vak_id = models.AutoField(primary_key=True) name = models.CharField(max_length=100) students = models.ManyToManyField('Gebruiker', related_name='vak_gebruikers', blank=True) - teachers = models.ManyToManyField('Gebruiker', related_name='vak_lesgevers') + teachers = models.ManyToManyField('Gebruiker', related_name='vak_lesgevers', blank=True) projects = models.ManyToManyField('Project', related_name='vak_projecten', blank=True) def __str__(self): return self.name + + def save(self, *args, **kwargs): + if self.teachers.filter(is_lesgever=False).exists(): + raise ValidationError("Niet alle gebruikers in 'teachers' zijn lesgevers.") + + if self.students.filter(is_lesgever=True).exists(): + raise ValidationError("Niet alle gebruikers in 'students' zijn studenten.") + + super(Vak, self).save(*args, **kwargs) diff --git a/api/serializers/vak.py b/api/serializers/vak.py index f42f9d9e..616156e8 100644 --- a/api/serializers/vak.py +++ b/api/serializers/vak.py @@ -6,6 +6,32 @@ class VakSerializer(serializers.ModelSerializer): class Meta: model = Vak fields = '__all__' - + def create(self, validated_data): - return Vak.objects.create(**validated_data) + students_data = validated_data.pop('students') + teachers_data = validated_data.pop('teachers') + projects_data = validated_data.pop('projects') + vak = Vak.objects.create(**validated_data) + vak.students.set(students_data) + vak.teachers.set(teachers_data) + vak.projects.set(projects_data) + return vak + + def update(self, instance, validated_data): + instance.name = validated_data.pop('name') + + + students_data = validated_data.pop('students', []) + teachers_data = validated_data.pop('teachers', []) + projects_data = validated_data.pop('projects', []) + instance.students.clear() + instance.teachers.clear() + instance.projects.clear() + + instance.students.set(students_data) + instance.teachers.set(teachers_data) + instance.projects.set(projects_data) + + instance.save() + + return instance diff --git a/api/urls.py b/api/urls.py index bb78fb5d..be98723b 100644 --- a/api/urls.py +++ b/api/urls.py @@ -35,15 +35,15 @@ path('api/gebruikers/', gebruiker_list), path('api/gebruikers//', gebruiker_detail), path('api/vakken/', vak_list), - path('api/vakken/', vak_detail), - path('api/projecten', project_list), - path('api/projecten/', project_detail), - path('api/indieningen', indiening_list), - path('api/indieningen/', indiening_detail), - path('api/scores', score_list), - path('api/scores/', score_detail), - path('api/groepen', groep_list), - path('api/groepen/', groep_detail) + path('api/vakken//', vak_detail), + path('api/projecten/', project_list), + path('api/projecten//', project_detail), + path('api/indieningen/', indiening_list), + path('api/indieningen//', indiening_detail), + path('api/scores/', score_list), + path('api/scores//', score_detail), + path('api/groepen/', groep_list), + path('api/groepen//', groep_detail) ] urlpatterns = format_suffix_patterns(urlpatterns) diff --git a/api/utils.py b/api/utils.py index ea18ae67..22fe2e70 100644 --- a/api/utils.py +++ b/api/utils.py @@ -36,6 +36,9 @@ def get_graph_token(): def is_lesgever(id): - gebruiker = Gebruiker.objects.get(pk=id) - serializer = GebruikerSerializer(gebruiker) - return serializer.data['is_lesgever'] \ No newline at end of file + try: + gebruiker = Gebruiker.objects.get(pk=id) + serializer = GebruikerSerializer(gebruiker) + return serializer.data['is_lesgever'] + except Gebruiker.DoesNotExist: + return False \ No newline at end of file diff --git a/api/views/gebruiker.py b/api/views/gebruiker.py index 83ec9976..76215b0b 100644 --- a/api/views/gebruiker.py +++ b/api/views/gebruiker.py @@ -4,56 +4,50 @@ from api.models.gebruiker import Gebruiker from api.serializers.gebruiker import GebruikerSerializer -from api.utils import is_lesgever @api_view(['GET', 'POST']) def gebruiker_list(request): + print(request.META) if request.method == 'GET': - if request.user.is_superuser: - gebruikers = Gebruiker.objects.all() - - if 'is_lesgever' in request.GET and request.GET.get('is_lesgever').lower() in ['true', 'false']: - gebruikers = gebruikers.filter(is_lesgever = (request.GET.get('is_lesgever').lower() == 'true')) - if 'heeft_vak' in request.GET: - try: - vak = eval(request.GET.get('heeft_vak')) - gebruikers = gebruikers.filter(subjects=vak) - except NameError: - pass - - serializer = GebruikerSerializer(gebruikers, many=True) - return Response(serializer.data) - else: - return Response(status=status.HTTP_403_FORBIDDEN) + gebruikers = Gebruiker.objects.all() + + if 'is_lesgever' in request.GET and request.GET.get('is_lesgever').lower() in ['true', 'false']: + gebruikers = gebruikers.filter(is_lesgever = (request.GET.get('is_lesgever').lower() == 'true')) + if 'heeft_vak' in request.GET: + try: + vak = eval(request.GET.get('heeft_vak')) + gebruikers = gebruikers.filter(subjects=vak) + except NameError: + pass + + serializer = GebruikerSerializer(gebruikers, many=True) + return Response(serializer.data) elif request.method == 'POST': - if request.user.is_superuser or is_lesgever(request.user.id): - serializer = GebruikerSerializer(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) - else: - return Response(status=status.HTTP_403_FORBIDDEN) + serializer = GebruikerSerializer(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) @api_view(['GET', 'PUT']) def gebruiker_detail(request, id): - if request.user.id == id: - try: - gebruiker = Gebruiker.objects.get(pk=id) - except Gebruiker.DoesNotExist: - return Response(status=status.HTTP_404_NOT_FOUND) - - if request.method == 'GET': - serializer = GebruikerSerializer(gebruiker) + try: + gebruiker = Gebruiker.objects.get(pk=id) + except Gebruiker.DoesNotExist: + return Response(status=status.HTTP_404_NOT_FOUND) + + if request.method == 'GET': + serializer = GebruikerSerializer(gebruiker) + return Response(serializer.data) + if request.method == 'PUT': + serializer = GebruikerSerializer(gebruiker, data=request.data) + if serializer.is_valid(): + serializer.save() return Response(serializer.data) - elif request.method == 'PUT': - serializer = GebruikerSerializer(gebruiker, data=request.data) - if serializer.is_valid(): - serializer.save() - return Response(serializer.data) - return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + diff --git a/api/views/project.py b/api/views/project.py index 197fe370..5f34eef6 100644 --- a/api/views/project.py +++ b/api/views/project.py @@ -19,6 +19,7 @@ def project_list(request, format=None): 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) @api_view(['GET', 'PUT', 'DELETE']) def project_detail(request, id, format=None): diff --git a/api/views/vak.py b/api/views/vak.py index 5e0dffce..1d266ab8 100644 --- a/api/views/vak.py +++ b/api/views/vak.py @@ -5,6 +5,8 @@ from api.models.vak import Vak from api.serializers.vak import VakSerializer +from django.core.exceptions import ValidationError + @api_view(['GET', 'POST']) def vak_list(request, format=None): @@ -15,13 +17,17 @@ def vak_list(request, format=None): return Response(serializer.data) elif request.method == 'POST': - serializer = VakSerializer(data=request.data) - if serializer.is_valid(): - serializer.save() - return Response(serializer.data, status=status.HTTP_201_CREATED) + try: + serializer = VakSerializer(data=request.data) + if serializer.is_valid(): + serializer.save() + return Response(serializer.data, status=status.HTTP_201_CREATED) + except ValidationError as e: + return Response({'error': e}, status=status.HTTP_400_BAD_REQUEST) + @api_view(['GET', 'PUT', 'DELETE']) -def vak_detail(request, id, format=None): +def vak_detail(request, id, format=None): try: vak = Vak.objects.get(pk=id) except Vak.DoesNotExist: @@ -32,11 +38,14 @@ def vak_detail(request, id, format=None): return Response(serializer.data) elif request.method == 'PUT': - serializer = VakSerializer(vak, data=request.data) - if serializer.is_valid(): - serializer.save() - return Response(serializer.data) - return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + try: + serializer = VakSerializer(vak, data=request.data) + if serializer.is_valid(): + serializer.save() + return Response(serializer.data) + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + except ValidationError as e: + return Response({'error': e}, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': vak.delete() diff --git a/api/views/views.py b/api/views/views.py index 76aad9b2..346502eb 100644 --- a/api/views/views.py +++ b/api/views/views.py @@ -3,10 +3,14 @@ from rest_framework.response import Response from rest_framework.decorators import api_view from api.serializers.gebruiker import GebruikerSerializer -from api.utils import API_URLS +from api.utils import API_URLS, get_graph_token def login_redirect(request): + + + print(get_graph_token()) + gebruiker_post_data = { 'user': request.user.id, 'subjects': [], diff --git a/data/opgaves/opgave_schaakklok.txt b/data/opgaves/opgave_schaakklok.txt new file mode 100644 index 00000000..a613afff --- /dev/null +++ b/data/opgaves/opgave_schaakklok.txt @@ -0,0 +1,3 @@ +Dit is de opgave + +joepie diff --git a/data/opgaves/opgave_schaakklok_3R5Z3NY.txt b/data/opgaves/opgave_schaakklok_3R5Z3NY.txt new file mode 100644 index 00000000..a613afff --- /dev/null +++ b/data/opgaves/opgave_schaakklok_3R5Z3NY.txt @@ -0,0 +1,3 @@ +Dit is de opgave + +joepie diff --git a/data/opgaves/opgave_schaakklok_bLp0t9b.txt b/data/opgaves/opgave_schaakklok_bLp0t9b.txt new file mode 100644 index 00000000..a613afff --- /dev/null +++ b/data/opgaves/opgave_schaakklok_bLp0t9b.txt @@ -0,0 +1,3 @@ +Dit is de opgave + +joepie