Skip to content

Commit

Permalink
Merge pull request #47 from SELab-2/databank_fixes
Browse files Browse the repository at this point in the history
Databank fixes
  • Loading branch information
LGDTimtou authored Mar 12, 2024
2 parents a3374f8 + f083df6 commit bd8b4f0
Show file tree
Hide file tree
Showing 22 changed files with 144 additions and 166 deletions.
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)
3 changes: 1 addition & 2 deletions api/models/gebruiker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +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
7 changes: 4 additions & 3 deletions api/models/groep.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@


class Groep(models.Model):
group_id = models.AutoField(primary_key=True)
students = models.ManyToManyField('Gebruiker', related_name='groep_studenten', blank=True)
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}"

21 changes: 15 additions & 6 deletions api/models/indiening.py
Original file line number Diff line number Diff line change
@@ -1,13 +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)
project = models.ForeignKey('Project', on_delete=models.CASCADE, default='0')
groep = models.ForeignKey('Groep', on_delete=models.CASCADE)
tijdstip = models.DateTimeField(auto_now_add=True)

def __str__(self):
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 self.tijdstip

return str(self.bestand.name)
7 changes: 6 additions & 1 deletion 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/')
opgave_bestanden = 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
4 changes: 2 additions & 2 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)

def __str__(self):
return self.score
return self.score_id
8 changes: 4 additions & 4 deletions api/models/vak.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +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
31 changes: 18 additions & 13 deletions api/serializers/groep.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,33 @@ class Meta:
fields = '__all__'

def create(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, validated_data):
students_data = validated_data.pop('students')
def update(self, instance, validated_data):
students_data = validated_data.pop('studenten')
validate_students(students_data, validated_data['project'])

validate_students(students_data)

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

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")
18 changes: 7 additions & 11 deletions api/serializers/indiening.py
Original file line number Diff line number Diff line change
@@ -1,18 +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):
# indiener is een groep, dus zeker student(en)
indiening = Indiening.objects.create(**validated_data)
return indiening

def update(self, instance, validated_data):
instance = Indiening.objects.create(**validated_data)
instance.save()
return instance

class IndieningBestandSerializer(serializers.ModelSerializer):
class Meta:
model = IndieningBestand
fields = ('__all__')
14 changes: 6 additions & 8 deletions api/serializers/project.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from rest_framework import serializers
from api.models.project import Project

from datetime import datetime
from django.utils import timezone


class ProjectSerializer(serializers.ModelSerializer):
Expand All @@ -11,24 +10,23 @@ class Meta:

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

validate_deadline(deadline)

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

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

validate_deadline(deadline)

instance = Project.objects.create(**validated_data)
instance.deadline.set(deadline)
super().update(instance=instance, validated_data=validated_data)
instance.deadline = deadline
instance.save()
return instance


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

def create(self, validated_data):
score = Score.objects.create(**validated_data)
return score
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):
instance = Score.objects.create(**validated_data)
validate_score(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}')


52 changes: 11 additions & 41 deletions api/serializers/vak.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,65 +8,35 @@ 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")
4 changes: 3 additions & 1 deletion api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from .views.gebruiker import gebruiker_list, gebruiker_detail
from .views.vak import vak_list, vak_detail
from .views.project import project_list, project_detail
from .views.indiening import indiening_list, indiening_detail
from .views.indiening import indiening_list, indiening_detail, indiening_bestand_list, indiening_bestand_detail
from .views.score import score_list, score_detail
from .views.groep import groep_list, groep_detail

Expand All @@ -40,6 +40,8 @@
path('api/projecten/<int:id>/', project_detail),
path('api/indieningen/', indiening_list),
path('api/indieningen/<int:id>/', indiening_detail),
path('api/indiening_bestanden/', indiening_bestand_list),
path('api/indiening_bestanden/<int:id>/', indiening_bestand_detail),
path('api/scores/', score_list),
path('api/scores/<int:id>/', score_detail),
path('api/groepen/', groep_list),
Expand Down
Loading

0 comments on commit bd8b4f0

Please sign in to comment.