Skip to content

Commit

Permalink
Merge branch 'develop' into tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnoutAllaert committed Mar 13, 2024
2 parents c7654b3 + f765375 commit ec1ef57
Show file tree
Hide file tree
Showing 30 changed files with 525 additions and 377 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/django.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ jobs:
pip install -r requirements.txt
- name: Linting API
run: |
cd api
flake8 .
cd ..
flake8 ./api
- name: Run Tests
env:
CLIENT_ID: ${{ secrets.CLIENT_ID }}
Expand Down
3 changes: 2 additions & 1 deletion api/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from api.models.vak import Vak
from api.models.groep import Groep
from api.models.project import Project
from api.models.indiening import Indiening
from api.models.indiening import Indiening, IndieningBestand
from api.models.score import Score

admin.site.register(Gebruiker)
Expand All @@ -12,3 +12,4 @@
admin.site.register(Project)
admin.site.register(Indiening)
admin.site.register(Score)
admin.site.register(IndieningBestand)
5 changes: 1 addition & 4 deletions api/models/gebruiker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

class Gebruiker(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
subjects = models.ManyToManyField(
"Vak", related_name="gebruiker_vakken", blank=True
)
is_lesgever = models.BooleanField(default=False)

def __str__(self):
return self.user.first_name
return self.user.first_name + ' ' + self.user.last_name
11 changes: 5 additions & 6 deletions api/models/groep.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@


class Groep(models.Model):
group_id = models.AutoField(primary_key=True)
students = models.ManyToManyField(
"Gebruiker", related_name="groep_studenten", blank=True
)
project = models.ForeignKey("Project", on_delete=models.CASCADE)
groep_id = models.AutoField(primary_key=True)
studenten = models.ManyToManyField('Gebruiker', related_name='groep_studenten', blank=True)
project = models.ForeignKey('Project', on_delete=models.CASCADE)

def __str__(self):
return f"Group {self.group_id}"
return f"Group {self.groep_id}"

19 changes: 15 additions & 4 deletions api/models/indiening.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
from django.db import models


def upload_to(instance, filename):
return f'data/indieningen/indiening_{instance.indiening_id}/{filename}'

class Indiening(models.Model):
indiening_id = models.AutoField(primary_key=True)
indiener = models.ForeignKey("Groep", on_delete=models.CASCADE)
indieningsbestanden = models.FileField(upload_to="uploads/")
tijdstip = models.DateTimeField(null=False)
groep = models.ForeignKey('Groep', on_delete=models.CASCADE)
tijdstip = models.DateTimeField(auto_now_add=True)

def __str__(self):
return self.tijdstip
return str(self.indiening_id)


class IndieningBestand(models.Model):
indiening_bestand_id = models.AutoField(primary_key=True)
indiening = models.ForeignKey('Indiening', on_delete=models.CASCADE)
bestand = models.FileField(upload_to=upload_to)

def __str__(self):
return str(self.bestand.name)
9 changes: 7 additions & 2 deletions api/models/project.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
from django.db import models
from .vak import Vak

def upload_to(instance, filename):
vak_id = instance.vak.vak_id
return f'data/opgaves/vak_{vak_id}/{filename}'


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="data/opgaves/")
beschrijving = models.TextField()
opgave_bestand = models.FileField(upload_to=upload_to)
vak = models.ForeignKey(Vak, on_delete=models.CASCADE)
deadline = models.DateTimeField(null=True)
max_score = models.IntegerField(default=20)
# indiening restricties

def __str__(self):
Expand Down
6 changes: 3 additions & 3 deletions api/models/score.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@


class Score(models.Model):
score_id = models.AutoField(primary_key=True)
score = models.SmallIntegerField()
indiening = models.ForeignKey("Indiening", on_delete=models.CASCADE)
groep = models.ForeignKey("Groep", on_delete=models.CASCADE)
indiening = models.ForeignKey('Indiening', on_delete=models.CASCADE)

def __str__(self):
return str(self.score)
return self.score_id
14 changes: 6 additions & 8 deletions api/models/vak.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@

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", blank=True
)
naam = models.CharField(max_length=100)
studenten = models.ManyToManyField('Gebruiker', related_name='vak_gebruikers', blank=True)
lesgevers = models.ManyToManyField('Gebruiker', related_name='vak_lesgevers', blank=True)

def __str__(self):
return self.name
return self.naam


33 changes: 2 additions & 31 deletions api/serializers/gebruiker.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,10 @@ class Meta:
fields = "__all__"

def create(self, validated_data):
subjects_data = validated_data.pop("subjects")
instance = Gebruiker.objects.create(**validated_data)
instance.subjects.set(subjects_data)

populate_subject_gebruikers(instance)

return instance

def update(self, instance, validated_data):
instance.is_lesgever = validated_data.pop("is_lesgever", False)

subjects_data = validated_data.pop("subjects", [])

depopulate_subject_gebruikers(instance)

instance.subjects.clear()
instance.subjects.set(subjects_data)

populate_subject_gebruikers(instance)

instance.is_lesgever = validated_data.pop('is_lesgever')
instance.save()
return instance


def populate_subject_gebruikers(gebruiker):
for subject in gebruiker.subjects.all():
if gebruiker.is_lesgever:
subject.teachers.add(gebruiker)
else:
subject.students.add(gebruiker)


def depopulate_subject_gebruikers(gebruiker):
for subject in gebruiker.subjects.all():
subject.students.remove(gebruiker)
subject.teachers.remove(gebruiker)
return instance
40 changes: 22 additions & 18 deletions api/serializers/groep.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,33 @@ class Meta:
fields = "__all__"

def create(self, validated_data):
students_data = validated_data.pop("students")

validate_students(students_data)

instance = Groep.objects.create(**validated_data)
instance.students.set(students_data)
return instance

def update(self, validated_data):
students_data = validated_data.pop("students")

validate_students(students_data)

students_data = validated_data.pop('studenten')
validate_students(students_data, validated_data['project'])

instance = Groep.objects.create(**validated_data)
instance.students.set(students_data)
instance.studenten.set(students_data)

return instance

def update(self, instance, validated_data):
students_data = validated_data.pop('studenten')
validate_students(students_data, validated_data['project'])

super().update(instance=instance, validated_data=validated_data)
instance.studenten.set(students_data)
instance.save()

return instance


def validate_students(students_data):

def validate_students(students_data, project):
groepen = Groep.objects.filter(project=project)

for student in students_data:
if student.is_lesgever:
raise serializers.ValidationError(
"Alle gebruikers in 'students' moeten studenten zijn!"
)
raise serializers.ValidationError("Alle gebruikers in 'studenten' moeten studenten zijn!")

for groep in groepen:
if groep.studenten.contains(student):
raise serializers.ValidationError(f"Gebruiker {student.user.id} zit al in een groep voor dit project")
11 changes: 7 additions & 4 deletions api/serializers/indiening.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from rest_framework import serializers
from api.models.indiening import Indiening
from api.models.indiening import Indiening, IndieningBestand


class IndieningSerializer(serializers.ModelSerializer):
class Meta:
model = Indiening
fields = "__all__"
fields = ('__all__')

def create(self, validated_data):
return Indiening.objects.create(**validated_data)

class IndieningBestandSerializer(serializers.ModelSerializer):
class Meta:
model = IndieningBestand
fields = ('__all__')
26 changes: 25 additions & 1 deletion api/serializers/project.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
from rest_framework import serializers
from api.models.project import Project
from django.utils import timezone


class ProjectSerializer(serializers.ModelSerializer):
class Meta:
model = Project
fields = "__all__"
fields = '__all__'

def create(self, validated_data):
deadline = validated_data.pop('deadline')
validate_deadline(deadline)

project = Project.objects.create(**validated_data)
project.deadline = deadline
project.save()
return project

def update(self, instance, validated_data):
deadline = validated_data.pop('deadline')
validate_deadline(deadline)

super().update(instance=instance, validated_data=validated_data)
instance.deadline = deadline
instance.save()
return instance


def validate_deadline(deadline):
if deadline <= timezone.now():
raise serializers.ValidationError("Deadline moet in de toekomst liggen")
21 changes: 21 additions & 0 deletions api/serializers/score.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,25 @@ class Meta:
fields = "__all__"

def create(self, validated_data):
if Score.objects.filter(indiening=validated_data.get('indiening')).exists():
raise serializers.ValidationError("Deze indiening heeft al een bestaande score")
validate_score(validated_data)
return Score.objects.create(**validated_data)

def update(self, instance, validated_data):
validate_score(validated_data)
validate_indiening(instance, validated_data)
super().update(instance=instance, validated_data=validated_data)
instance.save()
return instance

def validate_score(data):
max_score = data.get('indiening').groep.project.max_score
if data['score'] > max_score:
raise serializers.ValidationError(f'Score kan niet hoger zijn dan de maximale score van {max_score}')

def validate_indiening(instance, data):
if instance.indiening != data.get('indiening'):
raise serializers.ValidationError('indiening_id kan niet aangepast worden')


60 changes: 12 additions & 48 deletions api/serializers/vak.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,72 +8,36 @@ class Meta:
fields = "__all__"

def create(self, validated_data):
students_data = validated_data.pop("students")
teachers_data = validated_data.pop("teachers")
projects_data = validated_data.pop("projects")

students_data = validated_data.pop('studenten')
teachers_data = validated_data.pop('lesgevers')

validate_students_teachers(students_data, teachers_data)

vak = Vak.objects.create(**validated_data)
vak.students.set(students_data)
vak.teachers.set(teachers_data)
vak.projects.set(projects_data)

populate_student_teacher_vakken(vak)
vak.studenten.set(students_data)
vak.lesgevers.set(teachers_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", [])
students_data = validated_data.pop('studenten', [])
teachers_data = validated_data.pop('lesgevers', [])

validate_students_teachers(students_data, teachers_data)

depopulate_student_teacher_vakken(instance)

instance.students.clear()
instance.teachers.clear()
instance.projects.clear()

instance.students.set(students_data)
instance.teachers.set(teachers_data)
instance.projects.set(projects_data)
super().update(instance=instance, validated_data=validated_data)
instance.studenten.set(students_data)
instance.lesgevers.set(teachers_data)

instance.save()

populate_student_teacher_vakken(instance)

return instance


def validate_students_teachers(students_data, teachers_data):
for student in students_data:
if student.is_lesgever:
raise serializers.ValidationError(
"Alle gebruikers in 'students' moeten studenten zijn"
)
raise serializers.ValidationError("Alle gebruikers in 'studenten' moeten studenten zijn")

for teacher in teachers_data:
if not teacher.is_lesgever:
raise serializers.ValidationError(
"Alle gebruikers in 'teachers' moeten lesgevers zijn"
)


def populate_student_teacher_vakken(vak):
for student in vak.students.all():
student.subjects.add(vak)

for teacher in vak.teachers.all():
teacher.subjects.add(vak)


def depopulate_student_teacher_vakken(vak):
for student in vak.students.all():
student.subjects.remove(vak)

for teacher in vak.teachers.all():
teacher.subjects.remove(vak)
raise serializers.ValidationError("Alle gebruikers in 'lesgevers' moeten lesgevers zijn")
Loading

0 comments on commit ec1ef57

Please sign in to comment.