Skip to content

Commit

Permalink
Merge branch 'tests' of github.com:SELab-2/UGent-4 into tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnoutAllaert committed Mar 13, 2024
2 parents fcdbda6 + 56d3617 commit 926e31b
Show file tree
Hide file tree
Showing 35 changed files with 334 additions and 263 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/django.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ jobs:
pip install -r requirements.txt
- name: Linting API
run: |
flake8 ./api
cd api
flake8 .
cd ..
- name: Run Tests
env:
CLIENT_ID: ${{ secrets.CLIENT_ID }}
Expand Down
2 changes: 1 addition & 1 deletion api/models/gebruiker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ class Gebruiker(models.Model):
is_lesgever = models.BooleanField(default=False)

def __str__(self):
return self.user.first_name + ' ' + self.user.last_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 @@ -3,9 +3,10 @@

class Groep(models.Model):
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)
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.groep_id}"

13 changes: 7 additions & 6 deletions api/models/indiening.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@


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

return f"data/indieningen/indiening_{instance.indiening_id}/{filename}"


class Indiening(models.Model):
indiening_id = models.AutoField(primary_key=True)
groep = models.ForeignKey('Groep', on_delete=models.CASCADE)
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)
indiening = models.ForeignKey("Indiening", on_delete=models.CASCADE)
bestand = models.FileField(upload_to=upload_to)

def __str__(self):
return str(self.bestand.name)
return str(self.bestand.name)
3 changes: 2 additions & 1 deletion api/models/project.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
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}'
return f"data/opgaves/vak_{vak_id}/{filename}"


class Project(models.Model):
Expand Down
2 changes: 1 addition & 1 deletion api/models/score.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class Score(models.Model):
score_id = models.AutoField(primary_key=True)
score = models.SmallIntegerField()
indiening = models.ForeignKey('Indiening', on_delete=models.CASCADE)
indiening = models.ForeignKey("Indiening", on_delete=models.CASCADE)

def __str__(self):
return str(self.score_id)
10 changes: 6 additions & 4 deletions api/models/vak.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
class Vak(models.Model):
vak_id = models.AutoField(primary_key=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)
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.naam


4 changes: 2 additions & 2 deletions api/serializers/gebruiker.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ def create(self, validated_data):
return instance

def update(self, instance, validated_data):
instance.is_lesgever = validated_data.pop('is_lesgever')
instance.is_lesgever = validated_data.pop("is_lesgever")
instance.save()
return instance
return instance
38 changes: 23 additions & 15 deletions api/serializers/groep.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,47 @@ class Meta:
fields = "__all__"

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

instance = Groep.objects.create(**validated_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'], current_group=instance)

students_data = validated_data.pop("studenten")
validate_students(
students_data, validated_data["project"], current_group=instance
)

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

return instance



def validate_students(students_data, project, current_group=None):
student_counts = Counter(students_data)
for student, count in student_counts.items():
if count > 1:
raise serializers.ValidationError(f"Student {student.user.id} zit al in deze groep.")
raise serializers.ValidationError(
f"Student {student.user.id} zit al in deze groep."
)

groepen = Groep.objects.filter(project=project)

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

raise serializers.ValidationError(
"Alle gebruikers in 'studenten' moeten studenten zijn!"
)

for groep in groepen:
if (current_group and groep.groep_id != current_group.groep_id) and groep.studenten.contains(student):
raise serializers.ValidationError(f"Gebruiker {student.user.id} zit al in een groep voor dit project")

if (
current_group and groep.groep_id != current_group.groep_id
) and groep.studenten.contains(student):
raise serializers.ValidationError(
f"Gebruiker {student.user.id} zit al in een groep voor dit project"
)
4 changes: 2 additions & 2 deletions api/serializers/indiening.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
class IndieningSerializer(serializers.ModelSerializer):
class Meta:
model = Indiening
fields = ('__all__')
fields = "__all__"


class IndieningBestandSerializer(serializers.ModelSerializer):
class Meta:
model = IndieningBestand
fields = ('__all__')
fields = "__all__"
12 changes: 6 additions & 6 deletions api/serializers/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@
class ProjectSerializer(serializers.ModelSerializer):
class Meta:
model = Project
fields = '__all__'
fields = "__all__"

def create(self, validated_data):
deadline = validated_data.pop('deadline')
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')
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")
raise serializers.ValidationError("Deadline moet in de toekomst liggen")
28 changes: 16 additions & 12 deletions api/serializers/score.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,29 @@ 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")
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')

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")
18 changes: 11 additions & 7 deletions api/serializers/vak.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ class Meta:
fields = "__all__"

def create(self, validated_data):
students_data = validated_data.pop('studenten')
teachers_data = validated_data.pop('lesgevers')
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)
Expand All @@ -20,8 +20,8 @@ def create(self, validated_data):
return vak

def update(self, instance, validated_data):
students_data = validated_data.pop('studenten', [])
teachers_data = validated_data.pop('lesgevers', [])
students_data = validated_data.pop("studenten", [])
teachers_data = validated_data.pop("lesgevers", [])

validate_students_teachers(students_data, teachers_data)

Expand All @@ -36,8 +36,12 @@ def update(self, instance, validated_data):
def validate_students_teachers(students_data, teachers_data):
for student in students_data:
if student.is_lesgever:
raise serializers.ValidationError("Alle gebruikers in 'studenten' 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 'lesgevers' moeten lesgevers zijn")
raise serializers.ValidationError(
"Alle gebruikers in 'lesgevers' moeten lesgevers zijn"
)
11 changes: 5 additions & 6 deletions api/tests/factories/gebruiker.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import factory
from django.contrib.auth.models import User
from api.models.gebruiker import Gebruiker
from factory.django import DjangoModelFactory
Expand All @@ -9,15 +8,15 @@ class UserFactory(DjangoModelFactory):
class Meta:
model = User

username = Faker('user_name')
first_name = Faker('first_name')
last_name = Faker('last_name')
email = Faker('email')
username = Faker("user_name")
first_name = Faker("first_name")
last_name = Faker("last_name")
email = Faker("email")


class GebruikerFactory(DjangoModelFactory):
class Meta:
model = Gebruiker

user = SubFactory(UserFactory)
is_lesgever = Faker('boolean')
is_lesgever = Faker("boolean")
2 changes: 1 addition & 1 deletion api/tests/factories/groep.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ def studenten(self, create, extracted, **kwargs):
for student in extracted:
self.studenten.add(student)
else:
self.studenten.add(GebruikerFactory(is_lesgever=False))
self.studenten.add(GebruikerFactory(is_lesgever=False))
4 changes: 3 additions & 1 deletion api/tests/factories/indiening.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

fake = Faker()


class IndieningFactory(DjangoModelFactory):
class Meta:
model = Indiening
Expand All @@ -22,10 +23,11 @@ class Meta:
)
)


class IndieningBestandFactory(DjangoModelFactory):
class Meta:
model = IndieningBestand

indiening_bestand_id = factory.Sequence(lambda n: n)
indiening = SubFactory(IndieningFactory)
bestand = FileField(filename='test.txt', data=b"file content")
bestand = FileField(filename="test.txt", data=b"file content")
8 changes: 4 additions & 4 deletions api/tests/factories/project.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import factory
from api.models.project import Project
from django.core.files.base import ContentFile
from factory.django import DjangoModelFactory
from factory import SubFactory
from django.utils import timezone
Expand All @@ -9,18 +8,19 @@

fake = Faker()


class ProjectFactory(DjangoModelFactory):
class Meta:
model = Project

project_id = factory.Sequence(lambda n: n)
titel = factory.Faker('word')
beschrijving = factory.Faker('paragraph')
titel = factory.Faker("word")
beschrijving = factory.Faker("paragraph")
opgave_bestand = factory.django.FileField(data=b"file content")
vak = SubFactory(VakFactory)
deadline = factory.LazyFunction(
lambda: timezone.make_aware(
fake.date_time_between(start_date="+1d", end_date="+30d")
)
)
max_score = factory.Faker('random_int', min=10, max=30)
max_score = factory.Faker("random_int", min=10, max=30)
4 changes: 2 additions & 2 deletions api/tests/factories/score.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ class Meta:
model = Score

score_id = factory.Sequence(lambda n: n)
score = Faker('random_int', min=0, max=100)
indiening = SubFactory(IndieningFactory)
score = Faker("random_int", min=0, max=100)
indiening = SubFactory(IndieningFactory)
Loading

0 comments on commit 926e31b

Please sign in to comment.