def test_with_filters_case_childs_with_different_academic_years(self): child_leaf_other_ac_year = LearningUnitYearFactory( academic_year=AcademicYearFactory( year=self.current_academic_year.year - 1)) with self.assertRaises(AttributeError): group_element_year.find_learning_unit_roots( [self.child_leaf, child_leaf_other_ac_year])
def test_find_learning_unit_roots_improper_parameters(self): with self.assertRaisesMessage( ValueError, "If parameter with_parents_of_parents is True, parameter parents_as_instances must be True" ): group_element_year.find_learning_unit_roots( [], return_result_params={ 'parents_as_instances': False, 'with_parents_of_parents': True })
def test_with_filters_case_direct_parent_id_root_and_matches_filters(self): element_year = GroupElementYearFactory(parent=self.root, child_branch=None, child_leaf=self.child_leaf) result = group_element_year.find_learning_unit_roots([self.child_leaf]) expected_result = {self.child_leaf.id: [element_year.parent.id]} self.assertEqual(result, expected_result)
def get_context_data(self, **kwargs): context = super().get_context_data() learning_unit_year = context["learning_unit_year"] education_group_year_root = EducationGroupYear.objects.get( id=context["root_id"]) formations = group_element_year.find_learning_unit_roots( [learning_unit_year], return_result_params={ 'parents_as_instances': True, 'with_parents_of_parents': True }) formations_set = set( flatten([parents for child_id, parents in formations.items()])) if education_group_year_root not in formations_set: raise PermissionDenied( _("You must be in the context of a training to modify the prerequisites to a learning unit " "(current context: %(partial_acronym)s - %(acronym)s)") % { 'acronym': education_group_year_root.acronym, 'partial_acronym': education_group_year_root.partial_acronym }) return context
def test_with_kwarg_parents_as_instances_is_true(self): group_element = GroupElementYearFactory(child_branch=None, child_leaf=self.child_leaf) result = group_element_year.find_learning_unit_roots( [self.child_leaf], return_result_params={'parents_as_instances': True}) self.assertEqual(result[self.child_leaf.id], [group_element.parent])
def _parents(self): return group_element_year.find_learning_unit_roots( [self.parent], return_result_params={ 'parents_as_instances': True } )[self.parent.pk] + [self.parent]
def has_or_is_prerequisite(self, education_group_year): formations = group_element_year.find_learning_unit_roots( [education_group_year])[education_group_year.id] return PrerequisiteItem.objects.filter( Q(prerequisite__learning_unit_year=self, prerequisite__education_group_year__in=formations) | Q(prerequisite__education_group_year__in=formations, learning_unit=self.learning_unit)).exists()
def test_with_filters_case_objects_are_education_group_instance(self): root = EducationGroupYearFactory( academic_year=self.current_academic_year, ) child_branch = EducationGroupYearFactory( academic_year=self.current_academic_year, ) GroupElementYearFactory(parent=root, child_branch=child_branch) result = group_element_year.find_learning_unit_roots([child_branch]) expected_result = {child_branch.id: [root.id]} self.assertDictEqual(result, expected_result)
def test_with_kwarg_is_root_when_matches_is_complementary_module_and_not_in_it( self): group_element = GroupElementYearFactory(child_branch=None, child_leaf=self.child_leaf) result = group_element_year.find_learning_unit_roots( [self.child_leaf], luy=self.child_leaf, is_root_when_matches=[GroupType.COMPLEMENTARY_MODULE]) self.assertEqual(result[self.child_leaf.id], [group_element.parent.id])
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["group_element_years"] = self.object.child_branch.select_related("parent") context["formations"] = find_learning_unit_roots( list(grp.parent for grp in self.object.child_branch.select_related("parent")), return_result_params={ 'parents_as_instances': True } ) return context
def test_case_group_category_is_not_root(self): a_group_type = EducationGroupTypeFactory( name='Subgroup', category=education_group_categories.GROUP) hierarchy = self._build_hierarchy(self.current_academic_year, a_group_type, self.child_leaf) result = group_element_year.find_learning_unit_roots([self.child_leaf]) self.assertNotIn(hierarchy['group_element_child'].parent.id, result[self.child_leaf.id]) self.assertIn(hierarchy['group_element_root'].parent.id, result[self.child_leaf.id])
def test_all_group_types_of_category_training_stops_recursivity(self): type_bachelor = EducationGroupTypeFactory( name='Bachelor', category=education_group_categories.TRAINING) hierarchy = self._build_hierarchy(self.current_academic_year, type_bachelor, self.child_leaf) result = group_element_year.find_learning_unit_roots([self.child_leaf]) self.assertNotIn(hierarchy['group_element_root'].parent.id, result[self.child_leaf.id]) self.assertIn(hierarchy['group_element_child'].parent.id, result[self.child_leaf.id])
def test_group_type_option_is_correctly_excluded(self): type_option = EducationGroupTypeFactory( name=MiniTrainingType.OPTION.name, category=education_group_categories.MINI_TRAINING) hierarchy = self._build_hierarchy(self.current_academic_year, type_option, self.child_leaf) result = group_element_year.find_learning_unit_roots([self.child_leaf]) self.assertNotIn(hierarchy['group_element_child'].parent.id, result[self.child_leaf.id]) self.assertIn(hierarchy['group_element_root'].parent.id, result[self.child_leaf.id])
def test_ids_correctly_converted_to_instances(self): group_element = GroupElementYearFactory( child_branch=None, child_leaf=LearningUnitYearFactory()) root_ids_by_object_id = group_element_year.find_learning_unit_roots( [group_element.child_leaf]) result = group_element_year._convert_parent_ids_to_instances( root_ids_by_object_id) expected_result = {group_element.child_leaf.id: [group_element.parent]} self.assertDictEqual(result, expected_result) self.assertIsInstance(list(result.keys())[0], int) self.assertIsInstance(result[group_element.child_leaf.id][0], EducationGroupYear)
def test_case_group_category_is_root(self): a_group_type = EducationGroupTypeFactory( name='Subgroup', category=education_group_categories.GROUP) group_element = GroupElementYearFactory( parent=EducationGroupYearFactory( academic_year=self.current_academic_year, education_group_type=a_group_type), child_branch=None, child_leaf=self.child_leaf) result = group_element_year.find_learning_unit_roots([self.child_leaf]) self.assertEqual(result[self.child_leaf.id], []) self.assertNotIn(group_element.parent.id, result[self.child_leaf.id])
def test_with_filters_case_direct_parent_is_root_and_not_matches_filter( self): root = EducationGroupYearFactory( academic_year=self.current_academic_year, education_group_type=EducationGroupTypeFactory( name='Options choices', category=education_group_categories.GROUP)) GroupElementYearFactory(parent=root, child_branch=None, child_leaf=self.child_leaf) expected_result = {self.child_leaf.id: []} result = group_element_year.find_learning_unit_roots([self.child_leaf]) self.assertDictEqual(result, expected_result)
def test_with_kwarg_is_root_when_matches_is_complementary_module_and_in_it( self): group_type = EducationGroupTypeFactory( name=GroupType.COMPLEMENTARY_MODULE.name, category=education_group_categories.GROUP) hierarchy = self._build_hierarchy(self.current_academic_year, group_type, self.child_leaf) result = group_element_year.find_learning_unit_roots( [self.child_leaf], luy=self.child_leaf, is_root_when_matches=[GroupType.COMPLEMENTARY_MODULE]) self.assertEqual(result[self.child_leaf.id], [hierarchy['group_element_child'].parent.id])
def get_context_data(self, **kwargs): context = super().get_context_data() learning_unit_year = context["learning_unit_year"] formations_id = group_element_year.find_learning_unit_roots([learning_unit_year]). \ get(learning_unit_year.id, []) qs = EducationGroupYear.objects.filter(id__in=formations_id) prefetch_prerequisites = Prefetch( "prerequisite_set", Prerequisite.objects.filter(learning_unit_year=learning_unit_year), to_attr="prerequisites") context["formations"] = qs.prefetch_related(prefetch_prerequisites) return context
def get_queryset(self): learning_unit_year = get_object_or_404( LearningUnitYear.objects.all().select_related('academic_year'), acronym=self.kwargs['acronym'].upper(), academic_year__year=self.kwargs['year'] ) education_group_root_ids = group_element_year.find_learning_unit_roots( [learning_unit_year], luy=learning_unit_year, is_root_when_matches=[GroupType.COMPLEMENTARY_MODULE] ).get(learning_unit_year.id, []) return EducationGroupYear.objects.filter( pk__in=education_group_root_ids ).select_related( 'education_group_type', 'academic_year' )
def map_learning_unit_year_with_entities_of_education_groups(learning_unit_year_qs): formations = group_element_year.find_learning_unit_roots( learning_unit_year_qs, return_result_params={ 'parents_as_instances': False } ) education_group_ids = list(itertools.chain.from_iterable(formations.values())) offer_year_entity = OfferYearEntity.objects.filter(education_group_year__in=education_group_ids). \ values_list("education_group_year", "entity") dict_entity_of_education_group = {education_group_year_id: entity_id for education_group_year_id, entity_id in offer_year_entity} dict_education_group_year_entities_for_learning_unit_year = {} for luy_id, formations_ids in formations.items(): dict_education_group_year_entities_for_learning_unit_year[luy_id] = \ [dict_entity_of_education_group.get(formation_id) for formation_id in formations_ids] return dict_education_group_year_entities_for_learning_unit_year
def test_with_filters_case_multiple_parents_in_2nd_level(self): root_2 = EducationGroupYearFactory( academic_year=self.current_academic_year, education_group_type=EducationGroupTypeFactory( name='Master', category=education_group_categories.TRAINING)) child_branch = EducationGroupYearFactory( academic_year=self.current_academic_year, education_group_type=EducationGroupTypeFactory( category=education_group_categories.GROUP)) GroupElementYearFactory(parent=self.root, child_branch=child_branch) GroupElementYearFactory(parent=root_2, child_branch=child_branch) GroupElementYearFactory(parent=child_branch, child_branch=None, child_leaf=self.child_leaf) result = group_element_year.find_learning_unit_roots([self.child_leaf]) self.assertEqual(len(result[self.child_leaf.id]), 2) self.assertIn(self.root.id, result[self.child_leaf.id]) self.assertIn(root_2.id, result[self.child_leaf.id])
def test_with_filters_case_root_in_2nd_level_and_direct_parent_matches_filter( self): root = EducationGroupYearFactory( academic_year=self.current_academic_year, education_group_type=EducationGroupTypeFactory( name='Master', category=education_group_categories.TRAINING)) child_branch = EducationGroupYearFactory( academic_year=self.current_academic_year, education_group_type=EducationGroupTypeFactory( name='Didactic Master', category=education_group_categories.TRAINING)) GroupElementYearFactory(parent=root, child_branch=child_branch) GroupElementYearFactory(parent=child_branch, child_branch=None, child_leaf=self.child_leaf) result = group_element_year.find_learning_unit_roots([self.child_leaf]) expected_result = {self.child_leaf.id: [child_branch.id]} self.assertDictEqual(result, expected_result) self.assertNotIn(root.id, result)
def test_ordered_by_acronym(self): learn_unit_year = LearningUnitYearFactory() group_element1 = GroupElementYearFactory( parent=EducationGroupYearFactory(acronym='ECGE1BA'), child_branch=None, child_leaf=learn_unit_year) group_element2 = GroupElementYearFactory( parent=EducationGroupYearFactory(acronym='DROI1BA'), child_branch=None, child_leaf=learn_unit_year) group_element3 = GroupElementYearFactory( parent=EducationGroupYearFactory(acronym='SPOL2MS/G'), child_branch=None, child_leaf=learn_unit_year) root_ids_by_object_id = group_element_year.find_learning_unit_roots( [learn_unit_year]) result = group_element_year._convert_parent_ids_to_instances( root_ids_by_object_id) expected_order = [ group_element2.parent, group_element1.parent, group_element3.parent ] self.assertListEqual(result[learn_unit_year.id], expected_order)
def publish(education_group_year): if not all([ settings.ESB_API_URL, settings.ESB_AUTHORIZATION, settings.ESB_REFRESH_PEDAGOGY_ENDPOINT, settings.ESB_REFRESH_COMMON_PEDAGOGY_ENDPOINT, settings.ESB_REFRESH_COMMON_ADMISSION_ENDPOINT ]): raise ImproperlyConfigured( 'ESB_API_URL / ESB_AUTHORIZATION / ESB_REFRESH_PEDAGOGY_ENDPOINT / ' 'ESB_REFRESH_COMMON_PEDAGOGY_ENDPOINT / ' 'ESB_REFRESH_COMMON_ADMISSION_ENDPOINT must be set in configuration' ) trainings = find_learning_unit_roots([education_group_year], return_result_params={ 'parents_as_instances': True }).get(education_group_year.pk, []) education_groups_to_publish = [education_group_year] + trainings \ if education_group_year.education_group_type.name != GroupType.COMMON_CORE.name \ else trainings t = Thread(target=_bulk_publish, args=(education_groups_to_publish, )) t.start() return True
def test_objects_instances_check_is_called(self, mock_check_instance): group_element_year.find_learning_unit_roots([self.child_leaf]) self.assertTrue(mock_check_instance.called)
def test_case_filters_arg_is_none(self): result = group_element_year.find_learning_unit_roots([self.child_leaf]) expected_result = {self.child_leaf.id: []} self.assertDictEqual(result, expected_result)
def test_case_arg_is_none(self): result = group_element_year.find_learning_unit_roots(None) self.assertEqual(result, {})