diff --git a/backend/api/serializers/project_serializer.py b/backend/api/serializers/project_serializer.py index b4493229..caa2cec9 100644 --- a/backend/api/serializers/project_serializer.py +++ b/backend/api/serializers/project_serializer.py @@ -2,6 +2,7 @@ from api.models.group import Group from api.models.project import Project from api.models.submission import Submission, ExtraCheckResult, StructureCheckResult, StateEnum +from api.models.checks import ExtraCheck, StructureCheck from api.serializers.course_serializer import CourseSerializer from django.core.files.uploadedfile import InMemoryUploadedFile from django.utils import timezone @@ -33,6 +34,22 @@ def to_representation(self, instance: Project): if (groups_submitted > non_empty_groups): non_empty_groups = groups_submitted + extra_checks_count = instance.extra_checks.count() + + if extra_checks_count: + passed_extra_checks_submission_ids = ExtraCheckResult.objects.filter( + submission__group__project=instance, + submission__is_valid=True, + result=StateEnum.SUCCESS + ).values_list('submission__id', flat=True) + + passed_extra_checks_group_ids = Submission.objects.filter( + id__in=passed_extra_checks_submission_ids + ).values_list('group_id', flat=True) + + unique_groups = set(passed_extra_checks_group_ids) + extra_checks_passed = len(unique_groups) + passed_structure_checks_submission_ids = StructureCheckResult.objects.filter( submission__group__project=instance, submission__is_valid=True, @@ -46,18 +63,12 @@ def to_representation(self, instance: Project): unique_groups = set(passed_structure_checks_group_ids) structure_checks_passed = len(unique_groups) - passed_extra_checks_submission_ids = ExtraCheckResult.objects.filter( - submission__group__project=instance, - submission__is_valid=True, - result=StateEnum.SUCCESS - ).values_list('submission__id', flat=True) - - passed_extra_checks_group_ids = Submission.objects.filter( - id__in=passed_extra_checks_submission_ids - ).values_list('group_id', flat=True) + # If there are no extra checks, we can set extra_checks_passed equal to structure_checks_passed + if not extra_checks_count: + extra_checks_passed = structure_checks_passed - unique_groups = set(passed_extra_checks_group_ids) - extra_checks_passed = len(unique_groups) + # If the extra checks succeed, the structure checks also succeed + structure_checks_passed -= extra_checks_passed # The total number of passed extra checks combined with the number of passed structure checks # can never exceed the total number of submissions (the seeder does not account for this restriction)