Beispiel #1
0
    def test_study_class_partially_update_teachers_primary_school(self):
        self.client.login(username=self.principal.username, password='******')

        primary_study_class = StudyClassFactory(school_unit=self.school_unit, class_grade='I', class_grade_arabic=1)
        class_master = primary_study_class.class_master
        TeacherClassThroughFactory(study_class=primary_study_class, teacher=class_master, is_class_master=True)

        teacher = UserProfileFactory(user_role=UserProfile.UserRoles.TEACHER, school_unit=self.school_unit)
        subject = SubjectFactory()
        teacher.taught_subjects.add(subject)
        teacher_class_through = TeacherClassThroughFactory(study_class=primary_study_class, teacher=teacher, is_class_master=False)

        url = self.build_url(primary_study_class.id)
        request_data = {
            'updated_teachers': [
                {
                    'id': teacher_class_through.id,
                    'teacher': class_master.id
                }
            ]
        }

        # This is allowed even if the teacher doesn't have the subject in taught subjects list
        # (because the teacher is the class master)
        response = self.client.patch(url, request_data)
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        teacher_class_through.refresh_from_db()
        self.assertEqual(teacher_class_through.teacher, class_master)
        self.assertTrue(teacher_class_through.is_class_master)
    def test_user_profile_update_taught_subjects(self):
        self.client.login(username=self.principal.username, password='******')
        url = self.build_url(self.teacher_profile.id)

        # Create a few taught subjects
        subject1 = SubjectFactory(name='Sport')
        subject2 = SubjectFactory(name='Religie')

        self.teacher_data['user_role'] = UserProfile.UserRoles.TEACHER
        self.teacher_data['taught_subjects'] = [subject1.id, subject2.id]

        response = self.client.put(url, self.teacher_data)
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        self.teacher_profile.refresh_from_db()
        for attr_name, value in self.teacher_data.items():
            if attr_name not in ['labels', 'taught_subjects']:
                self.assertEqual(getattr(self.teacher_profile, attr_name), value)

        self.assertCountEqual(self.teacher_profile.taught_subjects.values_list('id', flat=True), [subject1.id, subject2.id])

        study_class1 = StudyClassFactory(school_unit=self.school_unit, class_grade='I', class_grade_arabic=1)
        teacher_class_through1 = TeacherClassThroughFactory(study_class=study_class1, teacher=self.teacher_profile,
                                                            subject=subject1, is_class_master=False)

        study_class2 = StudyClassFactory(school_unit=self.school_unit, class_master=self.teacher_profile)
        teacher_class_through2 = TeacherClassThroughFactory(study_class=study_class2, teacher=self.teacher_profile,
                                                            subject=subject2, is_class_master=True)
        self.student_profile.student_in_class = study_class2
        self.student_profile.save()
        StudentCatalogPerYearFactory(student=self.student_profile, study_class=study_class2)
        catalog = StudentCatalogPerSubjectFactory(student=self.student_profile, teacher=self.teacher_profile,
                                                  study_class=study_class2, subject=subject2)

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

        self.teacher_data['taught_subjects'] = [subject1.id]
        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_200_OK)
        self.refresh_objects_from_db([teacher_class_through2, catalog])

        self.assertEqual(teacher_class_through2.teacher, new_teacher)
        self.assertFalse(teacher_class_through2.is_class_master)
        self.assertEqual(catalog.teacher, new_teacher)

        self.teacher_data['taught_subjects'] = []
        self.teacher_data['new_teachers'] = [
            {
                'id': teacher_class_through1.id,
                'teacher': study_class1.class_master.id
            }
        ]

        response = self.client.put(url, self.teacher_data)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        teacher_class_through1.refresh_from_db()

        self.assertEqual(teacher_class_through1.teacher, study_class1.class_master)
        self.assertTrue(teacher_class_through1.is_class_master)