Esempio n. 1
0
    def test_save_with_postponement_sets_inconsistents(self):
        # create postponed egy's
        self._create_postponed_egys()

        # update with inconsistant EducationGroupOrganization (organization present in the future not in present)
        self.education_group_year.refresh_from_db()
        unconsistent_egy = EducationGroupYear.objects.get(
            education_group=self.education_group_year.education_group,
            academic_year=self.list_acs[4])
        EducationGroupOrganizationFactory(
            organization=OrganizationFactory(),
            education_group_year=unconsistent_egy)
        EducationGroupOrganizationFactory(
            organization=OrganizationFactory(),
            education_group_year=self.education_group_year)

        form = TrainingForm(self.data,
                            instance=self.education_group_year,
                            user=self.user)
        self.assertTrue(form.is_valid(), form.errors)
        form.save()

        self.assertGreater(len(form.warnings), 0)
        self.assertEqual(form.warnings, [
            _("Consistency error in %(academic_year)s : %(error)s") % {
                'academic_year': unconsistent_egy.academic_year,
                'error': EducationGroupOrganization._meta.verbose_name.title()
            }
        ])
Esempio n. 2
0
    def test_save_with_postponement(self):
        # Create postponed egy
        form = TrainingForm(self.data,
                            instance=self.education_group_year,
                            user=self.user)
        self.assertTrue(form.is_valid(), form.errors)
        form.save()

        self.assertEqual(len(form.education_group_year_postponed), 6)

        self.assertEqual(
            EducationGroupYear.objects.filter(
                education_group=self.education_group_year.education_group).
            count(), 7)
        self.assertEqual(len(form.warnings), 0)

        # Update egys
        self.education_group_year.refresh_from_db()

        self.data["title"] = "Defence Against the Dark Arts"
        form = TrainingForm(self.data,
                            instance=self.education_group_year,
                            user=self.user)
        self.assertTrue(form.is_valid(), form.errors)
        form.save()

        self.assertEqual(
            EducationGroupYear.objects.filter(
                education_group=self.education_group_year.education_group).
            count(), 7)
        self.assertEqual(len(form.warnings), 0, form.warnings)
Esempio n. 3
0
    def test_save_with_postponement_m2m(self):
        domains = [
            DomainFactory(name="Alchemy"),
            DomainFactory(name="Muggle Studies")
        ]
        self.data["secondary_domains"] = '|'.join(
            [str(domain.pk) for domain in domains])

        certificate_aims = [
            CertificateAimFactory(code=100, section=1),
            CertificateAimFactory(code=101, section=2)
        ]
        self.data["certificate_aims"] = [
            str(aim.pk) for aim in certificate_aims
        ]

        self._create_postponed_egys()

        last = EducationGroupYear.objects.filter(
            education_group=self.education_group_year.education_group
        ).order_by('academic_year').last()

        self.education_group_year.refresh_from_db()
        self.assertEqual(self.education_group_year.secondary_domains.count(),
                         2)
        self.assertEqual(last.secondary_domains.count(), 2)
        self.assertEqual(last.certificate_aims.count(), len(certificate_aims))

        # update with a conflict
        dom3 = DomainFactory(name="Divination")
        EducationGroupYearDomainFactory(domain=dom3, education_group_year=last)

        domains = [
            DomainFactory(name="Care of Magical Creatures"),
            DomainFactory(name="Muggle Studies")
        ]

        self.data["secondary_domains"] = '|'.join(
            [str(domain.pk) for domain in domains])

        form = TrainingForm(self.data,
                            instance=self.education_group_year,
                            user=self.user)
        self.assertTrue(form.is_valid(), form.errors)
        form.save()

        self.assertEqual(len(form.education_group_year_postponed), 5)

        self.assertEqual(
            EducationGroupYear.objects.filter(
                education_group=self.education_group_year.education_group).
            count(), 7)
        last.refresh_from_db()
        self.education_group_year.refresh_from_db()

        self.assertEqual(self.education_group_year.secondary_domains.count(),
                         2)
        self.assertEqual(last.secondary_domains.count(), 3)
        self.assertEqual(len(form.warnings), 1)
Esempio n. 4
0
 def _create_postponed_egys(self):
     form = TrainingForm(self.data,
                         instance=self.education_group_year,
                         user=self.user)
     self.assertTrue(form.is_valid(), form.errors)
     form.save()
     self.assertEqual(len(form.education_group_year_postponed), 6)
     self.assertEqual(len(form.warnings), 0)
     self.assertEqual(
         EducationGroupYear.objects.filter(
             education_group=self.education_group_year.education_group).
         count(), 7)
Esempio n. 5
0
 def _postpone_coorganization_and_check(self):
     form = TrainingForm(self.data,
                         instance=self.education_group_year,
                         user=self.user)
     self.assertTrue(form.is_valid(), form.errors)
     form.save()
     self.assertEqual(len(form.warnings), 0, form.warnings)
     all_egys = EducationGroupYear.objects.filter(
         education_group=self.education_group_year.education_group)
     self.assertEqual(all_egys.count(), 7)
     for egy in all_egys:
         self.assertEqual(egy.educationgrouporganization_set.all().count(),
                          1)
Esempio n. 6
0
    def test_save_with_postponement_coorganization_inconsistant(self):
        # create postponed egy's
        self._create_postponed_egys()

        # update with a coorganization to postpone
        self.education_group_year.refresh_from_db()

        good_organization = EducationGroupOrganizationFactory(
            organization=OrganizationFactory(),
            education_group_year=self.education_group_year)
        self._postpone_coorganization_and_check()

        # update with unconsistant EducationGroupOrganization (field different from a year to another one)
        self.education_group_year.refresh_from_db()
        unconsistent_egy = EducationGroupYear.objects.get(
            education_group=self.education_group_year.education_group,
            academic_year=self.list_acs[4])
        unconsistent_orga = EducationGroupOrganization.objects.get(
            education_group_year=unconsistent_egy)
        unconsistent_orga.all_students = not good_organization.all_students
        unconsistent_orga.save()
        # have to invalidate cache
        del self.education_group_year.coorganizations
        form = TrainingForm(self.data,
                            instance=self.education_group_year,
                            user=self.user)

        self.assertTrue(form.is_valid(), form.errors)
        form.save()
        self.assertEqual(len(form.warnings), 1)
        error_msg = _("%(col_name)s has been already modified.") % {
            "col_name":
            _(
                EducationGroupOrganization._meta.get_field(
                    'all_students').verbose_name).title(),
        }
        self.assertEqual(form.warnings, [
            _("Consistency error in %(academic_year)s with %(model)s: %(error)s"
              ) %
            {
                'academic_year': unconsistent_egy.academic_year,
                'model': EducationGroupOrganization._meta.verbose_name.title(),
                'error': error_msg
            }
        ])
Esempio n. 7
0
    def test_save_with_postponement_error(self):
        EducationGroupYearFactory(
            academic_year=self.list_acs[4],
            education_group=self.education_group_year.education_group)

        form = TrainingForm(self.data,
                            instance=self.education_group_year,
                            user=UserFactory())
        self.assertTrue(form.is_valid(), form.errors)
        form.save()

        self.assertEqual(len(form.education_group_year_postponed), 5)

        self.assertEqual(
            EducationGroupYear.objects.filter(
                education_group=self.education_group_year.education_group).
            count(), 7)
        self.assertEqual(len(form.warnings), 15)
Esempio n. 8
0
    def test_save_with_postponement_certificate_aims_inconsistant(self):
        """
        This test ensure that the we haven't an error if the certificate aims is inconsistant in future because
        certificate aims is managed by program_manager (This form is only accessible on Central/Faculty manager)
        """
        # Save the training instance will create N+6 data...
        form = TrainingForm(self.form_data,
                            instance=self.training,
                            user=self.central_manager.user)
        self.assertTrue(form.is_valid(), form.errors)
        form.save()

        # Add certificate aims in future...
        training_future = EducationGroupYear.objects.filter(
            education_group=self.training.education_group,
            academic_year__year=self.training.academic_year.year + 2).get()
        certificate_aims_in_future = EducationGroupCertificateAimFactory(
            education_group_year=training_future)

        # Re-save and ensure that there are not consitency errors and the modification is correctly postponed
        form = TrainingForm(
            {
                **self.form_data, "title": "Modification du titre"
            },
            instance=self.training,
            user=self.central_manager.user,
        )
        self.assertTrue(form.is_valid(), form.errors)
        form.save()
        self.assertFalse(form.warnings, form.warnings)

        training_future.refresh_from_db()
        self.assertEqual(training_future.title, "Modification du titre")

        # Ensure that certificate aims has not be modified during postponement
        self.assertTrue(
            EducationGroupCertificateAim.objects.filter(
                pk=certificate_aims_in_future.pk).exists())
Esempio n. 9
0
    def setUpTestData(cls):
        super().setUpTestData()

        cls.current_academic_year = AcademicYearFactory(current=True)
        cls.generated_ac_years = AcademicYearFactory.produce(
            base_year=cls.current_academic_year.year,
            number_past=0,
            number_future=7)
        cls.entity_version = MainEntityVersionFactory(
            entity_type=entity_type.SECTOR)
        cls.central_manager = CentralManagerFactory()
        PersonEntityFactory(person=cls.central_manager,
                            entity=cls.entity_version.entity)
        cls.training = TrainingFactory(
            management_entity=cls.entity_version.entity,
            administration_entity=cls.entity_version.entity,
            academic_year=cls.current_academic_year)
        # Save the training instance will create N+6 data...
        form_data = model_to_dict_fk(cls.training,
                                     exclude=('secondary_domains', ))
        form_data.update({
            'primary_language': form_data['primary_language_id'],
            'administration_entity': cls.entity_version.pk,
            'management_entity': cls.entity_version.pk
        })
        training_form = TrainingForm(form_data,
                                     instance=cls.training,
                                     user=cls.central_manager.user)
        training_form.is_valid()
        training_form.save()

        cls.certificate_aim_type_2 = CertificateAimFactory(section=2, code=200)
        cls.certificate_aim_type_4 = CertificateAimFactory(section=4, code=400)
        cls.form_data = {
            'certificate_aims':
            [cls.certificate_aim_type_2.pk, cls.certificate_aim_type_4.pk]
        }
Esempio n. 10
0
def _update_training(request, education_group_year, root):
    # TODO :: IMPORTANT :: Fix urls patterns to get the GroupElementYear_id and the root_id in the url path !
    # TODO :: IMPORTANT :: Need to update form to filter on list of parents, not only on the first direct parent
    form_education_group_year = TrainingForm(request.POST or None,
                                             instance=education_group_year)
    if form_education_group_year.is_valid():
        return _common_success_redirect(request,
                                        form_education_group_year.save(), root)

    return layout.render(
        request, "education_group/update_trainings.html", {
            "education_group_year":
            education_group_year,
            "form_education_group_year":
            form_education_group_year.forms[forms.ModelForm],
            "form_education_group":
            form_education_group_year.forms[EducationGroupModelForm]
        })