def send(subject, recipients, template_base, **kwargs): ''' Send a given email to multiple recipients. User prefered language is taken in account. To translate the subject in the right language, you should ugettext_lazy ''' sender = kwargs.pop('sender', None) if not isinstance(recipients, (list, tuple)): recipients = [recipients] debug = current_app.config.get('DEBUG') connection = debug and dummyconnection or mail.connect with connection() as conn: for recipient in recipients: lang = i18n._default_lang(recipient) with i18n.language(lang): log.debug( 'Sending mail "%s" to recipient "%s"', subject, recipient) msg = Message(subject, sender=sender, recipients=[recipient.email]) msg.body = theme.render( 'mail/{0}.txt'.format(template_base), subject=subject, sender=sender, recipient=recipient, **kwargs) msg.html = theme.render( 'mail/{0}.html'.format(template_base), subject=subject, sender=sender, recipient=recipient, **kwargs) if debug: log.debug(msg.body) log.debug(msg.html) else: conn.send(msg)
def archive(dataset, comment=False): """Archive a dataset""" if dataset.archived: log.warning('Dataset %s already archived, bumping date', dataset) dataset.archived = datetime.now() dataset.save() if comment: log.info('Posting comment for dataset %s...', dataset) lang = current_app.config['DEFAULT_LANGUAGE'] title = current_app.config['ARCHIVE_COMMENT_TITLE'] user_id = current_app.config['ARCHIVE_COMMENT_USER_ID'] if user_id: with i18n.language(lang): msg = theme.render('comments/dataset_archived.txt') message = Message(content=msg, posted_by=user_id) discussion = Discussion(user=user_id, discussion=[message], subject=dataset, title=str(title)) discussion.save() else: log.warning('ARCHIVE_COMMENT_USER_ID not set, skipping comment') log.info('Archived dataset %s', dataset)
def test_display_preview_for_api_resources(locale): resource = ResourceFactory(extras={ 'geop:resource_id': 'RID', }) DatasetFactory(resources=[resource], extras={'geop:dataset_id': 'DID'}) expected = 'https://geo.data.gouv.fr/embed/datasets/DID/resources/RID?lang={0}'.format( locale) # noqa with language(locale): assert resource.preview_url == expected
def test_display_preview_for_api_resources(locale): resource = ResourceFactory(extras={ 'geop:resource_id': 'RID', }) DatasetFactory(resources=[resource], extras={ 'geop:dataset_id': 'DID' }) expected = 'https://geo.data.gouv.fr/embed/datasets/DID/resources/RID?lang={0}'.format(locale) # noqa with language(locale): assert resource.preview_url == expected
def labels_for_zone(zone): ''' Extract all known zone labels - main code - keys (postal...) - name translation in every supported languages ''' labels = set([zone.name, zone.code] + zone.keys_values) for lang in current_app.config['LANGUAGES'].keys(): with language(lang): labels.add(_(zone.name)) return list(labels)
def title_from_rdf(rdf, url): ''' Try to extract a distribution title from a property. As it's not a mandatory property, it fallback on building a title from the URL then the format and in last ressort a generic resource name. ''' title = rdf_value(rdf, DCT.title) if title: return title if url: last_part = url.split('/')[-1] if '.' in last_part and '?' not in last_part: return last_part fmt = rdf_value(rdf, DCT.term('format')) lang = current_app.config['DEFAULT_LANGUAGE'] with i18n.language(lang): if fmt: return i18n._('{format} resource').format(format=fmt.lower()) else: return i18n._('Nameless resource')
def get_spatial_granularities(lang): with language(lang): return [(l.id, _(l.name)) for l in GeoLevel.objects ] + [(id, label.value) for id, label in BASE_GRANULARITIES]
def get_spatial_granularities(lang): with language(lang): return [ (l.id, _(l.name)) for l in GeoLevel.objects ] + [(id, label.value) for id, label in BASE_GRANULARITIES]
def hardcoded(): out = g.lang_code + '-' with language('fr'): out += g.lang_code return out
def not_localized(): out = g.lang_code + '-' with language('pt'): out += g.lang_code return out