def test_user_profile_update_new_teachers_validations(self):
        self.client.login(username=self.principal.username, password='******')
        url = self.build_url(self.teacher_profile.id)

        subject1 = SubjectFactory()
        subject2 = SubjectFactory()
        self.teacher_profile.taught_subjects.add(subject1, subject2)

        study_class = StudyClassFactory(school_unit=self.school_unit)
        teacher_class_through1 = TeacherClassThroughFactory(study_class=study_class, teacher=self.teacher_profile, subject=subject1)

        # Incompatible field
        for taught_subjects in [[subject1.id, subject2.id], [subject1.id]]:
            self.teacher_data['taught_subjects'] = taught_subjects
            self.teacher_data['new_teachers'] = [
                {
                    "id": teacher_class_through1.id,
                    "teacher": self.teacher_profile.id,
                }
            ]
            response = self.client.put(url, self.teacher_data)
            self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
            self.assertEqual(response.data['new_teachers'], ['This field is incompatible with the request data.'])

        teacher_class_through2 = TeacherClassThroughFactory(study_class=study_class, teacher=self.teacher_profile, subject=subject2)
        new_teacher = UserProfileFactory(user_role=UserProfile.UserRoles.TEACHER, school_unit=self.school_unit)

        # Missing the required teacher
        response = self.client.put(url, self.teacher_data)
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(response.data['new_teachers'], ['There must be provided teachers for all classes for this subject.'])

        # Duplicated teachers
        self.teacher_data['new_teachers'] = [
            {
                "id": teacher_class_through2.id,
                "teacher": new_teacher.id,
            },
            {
                "id": teacher_class_through2.id,
                "teacher": new_teacher.id,
            }
        ]
        response = self.client.put(url, self.teacher_data)
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(response.data['new_teachers'], ['No duplicates allowed.'])

        # Invalid teachers
        inactive_teacher = UserProfileFactory(user_role=UserProfile.UserRoles.TEACHER, school_unit=self.school_unit, is_active=False)
        no_school_teacher = UserProfileFactory(user_role=UserProfile.UserRoles.TEACHER)

        for profile in [self.principal, inactive_teacher, no_school_teacher]:
            self.teacher_data['new_teachers'] = [
                {
                    "id": teacher_class_through2.id,
                    "teacher": profile.id,
                }
            ]
            response = self.client.put(url, self.teacher_data)
            self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
            self.assertEqual(response.data['new_teachers'], ['At least one teacher is invalid.'])

        # Teacher does not teach the subject
        self.teacher_data['new_teachers'] = [
            {
                "id": teacher_class_through2.id,
                "teacher": new_teacher.id,
            }
        ]
        response = self.client.put(url, self.teacher_data)
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(response.data['new_teachers'], ['Teacher {} does not teach {}.'.format(new_teacher.full_name, subject2.name)])

        study_class.class_grade = 'IV'
        study_class.class_grade_arabic = 4
        study_class.save()

        response = self.client.put(url, self.teacher_data)
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(response.data['new_teachers'], ['Teacher {} does not teach {}.'.format(new_teacher.full_name, subject2.name)])
Exemple #2
0
class CanUpdateExaminationGradesTestCase(CommonAPITestCase):
    @classmethod
    def setUpTestData(cls):
        cls.academic_year_calendar = AcademicYearCalendarFactory()
        cls.school_unit = RegisteredSchoolUnitFactory()
        cls.study_class = StudyClassFactory(school_unit=cls.school_unit)
        cls.corigente_event = SchoolEventFactory(
            event_type=SchoolEvent.EventTypes.CORIGENTE,
            academic_year_calendar=cls.academic_year_calendar,
            starts_at=datetime.date(2020, 8, 20),
            ends_at=datetime.date(2020, 8, 27))
        cls.diferente_event = SchoolEventFactory(
            event_type=SchoolEvent.EventTypes.DIFERENTE,
            academic_year_calendar=cls.academic_year_calendar,
            starts_at=datetime.date(2020, 9, 1),
            ends_at=datetime.date(2020, 9, 8))

    def setUp(self):
        self.study_class = StudyClassFactory(school_unit=self.school_unit)

    def test_can_update_examination_grades_no_calendar(self):
        self.academic_year_calendar.delete()
        self.assertFalse(
            can_update_examination_grades(
                self.study_class,
                ExaminationGrade.GradeTypes.SECOND_EXAMINATION))

    def test_can_update_examination_grades_study_class_not_from_current_academic_year(
            self):
        # This is only for 2nd examination grade type
        self.study_class.academic_year -= 1
        self.study_class.save()

        self.assertFalse(
            can_update_examination_grades(
                self.study_class,
                ExaminationGrade.GradeTypes.SECOND_EXAMINATION))

    @data(
        ('corigente_event', ExaminationGrade.GradeTypes.SECOND_EXAMINATION),
        ('diferente_event', ExaminationGrade.GradeTypes.DIFFERENCE),
    )
    @unpack
    def test_can_update_examination_grades_no_event(self, event_param,
                                                    grade_type):
        getattr(self, event_param).delete()
        self.assertFalse(
            can_update_examination_grades(self.study_class, grade_type))

    @patch('django.utils.timezone.now',
           return_value=timezone.datetime(2020, 8, 13).replace(tzinfo=utc))
    def test_can_update_examination_grades_outside_event(self, mocked_method):
        self.assertFalse(
            can_update_examination_grades(
                self.study_class,
                ExaminationGrade.GradeTypes.SECOND_EXAMINATION))
        self.assertFalse(
            can_update_examination_grades(
                self.study_class, ExaminationGrade.GradeTypes.DIFFERENCE))

    @patch('django.utils.timezone.now',
           return_value=timezone.datetime(2020, 8, 20).replace(tzinfo=utc))
    def test_can_update_examination_grades_2nd_exam_success(
            self, mocked_method):
        self.assertTrue(
            can_update_examination_grades(
                self.study_class,
                ExaminationGrade.GradeTypes.SECOND_EXAMINATION))

    @patch('django.utils.timezone.now',
           return_value=timezone.datetime(2020, 9, 8).replace(tzinfo=utc))
    def test_can_update_examination_grades_difference_success(
            self, mocked_method):
        self.assertTrue(
            can_update_examination_grades(
                self.study_class, ExaminationGrade.GradeTypes.DIFFERENCE))

    @patch('django.utils.timezone.now',
           return_value=timezone.datetime(2020, 9, 8).replace(tzinfo=utc))
    def test_can_update_examination_grades_difference_previous_year_success(
            self, mocked_method):
        self.study_class.academic_year = 2019
        self.study_class.save()

        self.assertTrue(
            can_update_examination_grades(
                self.study_class, ExaminationGrade.GradeTypes.DIFFERENCE))
    def test_deactivate_teacher_invalid_teacher(self):
        self.client.login(username=self.principal.username, password='******')

        teacher = UserProfileFactory(user_role=UserProfile.UserRoles.TEACHER, school_unit=self.school_unit)

        subject1 = SubjectFactory(name='Dirigentie', is_coordination=True)
        subject2 = SubjectFactory(name='Subject')
        subject3 = SubjectFactory(name='Another Subject')

        study_class1 = StudyClassFactory(school_unit=self.school_unit, class_master=teacher)
        teacher_class_through1 = TeacherClassThroughFactory(study_class=study_class1, teacher=teacher,
                                                            subject=subject1, is_class_master=True)
        teacher_class_through2 = TeacherClassThroughFactory(study_class=study_class1, teacher=teacher,
                                                            subject=subject2, is_class_master=True)

        study_class2 = StudyClassFactory(school_unit=self.school_unit, class_grade='IV', class_grade_arabic=4)
        teacher_class_through3 = TeacherClassThroughFactory(study_class=study_class2, teacher=teacher,
                                                            subject=subject3, is_class_master=False)

        new_teacher = UserProfileFactory(user_role=UserProfile.UserRoles.TEACHER, school_unit=self.school_unit)

        request_data = {
            'new_teachers': [
                {
                    'id': teacher_class_through1.id,
                    'teacher': 0
                },
                {
                    'id': teacher_class_through2.id,
                    'teacher': new_teacher.id
                },
                {
                    'id': teacher_class_through3.id,
                    'teacher': new_teacher.id
                }
            ]
        }

        response = self.client.post(self.build_url(teacher.id), data=request_data)
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(response.data['new_teachers'][0]['teacher'], [f'Invalid pk "{0}" - object does not exist.'])

        profile1 = UserProfileFactory(user_role=UserProfile.UserRoles.PARENT, school_unit=self.school_unit)
        profile2 = UserProfileFactory(user_role=UserProfile.UserRoles.TEACHER, school_unit=self.school_unit, is_active=False)
        profile3 = UserProfileFactory(user_role=UserProfile.UserRoles.TEACHER, school_unit=RegisteredSchoolUnitFactory())

        for profile in [profile1, profile2, profile3]:
            request_data['new_teachers'][0]['teacher'] = profile.id
            response = self.client.post(self.build_url(teacher.id), data=request_data)
            self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
            self.assertEqual(response.data['new_teachers'], ['At least one teacher is invalid.'])

        request_data['new_teachers'][0]['teacher'] = new_teacher.id
        response = self.client.post(self.build_url(teacher.id), data=request_data)
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(response.data['new_teachers'], ['Teacher {} does not teach {}.'.format(new_teacher.full_name, subject2.name)])

        new_teacher.taught_subjects.add(subject2)
        response = self.client.post(self.build_url(teacher.id), data=request_data)
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(response.data['new_teachers'], ['Teacher {} does not teach {}.'.format(new_teacher.full_name, subject3.name)])

        study_class2.class_master = new_teacher
        study_class2.save()

        response = self.client.post(self.build_url(teacher.id), data=request_data)
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(response.data['new_teachers'], ['Invalid class master.'])