def derive(cls, person: Person, derivable_event: Event, reference_event_type_types: Set[Type[EventType]]) -> bool: if not reference_event_type_types: return False reference_events = _get_reference_events(person, reference_event_type_types) reference_events_dates = cls._get_events_dates(reference_events) reference_events_dates = filter(lambda x: x[1].comparable, reference_events_dates) if derivable_event.date is not None: reference_events_dates = filter( lambda x: cls._compare(derivable_event.date, x[1]), reference_events_dates) reference_events_dates = cls._sort(reference_events_dates) try: reference_event, reference_date = reference_events_dates[0] except IndexError: return False if derivable_event.date is None: derivable_event.date = DateRange() cls._set(derivable_event, DerivedDate.derive(reference_date)) derivable_event.citations.append(*reference_event.citations) return True
def _load_event(loader: _Loader, element: ElementTree.Element): event_handle = element.get('handle') event_id = element.get('id') gramps_type = _xpath1(element, './ns:type') try: event_type = _EVENT_TYPE_MAP[gramps_type.text] except KeyError: event_type = UnknownEventType() getLogger().warning( 'Betty is unfamiliar with Gramps event "%s"\'s type of "%s". The event was imported, but its type was set to "%s".' % (event_id, gramps_type.text, event_type.label)) event = Event(event_id, event_type) event.date = _load_date(element) # Load the event place. place_handle = _load_handle('place', element) if place_handle is not None: loader.add_association(Event, event_handle, 'place', Place, place_handle) # Load the description. description_element = _xpath1(element, './ns:description') if description_element is not None: event.description = description_element.text _load_attribute_privacy(event, element, 'attribute') flattened_event = FlattenedEntity(event, event_handle) _load_objref(loader, flattened_event, element) _load_citationref(loader, flattened_event, element) loader.add_entity(flattened_event)
async def test_with_date(self): event = Event(None, Birth()) event.date = Date(1970) expected = '1970' async with self._render(data={ 'event': event, }) as (actual, _): self.assertEqual(expected, actual)
async def test_with_date_and_place(self): event = Event(None, Birth()) event.date = Date(1970) event.place = Place('P0', [PlaceName('The Place')]) expected = '1970 in <address><a href="/place/P0/index.html"><span>The Place</span></a></address>' async with self._render(data={ 'event': event, }) as (actual, _): self.assertEqual(expected, actual)
async def test_embedded(self): event = Event(None, Birth()) event.date = Date(1970) event.place = Place('P0', [PlaceName('The Place')]) event.citations.append(Citation(None, Source(None, 'The Source'))) expected = '1970 in <address><span>The Place</span></address>' async with self._render(data={ 'event': event, 'embedded': True, }) as (actual, _): self.assertEqual(expected, actual)
def test_date(self) -> None: sut = Event(None, Mock(EventType)) self.assertIsNone(sut.date) date = Mock(Date) sut.date = date self.assertEquals(date, sut.date)
async def test_event_should_encode_full(self): event = Event('the_event', Birth()) event.date = DateRange(Date(2000, 1, 1), Date(2019, 12, 31)) event.place = Place('the_place', [PlaceName('The Place')]) Presence(Person('the_person'), Subject(), event) event.citations.append(Citation('the_citation', Source('The Source'))) expected = { '$schema': '/schema.json#/definitions/event', '@context': { 'place': 'https://schema.org/location', }, '@type': 'https://schema.org/Event', 'id': 'the_event', 'type': 'birth', 'presences': [ { '@context': { 'person': 'https://schema.org/actor', }, 'role': 'subject', 'person': '/en/person/the_person/index.json', }, ], 'citations': [ '/en/citation/the_citation/index.json', ], 'date': { 'start': { 'year': 2000, 'month': 1, 'day': 1, }, 'end': { 'year': 2019, 'month': 12, 'day': 31, }, }, 'place': '/en/place/the_place/index.json', 'links': [ { 'url': '/en/event/the_event/index.json', 'relationship': 'canonical', 'mediaType': 'application/json', }, { 'url': '/nl/event/the_event/index.json', 'relationship': 'alternate', 'locale': 'nl-NL', }, { 'url': '/en/event/the_event/index.html', 'relationship': 'alternate', 'mediaType': 'text/html', }, ], } await self.assert_encodes(expected, event, 'event')