diff --git a/.github/workflows/django.yml b/.github/workflows/django.yml index 9e25adcb..18abfe50 100644 --- a/.github/workflows/django.yml +++ b/.github/workflows/django.yml @@ -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 }} diff --git a/api/models/gebruiker.py b/api/models/gebruiker.py index 8b26f94b..891dcf06 100644 --- a/api/models/gebruiker.py +++ b/api/models/gebruiker.py @@ -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 diff --git a/api/models/groep.py b/api/models/groep.py index 79f44a83..e60884ef 100644 --- a/api/models/groep.py +++ b/api/models/groep.py @@ -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}" - diff --git a/api/models/indiening.py b/api/models/indiening.py index 5c1b1097..35e6f0b3 100644 --- a/api/models/indiening.py +++ b/api/models/indiening.py @@ -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) \ No newline at end of file + return str(self.bestand.name) diff --git a/api/models/project.py b/api/models/project.py index 7855b3ba..fa39970e 100644 --- a/api/models/project.py +++ b/api/models/project.py @@ -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): diff --git a/api/models/score.py b/api/models/score.py index fba134b8..544d2349 100644 --- a/api/models/score.py +++ b/api/models/score.py @@ -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) diff --git a/api/models/vak.py b/api/models/vak.py index 77f05be6..574939ff 100644 --- a/api/models/vak.py +++ b/api/models/vak.py @@ -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 - - diff --git a/api/serializers/gebruiker.py b/api/serializers/gebruiker.py index 3fa92518..3d441fb6 100644 --- a/api/serializers/gebruiker.py +++ b/api/serializers/gebruiker.py @@ -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 \ No newline at end of file + return instance diff --git a/api/serializers/groep.py b/api/serializers/groep.py index 5b3e2faa..7fb56ae5 100644 --- a/api/serializers/groep.py +++ b/api/serializers/groep.py @@ -9,18 +9,20 @@ 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() @@ -28,20 +30,26 @@ def update(self, instance, validated_data): 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") - \ No newline at end of file + 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" + ) diff --git a/api/serializers/indiening.py b/api/serializers/indiening.py index 7f4f94be..8362df18 100644 --- a/api/serializers/indiening.py +++ b/api/serializers/indiening.py @@ -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__') \ No newline at end of file + fields = "__all__" diff --git a/api/serializers/project.py b/api/serializers/project.py index 33484740..28f02c89 100644 --- a/api/serializers/project.py +++ b/api/serializers/project.py @@ -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") \ No newline at end of file + raise serializers.ValidationError("Deadline moet in de toekomst liggen") diff --git a/api/serializers/score.py b/api/serializers/score.py index 2797d739..8a1449d4 100644 --- a/api/serializers/score.py +++ b/api/serializers/score.py @@ -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") diff --git a/api/serializers/vak.py b/api/serializers/vak.py index 584020b3..be0bab3c 100644 --- a/api/serializers/vak.py +++ b/api/serializers/vak.py @@ -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) @@ -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) @@ -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") \ No newline at end of file + raise serializers.ValidationError( + "Alle gebruikers in 'lesgevers' moeten lesgevers zijn" + ) diff --git a/api/tests/factories/gebruiker.py b/api/tests/factories/gebruiker.py index bdd4115b..f035ecaa 100644 --- a/api/tests/factories/gebruiker.py +++ b/api/tests/factories/gebruiker.py @@ -1,4 +1,3 @@ -import factory from django.contrib.auth.models import User from api.models.gebruiker import Gebruiker from factory.django import DjangoModelFactory @@ -9,10 +8,10 @@ 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): @@ -20,4 +19,4 @@ class Meta: model = Gebruiker user = SubFactory(UserFactory) - is_lesgever = Faker('boolean') \ No newline at end of file + is_lesgever = Faker("boolean") diff --git a/api/tests/factories/groep.py b/api/tests/factories/groep.py index 39e70471..1602802c 100644 --- a/api/tests/factories/groep.py +++ b/api/tests/factories/groep.py @@ -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)) \ No newline at end of file + self.studenten.add(GebruikerFactory(is_lesgever=False)) diff --git a/api/tests/factories/indiening.py b/api/tests/factories/indiening.py index 03c6fdf9..38d63ece 100644 --- a/api/tests/factories/indiening.py +++ b/api/tests/factories/indiening.py @@ -10,6 +10,7 @@ fake = Faker() + class IndieningFactory(DjangoModelFactory): class Meta: model = Indiening @@ -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") diff --git a/api/tests/factories/project.py b/api/tests/factories/project.py index 3f48a3fb..ccf6d80e 100644 --- a/api/tests/factories/project.py +++ b/api/tests/factories/project.py @@ -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 @@ -9,13 +8,14 @@ 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( @@ -23,4 +23,4 @@ class Meta: fake.date_time_between(start_date="+1d", end_date="+30d") ) ) - max_score = factory.Faker('random_int', min=10, max=30) \ No newline at end of file + max_score = factory.Faker("random_int", min=10, max=30) diff --git a/api/tests/factories/score.py b/api/tests/factories/score.py index fda28772..b47c1a40 100644 --- a/api/tests/factories/score.py +++ b/api/tests/factories/score.py @@ -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) \ No newline at end of file + score = Faker("random_int", min=0, max=100) + indiening = SubFactory(IndieningFactory) diff --git a/api/tests/factories/vak.py b/api/tests/factories/vak.py index 6f871185..ed7806bc 100644 --- a/api/tests/factories/vak.py +++ b/api/tests/factories/vak.py @@ -9,7 +9,7 @@ class VakFactory(DjangoModelFactory): class Meta: model = Vak - naam = Faker('name') + naam = Faker("name") @factory.post_generation def studenten(self, create, extracted, **kwargs): @@ -21,7 +21,7 @@ def studenten(self, create, extracted, **kwargs): self.studenten.add(gebruiker) else: self.studenten.add(GebruikerFactory()) - + @factory.post_generation def lesgevers(self, create, extracted, **kwargs): if not create: @@ -31,4 +31,4 @@ def lesgevers(self, create, extracted, **kwargs): for lesgever in extracted: self.lesgevers.add(lesgever) else: - self.lesgevers.add(GebruikerFactory(is_lesgever=True)) \ No newline at end of file + self.lesgevers.add(GebruikerFactory(is_lesgever=True)) diff --git a/api/tests/models/test_gebruiker.py b/api/tests/models/test_gebruiker.py index 62185a4c..f4bf162b 100644 --- a/api/tests/models/test_gebruiker.py +++ b/api/tests/models/test_gebruiker.py @@ -14,5 +14,7 @@ def test_gebruiker_is_lesgever(self): self.assertEqual(self.gebruiker2.is_lesgever, True) def test_str_method(self): - expected_object_name = self.gebruiker1.user.first_name + " " + self.gebruiker1.user.last_name + expected_object_name = ( + self.gebruiker1.user.first_name + " " + self.gebruiker1.user.last_name + ) self.assertEqual(str(self.gebruiker1), expected_object_name) diff --git a/api/tests/models/test_project.py b/api/tests/models/test_project.py index 7509a0f0..395217c6 100644 --- a/api/tests/models/test_project.py +++ b/api/tests/models/test_project.py @@ -13,4 +13,4 @@ def test_project_vak(self): self.assertIsNotNone(self.project.vak) def test_project_max_score(self): - self.assertTrue(10 <= self.project.max_score <= 30) \ No newline at end of file + self.assertTrue(10 <= self.project.max_score <= 30) diff --git a/api/tests/models/test_vak.py b/api/tests/models/test_vak.py index 5a4af892..e0f94427 100644 --- a/api/tests/models/test_vak.py +++ b/api/tests/models/test_vak.py @@ -8,12 +8,9 @@ def setUp(self): def test_str_method(self): self.assertEqual(str(self.vak), self.vak.naam) - def test_vak_studenten(self): self.assertEqual(self.vak.studenten.count(), 1) - def test_vak_lesgevers(self): self.assertEqual(self.vak.lesgevers.count(), 1) - diff --git a/api/tests/serializers/test_gebruiker.py b/api/tests/serializers/test_gebruiker.py index 0ba77076..a0279b4c 100644 --- a/api/tests/serializers/test_gebruiker.py +++ b/api/tests/serializers/test_gebruiker.py @@ -17,13 +17,13 @@ def test_contains_expected_fields(self): def test_user_field_content(self): data = self.serializer.data self.assertEqual(data["user"], self.user.id) - + def test_is_lesgever_field_content(self): data = self.serializer.data self.assertEqual(data["is_lesgever"], self.gebruiker.is_lesgever) - + def test_create(self): - data = {'user': UserFactory.create().id, 'is_lesgever':False} + data = {"user": UserFactory.create().id, "is_lesgever": False} serializer = GebruikerSerializer(data=data) self.assertTrue(serializer.is_valid()) gebruiker = serializer.save() @@ -34,12 +34,13 @@ def test_update(self): data = self.serializer.data self.assertFalse(data["is_lesgever"]) data["is_lesgever"] = True - serializer = GebruikerSerializer(instance=self.gebruiker, data=data, partial=True) + serializer = GebruikerSerializer( + instance=self.gebruiker, data=data, partial=True + ) self.assertTrue(serializer.is_valid()) self.gebruiker = serializer.save() self.assertTrue(self.gebruiker.is_lesgever) - + def test_validation_for_blank_items(self): serializer = GebruikerSerializer(data={"user": "", "is_lesgever": []}) self.assertRaises(ValidationError, serializer.is_valid, raise_exception=True) - diff --git a/api/tests/serializers/test_groep.py b/api/tests/serializers/test_groep.py index 5818613e..711cbea4 100644 --- a/api/tests/serializers/test_groep.py +++ b/api/tests/serializers/test_groep.py @@ -17,9 +17,7 @@ def test_contains_expected_fields(self): def test_project_field_content(self): data = self.serializer.data - self.assertEqual( - data["project"], self.groep.project.project_id - ) + self.assertEqual(data["project"], self.groep.project.project_id) def test_studenten_field_content(self): data = self.serializer.data @@ -27,12 +25,20 @@ def test_studenten_field_content(self): self.assertEqual(data["studenten"], students) def test_create(self): - data = {'project': self.groep.project.project_id, 'studenten': [GebruikerFactory.create(is_lesgever=False).user.id for _ in range(3)]} + data = { + "project": self.groep.project.project_id, + "studenten": [ + GebruikerFactory.create(is_lesgever=False).user.id for _ in range(3) + ], + } serializer = GroepSerializer(data=data) self.assertTrue(serializer.is_valid()) groep = serializer.save() - self.assertEqual(groep.project.project_id, data['project']) - self.assertEqual(set([student.user.id for student in groep.studenten.all()]), set(data['studenten'])) + self.assertEqual(groep.project.project_id, data["project"]) + self.assertEqual( + set([student.user.id for student in groep.studenten.all()]), + set(data["studenten"]), + ) def test_update(self): data = self.serializer.data @@ -41,7 +47,9 @@ def test_update(self): serializer = GroepSerializer(instance=self.groep, data=data, partial=True) self.assertTrue(serializer.is_valid()) groep = serializer.save() - self.assertEqual([student.user.id for student in groep.studenten.all()], data['studenten']) + self.assertEqual( + [student.user.id for student in groep.studenten.all()], data["studenten"] + ) def test_validation_for_blank_items(self): serializer = GroepSerializer(data={"project": "", "studenten": []}) diff --git a/api/tests/serializers/test_project.py b/api/tests/serializers/test_project.py index 413f71a6..d06e2148 100644 --- a/api/tests/serializers/test_project.py +++ b/api/tests/serializers/test_project.py @@ -7,7 +7,6 @@ from django.core.files import File - class ProjectSerializerTest(APITestCase): def setUp(self): self.project = ProjectFactory.create() @@ -24,7 +23,7 @@ def test_contains_expected_fields(self): "opgave_bestand", "vak", "deadline", - "max_score" + "max_score", ], ) @@ -44,15 +43,13 @@ def test_opgave_bestand_field_content(self): def test_vak_field_content(self): data = self.serializer.data - self.assertEqual( - data["vak"], self.project.vak.vak_id - ) - + self.assertEqual(data["vak"], self.project.vak.vak_id) + def test_max_score_field_content(self): data = self.serializer.data - self.assertGreaterEqual(data['max_score'], 10) - self.assertLessEqual(data['max_score'], 30) - + self.assertGreaterEqual(data["max_score"], 10) + self.assertLessEqual(data["max_score"], 30) + def test_deadline_field_content(self): data = self.serializer.data self.assertEqual(parse(data["deadline"]), self.project.deadline) @@ -69,32 +66,36 @@ def test_validation_for_blank_items(self): } ) self.assertRaises(ValidationError, serializer.is_valid, raise_exception=True) - + def test_create(self): vak = VakFactory.create().vak_id - with open('api/tests/data/test.txt', 'rb') as fp: - data = {"titel": "test project", - "beschrijving": "Dit is een test project.", - "opgave_bestand": File(fp), - "vak": vak, - "deadline": self.serializer.data["deadline"], - "max_score": 20 - } + with open("api/tests/data/test.txt", "rb") as fp: + data = { + "titel": "test project", + "beschrijving": "Dit is een test project.", + "opgave_bestand": File(fp), + "vak": vak, + "deadline": self.serializer.data["deadline"], + "max_score": 20, + } serializer = ProjectSerializer(data=data) self.assertTrue(serializer.is_valid()) project = serializer.save() - self.assertEqual(project.deadline, parse(data['deadline'])) - + self.assertEqual(project.deadline, parse(data["deadline"])) + def test_update(self): - with open('api/tests/data/test.txt', 'rb') as fp: - data = {"titel": "test project", - "beschrijving": "Dit is een test project.", - "opgave_bestand": File(fp), - "vak": self.serializer.data["vak"], - "deadline": self.serializer.data["deadline"], - "max_score": 20 - } - serializer = ProjectSerializer(instance=self.project, data=data, partial=True) + with open("api/tests/data/test.txt", "rb") as fp: + data = { + "titel": "test project", + "beschrijving": "Dit is een test project.", + "opgave_bestand": File(fp), + "vak": self.serializer.data["vak"], + "deadline": self.serializer.data["deadline"], + "max_score": 20, + } + serializer = ProjectSerializer( + instance=self.project, data=data, partial=True + ) self.assertTrue(serializer.is_valid()) project = serializer.save() - self.assertEqual(project.deadline, parse(data['deadline'])) + self.assertEqual(project.deadline, parse(data["deadline"])) diff --git a/api/tests/serializers/test_vak.py b/api/tests/serializers/test_vak.py index 2c1b7e7c..25f7601e 100644 --- a/api/tests/serializers/test_vak.py +++ b/api/tests/serializers/test_vak.py @@ -12,9 +12,7 @@ def setUp(self): def test_contains_expected_fields(self): data = self.serializer.data - self.assertCountEqual( - data.keys(), ["vak_id", "naam", "studenten", "lesgevers"] - ) + self.assertCountEqual(data.keys(), ["vak_id", "naam", "studenten", "lesgevers"]) def test_vak_id_field_content(self): data = self.serializer.data @@ -27,13 +25,15 @@ def test_naam_field_content(self): def test_studenten_field_content(self): data = self.serializer.data self.assertEqual( - set(data["studenten"]), set([student.user.id for student in self.vak_data.studenten.all()]) + set(data["studenten"]), + set([student.user.id for student in self.vak_data.studenten.all()]), ) def test_lesgevers_field_content(self): data = self.serializer.data self.assertEqual( - set(data["lesgevers"]), set([teacher.user.id for teacher in self.vak_data.lesgevers.all()]) + set(data["lesgevers"]), + set([teacher.user.id for teacher in self.vak_data.lesgevers.all()]), ) def test_validation_for_blank_items(self): @@ -48,31 +48,51 @@ def test_validation_for_blank_items(self): self.assertRaises(ValidationError, serializer.is_valid, raise_exception=True) def test_create(self): - students_data = [GebruikerFactory.create(is_lesgever=False).user.id for _ in range(3)] - teachers_data = [GebruikerFactory.create(is_lesgever=True).user.id for _ in range(3)] - data = {'naam': 'test vak', 'studenten': students_data, 'lesgevers': teachers_data} + students_data = [ + GebruikerFactory.create(is_lesgever=False).user.id for _ in range(3) + ] + teachers_data = [ + GebruikerFactory.create(is_lesgever=True).user.id for _ in range(3) + ] + data = { + "naam": "test vak", + "studenten": students_data, + "lesgevers": teachers_data, + } serializer = VakSerializer(data=data) self.assertTrue(serializer.is_valid()) vak = serializer.save() self.assertEqual( - set(students_data), set([student.user.id for student in vak.studenten.all()]) + set(students_data), + set([student.user.id for student in vak.studenten.all()]), ) self.assertEqual( - set(teachers_data), set([teacher.user.id for teacher in vak.lesgevers.all()]) + set(teachers_data), + set([teacher.user.id for teacher in vak.lesgevers.all()]), ) - self.assertEqual(vak.naam, 'test vak') + self.assertEqual(vak.naam, "test vak") def test_update(self): - students_data = [GebruikerFactory.create(is_lesgever=False).user.id for _ in range(3)] - teachers_data = [GebruikerFactory.create(is_lesgever=True).user.id for _ in range(3)] - data = {'naam': 'nieuw vak', 'studenten': students_data, 'lesgevers': teachers_data} + students_data = [ + GebruikerFactory.create(is_lesgever=False).user.id for _ in range(3) + ] + teachers_data = [ + GebruikerFactory.create(is_lesgever=True).user.id for _ in range(3) + ] + data = { + "naam": "nieuw vak", + "studenten": students_data, + "lesgevers": teachers_data, + } serializer = VakSerializer(instance=self.vak_data, data=data, partial=True) self.assertTrue(serializer.is_valid()) vak = serializer.save() self.assertEqual( - set(students_data), set([student.user.id for student in vak.studenten.all()]) + set(students_data), + set([student.user.id for student in vak.studenten.all()]), ) self.assertEqual( - set(teachers_data), set([teacher.user.id for teacher in vak.lesgevers.all()]) + set(teachers_data), + set([teacher.user.id for teacher in vak.lesgevers.all()]), ) - self.assertEqual(vak.naam, 'nieuw vak') \ No newline at end of file + self.assertEqual(vak.naam, "nieuw vak") diff --git a/api/urls.py b/api/urls.py index 23a5d813..9393d63d 100644 --- a/api/urls.py +++ b/api/urls.py @@ -23,30 +23,35 @@ 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, indiening_bestand_list, indiening_bestand_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 urlpatterns = [ - path('.well-known/microsoft-identity-association.json', microsoft_association), - path('admin/', admin.site.urls), - path('oauth2/', include('django_auth_adfs.urls')), - path('login_redirect/', login_redirect), - path('api/', home), - path('api/gebruikers/', gebruiker_list), - path('api/gebruikers//', gebruiker_detail, name='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/indiening_bestanden/', indiening_bestand_list), - path('api/indiening_bestanden//', indiening_bestand_detail), - path('api/scores/', score_list), - path('api/scores//', score_detail), - path('api/groepen/', groep_list), - path('api/groepen//', groep_detail, name='groep_detail') + path(".well-known/microsoft-identity-association.json", microsoft_association), + path("admin/", admin.site.urls), + path("oauth2/", include("django_auth_adfs.urls")), + path("login_redirect/", login_redirect), + path("api/", home), + path("api/gebruikers/", gebruiker_list), + path("api/gebruikers//", gebruiker_detail, name="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/indiening_bestanden/", indiening_bestand_list), + path("api/indiening_bestanden//", indiening_bestand_detail), + path("api/scores/", score_list), + path("api/scores//", score_detail), + path("api/groepen/", groep_list), + path("api/groepen//", groep_detail, naam="groep_detail"), ] urlpatterns = format_suffix_patterns(urlpatterns) diff --git a/api/utils.py b/api/utils.py index 17ac9ead..82436397 100644 --- a/api/utils.py +++ b/api/utils.py @@ -4,13 +4,13 @@ API_URLS = { - 'gebruikers': '/api/gebruikers', - 'vakken': '/api/vakken', - 'groepen': '/api/groepen', - 'indieningen': '/api/indieningen', - 'indiening_bestanden': '/api/indiening_bestanden', - 'scores': 'api/scores', - 'projecten': 'api/projecten' + "gebruikers": "/api/gebruikers", + "vakken": "/api/vakken", + "groepen": "/api/groepen", + "indieningen": "/api/indieningen", + "indiening_bestanden": "/api/indiening_bestanden", + "scores": "api/scores", + "projecten": "api/projecten", } @@ -37,16 +37,19 @@ def get_graph_token(): return response.json() except Exception: return None - + + def is_lesgever(user): if user.is_superuser: return True gebruiker = Gebruiker.objects.get(pk=user.id) return gebruiker.is_lesgever + def contains(lijst, user): gebruiker = Gebruiker.objects.get(pk=user.id) return lijst.all().contains(gebruiker) + def get_gebruiker(user): return Gebruiker.objects.get(pk=user.id) diff --git a/api/views/gebruiker.py b/api/views/gebruiker.py index a5ceaac7..183fffbc 100644 --- a/api/views/gebruiker.py +++ b/api/views/gebruiker.py @@ -8,24 +8,26 @@ from api.utils import is_lesgever - - -@api_view(['GET']) +@api_view(["GET"]) def gebruiker_list(request): - if request.method == 'GET': + if request.method == "GET": if is_lesgever(request.user): gebruikers = Gebruiker.objects.all() else: gebruikers = Gebruiker.objects.filter(user=request.user.id) - - 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 "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") + ) serializer = GebruikerSerializer(gebruikers, many=True) return Response(serializer.data) return Response(status=status.HTTP_403_FORBIDDEN) - + @api_view(["GET", "PUT"]) def gebruiker_detail(request, id): @@ -34,12 +36,12 @@ def gebruiker_detail(request, id): except Gebruiker.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) - if request.method == 'GET': + if request.method == "GET": if is_lesgever(request.user) or id == request.user.id: serializer = GebruikerSerializer(gebruiker) return Response(serializer.data) return Response(status=status.HTTP_403_FORBIDDEN) - elif request.method == 'PUT': + elif request.method == "PUT": if request.user.is_superuser: serializer = GebruikerSerializer(gebruiker, data=request.data) if serializer.is_valid(): @@ -47,7 +49,3 @@ def gebruiker_detail(request, id): return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) return Response(status=status.HTTP_403_FORBIDDEN) - - - - diff --git a/api/views/groep.py b/api/views/groep.py index 3b7b95e7..2dbbf4d6 100644 --- a/api/views/groep.py +++ b/api/views/groep.py @@ -9,7 +9,7 @@ @api_view(["GET", "POST"]) def groep_list(request, format=None): - if request.method == 'GET': + if request.method == "GET": if is_lesgever(request.user): groepen = Groep.objects.all() else: @@ -24,7 +24,7 @@ def groep_list(request, format=None): if "student" in request.GET: try: - student = eval(request.GET.get('student')) + student = eval(request.GET.get("student")) groepen = groepen.filter(studenten=student) except NameError: return Response(status=status.HTTP_400_BAD_REQUEST) @@ -32,8 +32,7 @@ def groep_list(request, format=None): serializer = GroepSerializer(groepen, many=True) return Response(serializer.data) - - elif request.method == 'POST': + elif request.method == "POST": if is_lesgever(request.user): serializer = GroepSerializer(data=request.data) if serializer.is_valid(): @@ -41,28 +40,29 @@ def groep_list(request, format=None): return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) return Response(status=status.HTTP_403_FORBIDDEN) - -@api_view(['GET', 'PUT', 'DELETE']) -def groep_detail(request, id, format=None): + + +@api_view(["GET", "PUT", "DELETE"]) +def groep_detail(request, id, format=None): try: groep = Groep.objects.get(pk=id) except Groep.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) - if request.method == 'GET': + if request.method == "GET": if is_lesgever(request.user) or contains(groep.studenten, request.user): serializer = GroepSerializer(groep) return Response(serializer.data) return Response(status=status.HTTP_403_FORBIDDEN) - + if is_lesgever(request.user): - if request.method == 'PUT': + if request.method == "PUT": serializer = GroepSerializer(groep, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) - - elif request.method == 'DELETE': + + elif request.method == "DELETE": groep.delete() return Response(status=status.HTTP_204_NO_CONTENT) - return Response(status=status.HTTP_403_FORBIDDEN) \ No newline at end of file + return Response(status=status.HTTP_403_FORBIDDEN) diff --git a/api/views/indiening.py b/api/views/indiening.py index 55a94142..7d07469e 100644 --- a/api/views/indiening.py +++ b/api/views/indiening.py @@ -10,7 +10,7 @@ @api_view(["GET", "POST"]) def indiening_list(request, format=None): - if request.method == 'GET': + if request.method == "GET": if is_lesgever(request.user): indieningen = Indiening.objects.all() else: @@ -19,18 +19,21 @@ def indiening_list(request, format=None): if "groep" in request.GET: try: - groep = eval(request.GET.get('groep')) + groep = eval(request.GET.get("groep")) indieningen = indieningen.filter(groep=groep) except NameError: return Response(status=status.HTTP_400_BAD_REQUEST) serializer = IndieningSerializer(indieningen, many=True) return Response(serializer.data) - - elif request.method == 'POST': - if 'indiening_bestanden' not in request.FILES: - return Response({"indiening_bestanden":["This field is required."]}, status=status.HTTP_400_BAD_REQUEST) - + + elif request.method == "POST": + if "indiening_bestanden" not in request.FILES: + return Response( + {"indiening_bestanden": ["This field is required."]}, + status=status.HTTP_400_BAD_REQUEST, + ) + serializer = IndieningSerializer(data=request.data) if serializer.is_valid(): serializer.save() @@ -38,49 +41,54 @@ def indiening_list(request, format=None): else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) - for file in request.FILES.getlist('indiening_bestanden'): - IndieningBestand.objects.create(indiening = indiening, bestand = file) + for file in request.FILES.getlist("indiening_bestanden"): + IndieningBestand.objects.create(indiening=indiening, bestand=file) return Response(serializer.data, status=status.HTTP_201_CREATED) - - -@api_view(['GET', 'DELETE']) -def indiening_detail(request, id, format=None): + +@api_view(["GET", "DELETE"]) +def indiening_detail(request, id, format=None): try: indiening = Indiening.objects.get(pk=id) except Indiening.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) - if request.method == 'GET': - if is_lesgever(request.user) or contains(indiening.groep.studenten, request.user): + if request.method == "GET": + if is_lesgever(request.user) or contains( + indiening.groep.studenten, request.user + ): serializer = IndieningSerializer(indiening) return Response(serializer.data) return Response(status=status.HTTP_403_FORBIDDEN) - - elif request.method == 'DELETE': - if is_lesgever(request.user) or contains(indiening.groep.studenten, request.user): + + elif request.method == "DELETE": + if is_lesgever(request.user) or contains( + indiening.groep.studenten, request.user + ): indiening.delete() return Response(status=status.HTTP_204_NO_CONTENT) return Response(status=status.HTTP_403_FORBIDDEN) - -@api_view(['GET']) +@api_view(["GET"]) def indiening_bestand_list(request, format=None): - if request.method == 'GET': + if request.method == "GET": if is_lesgever(request.user): indieningen_bestanden = IndieningBestand.objects.all() else: groepen = Groep.objects.filter(studenten=request.user.id) indieningen = Indiening.objects.filter(groep__in=groepen) - indieningen_bestanden = IndieningBestand.objects.filter(indiening__in=indieningen) - + indieningen_bestanden = IndieningBestand.objects.filter( + indiening__in=indieningen + ) if "indiening" in request.GET: try: - indiening = eval(request.GET.get('indiening')) - indieningen_bestanden = indieningen_bestanden.filter(indiening=indiening) + indiening = eval(request.GET.get("indiening")) + indieningen_bestanden = indieningen_bestanden.filter( + indiening=indiening + ) except NameError: return Response(status=status.HTTP_400_BAD_REQUEST) @@ -88,15 +96,17 @@ def indiening_bestand_list(request, format=None): return Response(serializer.data) -@api_view(['GET']) +@api_view(["GET"]) def indiening_bestand_detail(request, id, format=None): try: indiening_bestand = IndieningBestand.objects.get(pk=id) except IndieningBestand.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) - if request.method == 'GET': - if is_lesgever(request.user) or contains(indiening_bestand.indiening.groep.studenten, request.user): + if request.method == "GET": + if is_lesgever(request.user) or contains( + indiening_bestand.indiening.groep.studenten, request.user + ): serializer = IndieningBestandSerializer(indiening_bestand) return Response(serializer.data) return Response(status=status.HTTP_403_FORBIDDEN) diff --git a/api/views/project.py b/api/views/project.py index 7bd3d390..ffa55647 100644 --- a/api/views/project.py +++ b/api/views/project.py @@ -10,7 +10,7 @@ @api_view(["GET", "POST"]) def project_list(request, format=None): - if request.method == 'GET': + if request.method == "GET": if is_lesgever(request.user): projects = Project.objects.all() else: @@ -26,8 +26,8 @@ def project_list(request, format=None): serializer = ProjectSerializer(projects, many=True) return Response(serializer.data) - - elif request.method == 'POST': + + elif request.method == "POST": if is_lesgever(request.user): serializer = ProjectSerializer(data=request.data) if serializer.is_valid(): @@ -36,28 +36,29 @@ def project_list(request, format=None): return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) return Response(status=status.HTTP_403_FORBIDDEN) -@api_view(['GET', 'PUT', 'DELETE']) -def project_detail(request, id, format=None): + +@api_view(["GET", "PUT", "DELETE"]) +def project_detail(request, id, format=None): try: project = Project.objects.get(pk=id) except Project.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) - if request.method == 'GET': + if request.method == "GET": if is_lesgever(request.user) or contains(project.vak.studenten, request.user): serializer = ProjectSerializer(project) return Response(serializer.data) return Response(status=status.HTTP_403_FORBIDDEN) - + if is_lesgever(request.user): - if request.method == 'PUT': + if request.method == "PUT": serializer = ProjectSerializer(project, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) - - elif request.method == 'DELETE': + + elif request.method == "DELETE": project.delete() return Response(status=status.HTTP_204_NO_CONTENT) return Response(status=status.HTTP_403_FORBIDDEN) diff --git a/api/views/score.py b/api/views/score.py index 49ee79b7..eee21e0b 100644 --- a/api/views/score.py +++ b/api/views/score.py @@ -12,7 +12,7 @@ @api_view(["GET", "POST"]) def score_list(request, format=None): - if request.method == 'GET': + if request.method == "GET": if is_lesgever(request.user): scores = Score.objects.all() else: @@ -22,15 +22,15 @@ def score_list(request, format=None): if "indiening" in request.GET: try: - indiening = eval(request.GET.get('indiening')) + indiening = eval(request.GET.get("indiening")) scores = scores.filter(indiening=indiening) except NameError: return Response(status=status.HTTP_400_BAD_REQUEST) serializer = ScoreSerializer(scores, many=True) return Response(serializer.data) - - elif request.method == 'POST': + + elif request.method == "POST": if is_lesgever(request.user): serializer = ScoreSerializer(data=request.data) if serializer.is_valid(): @@ -38,29 +38,32 @@ def score_list(request, format=None): return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) return Response(status=status.HTTP_403_FORBIDDEN) - -@api_view(['GET', 'PUT', 'DELETE']) -def score_detail(request, id, format=None): + + +@api_view(["GET", "PUT", "DELETE"]) +def score_detail(request, id, format=None): try: score = Score.objects.get(pk=id) except Score.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) - - if request.method == 'GET': - if is_lesgever(request.user) or contains(score.indiening.groep.studenten, request.user): + + if request.method == "GET": + if is_lesgever(request.user) or contains( + score.indiening.groep.studenten, request.user + ): serializer = ScoreSerializer(score) return Response(serializer.data) return Response(status=status.HTTP_403_FORBIDDEN) - if is_lesgever(request.user): - if request.method == 'PUT': + if is_lesgever(request.user): + if request.method == "PUT": serializer = ScoreSerializer(score, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) - - elif request.method == 'DELETE': + + elif request.method == "DELETE": score.delete() return Response(status=status.HTTP_204_NO_CONTENT) return Response(status=status.HTTP_403_FORBIDDEN) diff --git a/api/views/vak.py b/api/views/vak.py index 549c7dbc..23e5f9f2 100644 --- a/api/views/vak.py +++ b/api/views/vak.py @@ -12,7 +12,7 @@ @api_view(["GET", "POST"]) def vak_list(request, format=None): - if request.method == 'GET': + if request.method == "GET": if is_lesgever(request.user): vakken = Vak.objects.all() else: @@ -20,8 +20,8 @@ def vak_list(request, format=None): serializer = VakSerializer(vakken, many=True) return Response(serializer.data) - - elif request.method == 'POST': + + elif request.method == "POST": if is_lesgever(request.user): serializer = VakSerializer(data=request.data) if serializer.is_valid(): @@ -29,22 +29,22 @@ def vak_list(request, format=None): return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) return Response(status=status.HTTP_403_FORBIDDEN) - - -@api_view(['GET', 'PUT', 'DELETE']) + + +@api_view(["GET", "PUT", "DELETE"]) def vak_detail(request, id, format=None): try: vak = Vak.objects.get(pk=id) except Vak.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) - if request.method == 'GET': + if request.method == "GET": if is_lesgever(request.user) or contains(vak.studenten, request.user): serializer = VakSerializer(vak) return Response(serializer.data) return Response(status=status.HTTP_403_FORBIDDEN) if is_lesgever(request.user): - if request.method == 'PUT': + if request.method == "PUT": try: serializer = VakSerializer(vak, data=request.data) if serializer.is_valid(): @@ -52,9 +52,9 @@ def vak_detail(request, id, format=None): 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': + return Response({"error": e}, status=status.HTTP_400_BAD_REQUEST) + + elif request.method == "DELETE": vak.delete() return Response(status=status.HTTP_204_NO_CONTENT) return Response(status=status.HTTP_403_FORBIDDEN) diff --git a/api/views/views.py b/api/views/views.py index 51deac6d..c567a3ba 100644 --- a/api/views/views.py +++ b/api/views/views.py @@ -8,8 +8,6 @@ def login_redirect(request): - print(get_graph_token()) - gebruiker_post_data = { "user": request.user.id, "subjects": [],