def build_calendar_by_entities(ac_year, reference): """ This function will compute date for each entity. If entity calendar not exist, get default date to academic calendar""" entity_structure = entity_version.build_current_entity_version_structure_in_memory( date=ac_year.end_date) entities_id = list(entity_structure.keys()) ac_calendar = academic_calendar.get_by_reference_and_academic_year( reference, ac_year) all_entities_calendars = EntityCalendar.objects.filter(entity__in=entities_id, academic_calendar=ac_calendar)\ .select_related('entity') # Specific date for an entity [record found on entity calendar] entity_calendar_computed = {} for entity_calendar in all_entities_calendars: # FIXME: We should use date OR datetime in all database model entity_calendar_computed[entity_calendar.entity_id] = { 'start_date': entity_calendar.start_date.date(), 'end_date': entity_calendar.end_date.date(), } entities_id.remove(entity_calendar.entity_id) default_dates = { 'start_date': ac_calendar.start_date, 'end_date': ac_calendar.end_date } entity_calendar_computed.update({ entity_id: _get_start_end_date_of_parent(entity_id, entity_structure, entity_calendar_computed, default_dates) for entity_id in entities_id }) return entity_calendar_computed
def filter_learning_unit_year_according_person(queryset: QuerySet, person: Person) -> QuerySet: """ This function will filter the learning unit year queryset according to permission of person. * As Entity Manager, we will filter on linked entities * As Program Manager, we will filter on learning unit year which are contained in the program that the person manage but not a borrow learning unit year :param queryset: LearningUnitYear queryset :param person: Person object :return: queryset """ structure = entity_version.build_current_entity_version_structure_in_memory( ) entities_with_descendants = entity_manager.find_entities_with_descendants_from_entity_managers( person.entitymanager_set.all().select_related('entity'), structure) learning_units_of_prgm_mngr = program_manager.get_learning_unit_years_attached_to_program_managers( person.programmanager_set.all(), structure) queryset = queryset.filter( Q(learning_container_year__requirement_entity__in= entities_with_descendants) | Q(id__in=learning_units_of_prgm_mngr)) return queryset
def filter_is_borrowed_learning_unit_year(learning_unit_year_qs, date, faculty_borrowing=None): entities = build_current_entity_version_structure_in_memory(date) entities_borrowing_allowed = [] if faculty_borrowing in entities: entities_borrowing_allowed.extend( entities[faculty_borrowing]["all_children"]) entities_borrowing_allowed.append( entities[faculty_borrowing]["entity_version"]) entities_borrowing_allowed = [ entity_version.entity.id for entity_version in entities_borrowing_allowed ] entities_faculty = compute_faculty_for_entities(entities) map_luy_entity = map_learning_unit_year_with_requirement_entity( learning_unit_year_qs) map_luy_education_group_entities = \ map_learning_unit_year_with_entities_of_education_groups(learning_unit_year_qs) return filter( lambda luy: __is_borrowed_learning_unit( luy, entities_faculty, map_luy_entity, map_luy_education_group_entities, entities_borrowing_allowed), learning_unit_year_qs)
def test_build_entity_version_structure_in_memory(self): partial_expected_result = { self.root.entity.id: { 'entity_version_parent': None, 'direct_children': [self.SC, self.LOCI], 'all_children': [ self.SC, self.LOCI, self.MATH, self.PHYS, self.URBA, self.BARC ], }, self.SC.entity.id: { 'entity_version_parent': self.root, 'direct_children': [self.MATH, self.PHYS], 'all_children': [self.MATH, self.PHYS], }, self.MATH.entity.id: { 'entity_version_parent': self.SC, 'direct_children': [], 'all_children': [], }, # ... } result = entity_version.build_current_entity_version_structure_in_memory( ) all_current_entities_version = entity_version.find_all_current_entities_version( ) # assert entities without children are present in the result self.assertEqual(len(result.keys()), len(all_current_entities_version)) self.assertEqual(result[self.MATH.entity.id]['all_children'], [])
def test_find_parent_of_type_without_parent(self): entity_v = EntityVersionFactory(parent=None, entity_type=SCHOOL) result = find_parent_of_type_into_entity_structure( entity_v, build_current_entity_version_structure_in_memory( timezone.now().date()), FACULTY) self.assertEqual(None, result)
def test_find_parent_of_type_itself(self): entity_v = EntityVersionFactory(entity_type=FACULTY) result = find_parent_of_type_into_entity_structure( entity_v, build_current_entity_version_structure_in_memory( timezone.now().date()), FACULTY) self.assertEqual(entity_v.entity, result)
def find_entities_with_descendants_from_entity_managers(entities_manager): entities_with_descendants = [] entities_by_id = entity_version.build_current_entity_version_structure_in_memory() for entity_manager in entities_manager: entities_with_descendants.append(entity_manager.entity) entities_with_descendants += [ ent_version.entity for ent_version in entities_by_id[entity_manager.entity_id].get('all_children') ] return entities_with_descendants
def find_entities_with_descendants_from_entity_managers(entities_manager): entities_with_descendants = [] entities_by_id = entity_version.build_current_entity_version_structure_in_memory() for entity_manager in entities_manager: entities_with_descendants.append(entity_manager.entity) entities_with_descendants += [ ent_version.entity for ent_version in entities_by_id[entity_manager.entity_id].get('all_children') ] return entities_with_descendants
def test_build_entity_version_structure_in_memory(self): result = entity_version.build_current_entity_version_structure_in_memory( ) all_current_entities_version = entity_version.find_all_current_entities_version( ) # assert entities without children are present in the result self.assertEqual(len(result.keys()), len(all_current_entities_version)) self.assertEqual(result[self.MATH.entity.id]['all_children'], [])
def test_find_parent_of_type_first_parent(self): parent_version = EntityVersionFactory(entity_type=FACULTY) entity_v = EntityVersionFactory(parent=parent_version.entity, entity_type=INSTITUTE) result = find_parent_of_type_into_entity_structure( entity_v, build_current_entity_version_structure_in_memory( timezone.now().date()), FACULTY) self.assertEqual(entity_v.parent, result)
def filter_is_borrowed_learning_unit_year(learning_unit_year_qs, date, faculty_borrowing=None): entities = build_current_entity_version_structure_in_memory(date) entities_borrowing_allowed = [] if faculty_borrowing in entities: entities_borrowing_allowed.extend(entities[faculty_borrowing]["all_children"]) entities_borrowing_allowed.append(entities[faculty_borrowing]["entity_version"]) entities_borrowing_allowed = [entity_version.entity.id for entity_version in entities_borrowing_allowed] entities_faculty = compute_faculty_for_entities(entities) map_luy_entity = map_learning_unit_year_with_requirement_entity(learning_unit_year_qs) map_luy_education_group_entities = \ map_learning_unit_year_with_entities_of_education_groups(learning_unit_year_qs) return filter(lambda luy: __is_borrowed_learning_unit(luy, entities_faculty, map_luy_entity, map_luy_education_group_entities, entities_borrowing_allowed), learning_unit_year_qs)
def find_descendants(entities, date=None, with_entities=True): date = date or timezone.now().date() entities_descendants = set() entities_by_id = entity_version.build_current_entity_version_structure_in_memory( date=date) for entity in entities: _append_current_entity(entities_by_id, entities_descendants, entity, with_entities) if entity.id in entities_by_id: entities_descendants |= { ent_version.entity for ent_version in entities_by_id[entity.id].get( 'all_children') } return list(entities_descendants)
def test_get_entity_version_from_type(self): structure = entity_version.build_current_entity_version_structure_in_memory( ) test_cases = [ { "entity_version_test": self.MATH, 'entity_type': 'SECTOR', 'expected_result': self.root, 'comment': 'to_test_sector' }, { "entity_version_test": self.MATH, 'entity_type': 'FACULTY', 'expected_result': self.SC, 'comment': 'to_test_faculty' }, { "entity_version_test": self.MATH, 'entity_type': 'SCHOOL', 'expected_result': self.MATH, 'comment': 'to_test_itself' }, { "entity_version_test": self.root, 'entity_type': 'SCHOOL', 'expected_result': None, 'comment': 'to_test_lt_itself' }, { "entity_version_test": self.root, 'entity_type': 'SECTOR', 'expected_result': self.root, 'comment': 'to_test_itself_without_parent' }, ] for case in test_cases: with self.subTest(status_code=case.get('comment')): del case.get('entity_version_test').entity.most_recent_acronym to_test = get_entity_version_parent_or_itself_from_type( entity_versions=structure, entity=case.get( 'entity_version_test').entity.most_recent_acronym, entity_type=case.get('entity_type')) self.assertEqual(case.get('expected_result'), to_test)
def test_get_structure_of_entity_version(self): result = entity_version.build_current_entity_version_structure_in_memory( ) self.assertEqual( get_structure_of_entity_version( entity_version. build_current_entity_version_structure_in_memory()), result) expected_result = None for r in result: if result[r]['entity_version'].acronym == self.root.acronym.upper( ): expected_result = result[r] self.assertEqual( get_structure_of_entity_version( entity_version. build_current_entity_version_structure_in_memory(), self.root.acronym), expected_result)
def find_attached_faculty_entities_version(self, acronym_exceptions=None): entity_structure = build_current_entity_version_structure_in_memory( timezone.now().date()) faculties = set() for entity in self.directly_linked_entities: faculties = faculties.union({ e.entity for e in entity_structure[entity.id]['all_children'] if e.entity_type == FACULTY or ( acronym_exceptions and e.acronym in acronym_exceptions) }) entity_version = entity_structure[entity.id]['entity_version'] if acronym_exceptions and entity_version.acronym in acronym_exceptions: faculties.add(entity) else: faculties.add( find_parent_of_type_into_entity_structure( entity_version, entity_structure, FACULTY)) return find_all_current_entities_version().filter(entity__in=faculties)
def find_entities_by_person(person): person_entities = PersonEntity.objects.filter( person=person).select_related('entity') entities = set() entities |= { pers_ent.entity for pers_ent in person_entities if not pers_ent.with_child } entities_with_child = [ pers_ent.entity for pers_ent in person_entities if pers_ent.with_child ] entities_data = entity_version.build_current_entity_version_structure_in_memory( ) for entity_with_child in entities_with_child: entities.add(entity_with_child) entity_data = entities_data.get(entity_with_child.id) if entity_data: entities |= set([ ent_version.entity for ent_version in entity_data['all_children'] ]) return list(entities)
def cache_structure(self): if self._cache_structure is None: self._cache_structure = build_current_entity_version_structure_in_memory( ) return self._cache_structure