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 populate_link(self, link: Link, site: Site, 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 = '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 site.with_locale(link.locale): link.description = _('Read more on Wikipedia.') if entry is not None and link.label is None: link.label = entry.title
def _encode_identifiable_resource( self, encoded: Dict, resource: Union[Identifiable, Resource]) -> None: if 'links' not in encoded: encoded['links'] = [] canonical = Link(self._generate_url(resource)) canonical.relationship = 'canonical' canonical.media_type = 'application/json' encoded['links'].append(canonical) for locale in self._site.configuration.locales: if locale == self._locale: continue translation = Link(self._generate_url(resource, locale=locale)) translation.relationship = 'alternate' translation.locale = locale encoded['links'].append(translation) html = Link(self._generate_url(resource, media_type='text/html')) html.relationship = 'alternate' html.media_type = 'text/html' encoded['links'].append(html)