Esempio n. 1
0
    def test_init(self):
        # In case of creation
        form = TrainingForm({},
                            user=self.user,
                            education_group_type=self.education_group_type)
        self.assertFalse(form.dict_initial_egy)
        self.assertEqual(form.initial_dicts,
                         {'educationgrouporganization_set': {}})

        # In case of update
        coorg = EducationGroupOrganizationFactory(
            organization=OrganizationFactory(),
            education_group_year=self.education_group_year)
        form = TrainingForm({},
                            user=self.user,
                            instance=self.education_group_year)
        dict_initial_egy = model_to_dict_fk(self.education_group_year,
                                            exclude=form.field_to_exclude)

        self.assertEqual(str(form.dict_initial_egy), str(dict_initial_egy))
        initial_dict_coorg = model_to_dict_fk(
            self.education_group_year.coorganizations.first(),
            exclude=FIELD_TO_EXCLUDE_IN_SET)
        self.assertEqual(
            form.initial_dicts, {
                'educationgrouporganization_set': {
                    coorg.organization.id: initial_dict_coorg
                }
            })
Esempio n. 2
0
def duplicate_education_group_year(old_education_group_year, new_academic_year, dict_initial_egy=None,
                                   hops_values=None):
    dict_new_value = model_to_dict_fk(old_education_group_year, exclude=FIELD_TO_EXCLUDE)

    defaults_values = {x: v for x, v in dict_new_value.items() if not isinstance(v, list)}

    postponed_egy, created = EducationGroupYear.objects.get_or_create(
        education_group=old_education_group_year.education_group,
        academic_year=new_academic_year,
        # Create object without m2m relations
        defaults=defaults_values
    )

    # During create of new postponed object, we need to update only the m2m relations
    if created:
        # Postpone the m2m [languages / secondary_domains]
        _postpone_m2m(old_education_group_year, postponed_egy, hops_values)

    # During the update, we need to check if the postponed object has been modify
    else:
        dict_postponed_egy = model_to_dict_fk(postponed_egy, exclude=FIELD_TO_EXCLUDE)
        differences = compare_objects(dict_initial_egy, dict_postponed_egy) \
            if dict_initial_egy and dict_postponed_egy else {}

        if differences:
            raise ConsistencyError(postponed_egy, differences)

        update_object(postponed_egy, dict_new_value)
        # Postpone the m2m [languages / secondary_domains]
        _postpone_m2m(old_education_group_year, postponed_egy, hops_values)

    return postponed_egy
Esempio n. 3
0
def duplicate_education_group_year(old_education_group_year,
                                   new_academic_year,
                                   initial_dicts=None,
                                   hops_values=None,
                                   fields_to_exclude=None):
    if initial_dicts is None:
        initial_dicts = {}
    if fields_to_exclude is None:
        fields_to_exclude = FIELD_TO_EXCLUDE
    dict_new_value = model_to_dict_fk(old_education_group_year,
                                      exclude=fields_to_exclude)

    defaults_values = {
        x: v
        for x, v in dict_new_value.items() if not isinstance(v, list)
    }

    postponed_egy, created = EducationGroupYear.objects.get_or_create(
        education_group=old_education_group_year.education_group,
        academic_year=new_academic_year,
        # Create object without m2m relations
        defaults=defaults_values)

    # During create of new postponed object, we need to update only the m2m relations
    if created:
        # Postpone the m2m [languages / secondary_domains]
        _postpone_m2m(old_education_group_year, postponed_egy, hops_values)

    # During the update, we need to check if the postponed object has been modify
    else:
        dict_postponed_egy = model_to_dict_fk(postponed_egy,
                                              exclude=fields_to_exclude)
        differences = compare_objects(initial_dicts['dict_initial_egy'], dict_postponed_egy) \
            if initial_dicts['dict_initial_egy'] and dict_postponed_egy else {}

        if differences:
            raise ConsistencyError(
                {
                    'model': EducationGroupYear,
                    'last_instance_updated': postponed_egy
                }, differences)

        update_object(postponed_egy, dict_new_value)
        # Postpone the m2m [languages / secondary_domains]
        _postpone_m2m(old_education_group_year,
                      postponed_egy,
                      hops_values,
                      fields_to_exclude=fields_to_exclude)

    if education_group.has_coorganization(old_education_group_year):
        duplicate_set(old_education_group_year, postponed_egy,
                      initial_dicts.get('initial_sets_dict'))
    return postponed_egy
Esempio n. 4
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)
        cls.form_data = model_to_dict_fk(cls.training,
                                         exclude=('secondary_domains', ))
        cls.form_data.update({
            'primary_language':
            cls.form_data['primary_language_id'],
            'administration_entity':
            cls.entity_version.pk,
            'management_entity':
            cls.entity_version.pk
        })
Esempio n. 5
0
def _postpone_m2m(education_group_year,
                  postponed_egy,
                  hops_values,
                  fields_to_exclude=None):
    if fields_to_exclude is None:
        fields_to_exclude = []

    opts = education_group_year._meta
    for f in opts.many_to_many:
        if f.name in fields_to_exclude:
            continue
        m2m_cls = f.remote_field.through

        # Remove records of postponed_egy
        m2m_cls.objects.all().filter(
            education_group_year=postponed_egy).delete()

        # Recreate records
        for m2m_obj in m2m_cls.objects.all().filter(
                education_group_year_id=education_group_year):
            m2m_data_to_postpone = model_to_dict_fk(
                m2m_obj, exclude=['id', 'external_id', 'education_group_year'])
            m2m_cls(education_group_year=postponed_egy,
                    **m2m_data_to_postpone).save()

    if hops_values and any(elem in HOPS_FIELDS and hops_values[elem]
                           for elem in hops_values):
        _postpone_hops(hops_values, postponed_egy)
Esempio n. 6
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.postpone_start_year = None
        self.postpone_end_year = None
        # The list will not include the current instance of education group year
        self.education_group_year_postponed = []
        self.postponement_errors = {}
        self.warnings = []

        if not self._is_creation():
            self.dict_initial_egy = model_to_dict_fk(
                self.forms[forms.ModelForm].instance,
                exclude=self.field_to_exclude)
            self.initial_dicts['educationgrouporganization_set'] = {
                coorganization.organization.id:
                model_to_dict_fk(coorganization,
                                 exclude=FIELD_TO_EXCLUDE_IN_SET)
                for coorganization in self.forms[
                    forms.ModelForm].instance.coorganizations
            }
Esempio n. 7
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.postpone_start_year = None
        self.postpone_end_year = None
        # The list will not include the current instance of education group year
        self.education_group_year_postponed = []
        self.postponement_errors = {}
        self.warnings = []

        if not self._is_creation():
            self.dict_initial_egy = model_to_dict_fk(
                self.forms[forms.ModelForm].instance, exclude=self.field_to_exclude
            )
Esempio n. 8
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.postpone_start_year = None
        self.postpone_end_year = None
        # The list will not include the current instance of education group year
        self.education_group_year_postponed = []
        self.postponement_errors = {}
        self.warnings = []

        if not self._is_creation():
            self.dict_initial_egy = model_to_dict_fk(
                self.forms[forms.ModelForm].instance,
                exclude=self.field_to_exclude)
Esempio n. 9
0
def duplicate_education_group_year(old_education_group_year,
                                   new_academic_year,
                                   dict_initial_egy=None,
                                   hops_values=None):
    dict_new_value = model_to_dict_fk(old_education_group_year,
                                      exclude=FIELD_TO_EXCLUDE)

    defaults_values = {
        x: v
        for x, v in dict_new_value.items() if not isinstance(v, list)
    }

    postponed_egy, created = EducationGroupYear.objects.get_or_create(
        education_group=old_education_group_year.education_group,
        academic_year=new_academic_year,
        # Create object without m2m relations
        defaults=defaults_values)

    # During create of new postponed object, we need to update only the m2m relations
    if created:
        # Postpone the m2m [languages / secondary_domains]
        _postpone_m2m(old_education_group_year, postponed_egy, hops_values)

    # During the update, we need to check if the postponed object has been modify
    else:
        dict_postponed_egy = model_to_dict_fk(postponed_egy,
                                              exclude=FIELD_TO_EXCLUDE)
        differences = compare_objects(dict_initial_egy, dict_postponed_egy) \
            if dict_initial_egy and dict_postponed_egy else {}

        if differences:
            raise ConsistencyError(postponed_egy, differences)

        update_object(postponed_egy, dict_new_value)
        # Postpone the m2m [languages / secondary_domains]
        _postpone_m2m(old_education_group_year, postponed_egy, hops_values)

    return postponed_egy
Esempio n. 10
0
    def test_init(self):
        # In case of creation
        form = TrainingForm({},
                            user=self.user,
                            education_group_type=self.education_group_type)
        self.assertFalse(form.dict_initial_egy)

        # In case of update
        form = TrainingForm({},
                            user=self.user,
                            instance=self.education_group_year)
        dict_initial_egy = model_to_dict_fk(self.education_group_year,
                                            exclude=form.field_to_exclude)

        self.assertEqual(str(form.dict_initial_egy), str(dict_initial_egy))
Esempio n. 11
0
    def test_compute_end_postponement_case_specific_end_date_and_data_in_future_gte(self):
        # Set end date of education group
        self.education_group_year.education_group.end_year = self.current_academic_year.year + 2
        self.education_group_year.refresh_from_db()

        # Create data in future
        lastest_academic_year = self.generated_ac_years.academic_years[-1]
        field_to_exclude = ['id', 'external_id', 'academic_year', 'languages', 'secondary_domains', 'certificate_aims']
        defaults = model_to_dict_fk(self.education_group_year, exclude=field_to_exclude)
        EducationGroupYear.objects.update_or_create(
            education_group=self.education_group_year.education_group,
            academic_year=lastest_academic_year,
            defaults=defaults
        )

        result = _compute_end_year(self.education_group_year.education_group)
        self.assertEqual(result, lastest_academic_year.year)
Esempio n. 12
0
def _check_differences_and_update(dict_new_values, initial_set, postponed_item,
                                  set_tuple):
    set_name, set_model, set_filter_field = set_tuple

    dict_postponed_item = model_to_dict_fk(postponed_item,
                                           exclude=FIELD_TO_EXCLUDE_IN_SET)
    initial_item = initial_set.get(dict_postponed_item[set_filter_field])

    differences = compare_objects(initial_item, dict_postponed_item) \
        if initial_item and dict_postponed_item else {}
    if differences:
        raise ConsistencyError(
            {
                'model': set_model,
                'last_instance_updated': postponed_item
            }, differences)
    update_object(postponed_item, dict_new_values)
Esempio n. 13
0
def _postpone_m2m(education_group_year, postponed_egy, hops_values):
    fields_to_exclude = []

    opts = education_group_year._meta
    for f in opts.many_to_many:
        if f.name in fields_to_exclude:
            continue
        m2m_cls = f.rel.through

        # Remove records of postponed_egy
        m2m_cls.objects.all().filter(education_group_year=postponed_egy).delete()

        # Recreate records
        for m2m_obj in m2m_cls.objects.all().filter(education_group_year_id=education_group_year):
            m2m_data_to_postpone = model_to_dict_fk(m2m_obj, exclude=['id', 'external_id', 'education_group_year'])
            m2m_cls(education_group_year=postponed_egy, **m2m_data_to_postpone).save()

    if hops_values and any(elem in HOPS_FIELDS and hops_values[elem] for elem in hops_values):
        _postpone_hops(hops_values, postponed_egy)
Esempio n. 14
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. 15
0
def _update_and_check_consistency_of_set(education_group_year, old_egy,
                                         initial_sets, set_tuple):
    ids = []
    set_name, set_model, set_filter_field = set_tuple

    initial_set = initial_sets.get(set_name, {})
    egy_set = getattr(old_egy, set_name).all()
    sets_is_consistent = _check_consistency_of_all_set(education_group_year,
                                                       initial_set, set_tuple)

    if not sets_is_consistent:
        raise ConsistencyError(
            {
                'model': set_model,
                'last_instance_updated': education_group_year
            }, {'consistency': ()})

    for item_set in egy_set:
        dict_new_values = model_to_dict_fk(item_set,
                                           exclude=FIELD_TO_EXCLUDE_IN_SET)
        defaults_values = {
            x: v
            for x, v in dict_new_values.items() if not isinstance(v, list)
        }
        postponed_item, created = set_model.objects.get_or_create(
            education_group_year=education_group_year,
            defaults=defaults_values,
            **{set_filter_field: dict_new_values[set_filter_field]})
        ids.append(postponed_item.id)

        if not created:
            _check_differences_and_update(dict_new_values, initial_set,
                                          postponed_item, set_tuple)

    set_model.objects.filter(
        education_group_year=education_group_year).exclude(
            id__in=ids).delete()