Skip to content

Commit

Permalink
Project en vak view volledig geimplementeerd
Browse files Browse the repository at this point in the history
  • Loading branch information
ticoucke committed Mar 6, 2024
1 parent 087a526 commit 3e3e602
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 67 deletions.
2 changes: 1 addition & 1 deletion api/models/gebruiker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion api/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

12 changes: 11 additions & 1 deletion api/models/vak.py
Original file line number Diff line number Diff line change
@@ -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)
30 changes: 28 additions & 2 deletions api/serializers/vak.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 9 additions & 9 deletions api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@
path('api/gebruikers/', gebruiker_list),
path('api/gebruikers/<int:id>/', gebruiker_detail),
path('api/vakken/', vak_list),
path('api/vakken/<int:id>', vak_detail),
path('api/projecten', project_list),
path('api/projecten/<int:id>', project_detail),
path('api/indieningen', indiening_list),
path('api/indieningen/<int:id>', indiening_detail),
path('api/scores', score_list),
path('api/scores/<int:id>', score_detail),
path('api/groepen', groep_list),
path('api/groepen/<int:id>', groep_detail)
path('api/vakken/<int:id>/', vak_detail),
path('api/projecten/', project_list),
path('api/projecten/<int:id>/', project_detail),
path('api/indieningen/', indiening_list),
path('api/indieningen/<int:id>/', indiening_detail),
path('api/scores/', score_list),
path('api/scores/<int:id>/', score_detail),
path('api/groepen/', groep_list),
path('api/groepen/<int:id>/', groep_detail)
]

urlpatterns = format_suffix_patterns(urlpatterns)
9 changes: 6 additions & 3 deletions api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
try:
gebruiker = Gebruiker.objects.get(pk=id)
serializer = GebruikerSerializer(gebruiker)
return serializer.data['is_lesgever']
except Gebruiker.DoesNotExist:
return False
72 changes: 33 additions & 39 deletions api/views/gebruiker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

1 change: 1 addition & 0 deletions api/views/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
29 changes: 19 additions & 10 deletions api/views/vak.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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:
Expand All @@ -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()
Expand Down
6 changes: 5 additions & 1 deletion api/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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': [],
Expand Down
3 changes: 3 additions & 0 deletions data/opgaves/opgave_schaakklok.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Dit is de opgave

joepie
3 changes: 3 additions & 0 deletions data/opgaves/opgave_schaakklok_3R5Z3NY.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Dit is de opgave

joepie
3 changes: 3 additions & 0 deletions data/opgaves/opgave_schaakklok_bLp0t9b.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Dit is de opgave

joepie

0 comments on commit 3e3e602

Please sign in to comment.