def _encode_entity(self, encoded: Dict, entity: Entity) -> None: self._encode_schema( encoded, upper_camel_case_to_lower_camel_case( get_entity_type_name(entity.entity_type()))) if 'links' not in encoded: encoded['links'] = [] if not isinstance(entity.id, GeneratedEntityId): encoded['id'] = entity.id canonical = Link(self._generate_url(entity)) canonical.relationship = 'canonical' canonical.media_type = 'application/json' encoded['links'].append(canonical) for locale_configuration in self._app.configuration.locales: if locale_configuration.locale == self._locale: continue translation = Link( self._generate_url(entity, locale=locale_configuration.locale)) translation.relationship = 'alternate' translation.locale = locale_configuration.locale encoded['links'].append(translation) html = Link(self._generate_url(entity, media_type='text/html')) html.relationship = 'alternate' html.media_type = 'text/html' encoded['links'].append(html)
async def test_populate_link_should_set_locale(self, expected: str, entry_language: str, locale: Optional[str], m_retriever) -> None: link = Link('http://%s.wikipedia.org/wiki/Amsterdam' % entry_language) link.locale = locale with TemporaryDirectory() as output_directory_path: with TemporaryDirectory() as cache_directory_path: configuration = Configuration( output_directory_path, 'https://example.com') configuration.cache_directory_path = cache_directory_path async with App(configuration) as app: sut = _Populator(app, m_retriever) await sut.populate_link(link, entry_language) self.assertEqual(expected, link.locale)
async def test_link_should_encode_full(self) -> None: link = Link('https://example.com') link.label = 'The Link' link.relationship = 'external' link.locale = 'nl-NL' link.media_type = MediaType('text/html') expected = { 'url': 'https://example.com', 'relationship': 'external', 'label': 'The Link', 'locale': 'nl-NL', 'mediaType': 'text/html', } await self.assert_encodes(expected, link, 'link')
async def populate_link(self, link: Link, entry_language: str, entry: Optional[Entry] = None) -> None: if link.url.startswith('http:'): link.url = 'https:' + link.url[5:] if link.media_type is None: link.media_type = MediaType('text/html') if link.relationship is None: link.relationship = 'external' if link.locale is None: link.locale = entry_language if link.description is None: # There are valid reasons for links in locales that aren't supported. with suppress(ValueError): async with self._app.with_locale(link.locale): link.description = _('Read more on Wikipedia.') if entry is not None and link.label is None: link.label = entry.title