def get_queryset(self): country = self.forwarded.get('country', None) if country and country == "all": qs = find_all_current_entities_version().order_by('acronym') elif country: qs = find_all_current_entities_version().filter(entity__country__id=country).order_by('acronym') else: qs = find_pedagogical_entities_version() if self.q: qs = qs.filter(acronym__icontains=self.q).order_by('acronym') return 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_build_entity_version_by_entity_id_parent(self): all_current_entities_version = entity_version.find_all_current_entities_version( ) result = entity_version._build_entity_version_by_entity_id( all_current_entities_version) expected_keys = [v.entity_id for v in all_current_entities_version] self.assertListEqual(list(sorted(result.keys())), sorted(expected_keys))
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_build_direct_children_by_entity_version_id(self): entity_version_by_entity_id = entity_version._build_entity_version_by_entity_id( entity_version.find_all_current_entities_version()) result = entity_version._build_direct_children_by_entity_version_id(entity_version_by_entity_id) count_entities_version_with_children = 4 self.assertEqual(len(result.keys()), count_entities_version_with_children) root_direct_children = [self.SC, self.LOCI] self.assertEqual(set(result[self.root.id]), set(root_direct_children)) sc_direct_children = [self.MATH, self.PHYS] self.assertEqual(set(result[self.SC.id]), set(sc_direct_children)) self.assertNotIn(self.MATH.id, result) # No children for MATH
class EntityContainerYearModelForm(forms.ModelForm): entity = EntitiesVersionChoiceField( widget=autocomplete.ModelSelect2(url='entity_autocomplete', attrs={'data-html': True}, forward=['country']), queryset=find_all_current_entities_version()) entity_type = '' country = forms.ChoiceField(choices=lazy(_get_section_choices, list), required=False, label=_("Country")) def __init__(self, *args, **kwargs): self.person = kwargs.pop('person') super().__init__(*args, prefix=self.entity_type.lower(), **kwargs) self.fields['entity'].label = EntityContainerYearLinkTypes[ self.entity_type].value self.instance.type = self.entity_type if hasattr(self.instance, 'entity'): self.initial['entity'] = get_last_version(self.instance.entity).pk class Meta: model = EntityContainerYear fields = ['entity', 'country'] def pre_save(self, learning_container_year): self.instance.learning_container_year = learning_container_year def save(self, commit=True): if hasattr(self.instance, 'entity'): return super().save(commit) elif self.instance.pk: # if the instance has no entity, it must be deleted self.instance.delete() @property def entity_version(self): return self.fields["entity"].entity_version def post_clean(self, start_date): entity = self.cleaned_data.get('entity') if not entity: return
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 test_build_all_children_by_entity_version_id(self): all_current_entites_versions = entity_version.find_all_current_entities_version( ) entity_version_by_entity_id = entity_version._build_entity_version_by_entity_id( all_current_entites_versions) direct_children_by_entity_version_id = entity_version \ ._build_direct_children_by_entity_version_id(entity_version_by_entity_id) result = entity_version._build_all_children_by_entity_version_id( direct_children_by_entity_version_id) count_entities_version_with_children = 4 self.assertEqual(len(result.keys()), count_entities_version_with_children) root_all_children = [ self.SC, self.LOCI, self.MATH, self.PHYS, self.URBA, self.BARC ] self.assertEqual(set(result[self.root.id]), set(root_all_children)) sc_all_children = [self.MATH, self.PHYS] self.assertEqual(set(result[self.SC.id]), set(sc_all_children)) self.assertNotIn(self.MATH.id, result.keys())
def __init__(self, queryset, *args, **kwargs): queryset = find_all_current_entities_version().filter(entity__organization__type=MAIN).order_by('acronym') super(PublicationContactEntityChoiceField, self).__init__(queryset, *args, **kwargs)
def __init__(self, queryset, *args, **kwargs): queryset = find_all_current_entities_version().filter( entity__organization__type=MAIN).order_by('acronym') super(PublicationContactEntityChoiceField, self).__init__(queryset, *args, **kwargs)