def get_translated_group_title(titles_string): group_titles_dict = parse_json(titles_string) return get_localized_value( group_titles_dict, i18n.get_lang(), titles_string )
def mail_process_status(locale, member_user, approve, group_name, capacity): current_locale = get_lang() if locale == 'en': _reset_lang() else: set_lang(locale) role_name = _(capacity) subject_template = _SUBJECT_MEMBERSHIP_APPROVED( ) if approve else _SUBJECT_MEMBERSHIP_REJECTED() message_template = _MESSAGE_MEMBERSHIP_APPROVED( ) if approve else _MESSAGE_MEMBERSHIP_REJECTED() subject = subject_template % {'organization': group_name} message = message_template % { 'role': role_name, 'organization': group_name } try: mail_user(member_user, subject, message) except Exception: log.exception("Mail could not be sent") # raise MailerException("Mail could not be sent") finally: set_lang(current_locale)
def mail_process_status(locale, member_user, approve, group_name, capacity): current_locale = get_lang() if locale == 'en': _reset_lang() else: set_lang(locale) role_name = _(capacity) subject_template = _SUBJECT_MEMBERSHIP_APPROVED( ) if approve else _SUBJECT_MEMBERSHIP_REJECTED() message_template = _MESSAGE_MEMBERSHIP_APPROVED( ) if approve else _MESSAGE_MEMBERSHIP_REJECTED() subject = subject_template % { 'organization': group_name } message = message_template % { 'role': role_name, 'organization': group_name } try: mail_user(member_user, subject, message) except Exception: log.exception("Mail could not be sent") # raise MailerException("Mail could not be sent") finally: set_lang(current_locale)
def _mail_new_membership_request(locale, admin, group, url, user_obj, data_dict=None, admin_list=None): current_locale = get_lang() if locale == 'en': _reset_lang() else: set_lang(locale) subject = hdx_util._SUBJECT_MEMBERSHIP_REQUEST.format(**{ 'user_fullname': user_obj.display_name }) user_message = data_dict.get('message') if data_dict else 'Please add me to this organisation' message = hdx_util._MESSAGE_MEMBERSHIP_REQUEST.format(**{ 'user_fullname': user_obj.display_name, 'user_email': user_obj.email, 'org_title': group.display_name, 'org_add_member_url': (config['ckan.site_url'] + '/organization/members/{org_name}').format( org_name=group.name), 'user_message': user_message }) try: # mail_user(admin, subject, message) # HDX change if admin: hdx_mail.send_mail([{'display_name': admin.display_name or admin.fullname, 'email': admin.email}], subject, message) else: hdx_mail.send_mail(admin_list, subject, message, True) except MailerException, e: log.error(e)
def _send_comment_notification_mail(recipient_name, recipient_email, data, lang="en"): ''' A helper function to send notification emails to given recipients ''' current_locale = get_lang() if lang: set_lang(lang) from ckanext.datarequests import email_template # recreate the request url url = str(g.site_url) + tk.url_for( controller= 'ckanext.datarequests.controllers.ui_controller:DataRequestsUI', action='show', id=data.get("request_id")) data["link"] = url # fill out the email template subject = email_template.subject.format(**data) message = email_template.message.format(**data) # Finally mail the user and reset locale try: mail_recipient(recipient_name, recipient_email, subject, message) except MailerException, e: log.error(e)
def get_fao_m49_region(name): lang = get_lang() term = VocabularyTerm.get(Vocabulary.VOCABULARY_M49_REGIONS, name) if term: label = term.get_label(lang) or term.get_label(DEFAULT_LANG) or term.get_label('en') return label.label return name
def get_fao_agrovoc_term(name): lang = get_lang() term = VocabularyTerm.get(Vocabulary.VOCABULARY_AGROVOC, name) if term: label = term.get_label(lang) or term.get_label(DEFAULT_LANG) or term.get_label('en') return label.label return name
def _send_comment_notification_mail(recipient_name, recipient_email, data, lang="en"): ''' A helper function to send notification emails to given recipients ''' current_locale = get_lang() if lang: set_lang(lang) from ckanext.datarequests import email_template # recreate the request url url = str(g.site_url) + tk.url_for(controller='ckanext.datarequests.controllers.ui_controller:DataRequestsUI', action='show', id=data.get("request_id")) data["link"] = url # fill out the email template subject = email_template.subject.format(**data) message = email_template.message.format(**data) # Finally mail the user and reset locale try: mail_recipient(recipient_name, recipient_email, subject, message) except MailerException, e: log.error(e)
def fao_autocomplete(self, _vocabulary): q = request.str_params.get('incomplete', '') q = unicode(urllib.unquote(q), 'utf-8') limit = request.params.get('limit', 10) lang = request.params.get('lang') or get_lang() tag_names = [] if q: context = { 'model': model, 'session': model.Session, 'user': c.user or c.author, 'auth_user_obj': c.userobj } data_dict = { 'q': q, 'limit': limit, 'lang': lang, 'vocabulary': _vocabulary } tag_names = get_action('fao_autocomplete')(context, data_dict) tag_names = tag_names['tags'] resultSet = { 'ResultSet': { 'Result': tag_names, }, 'Query': q, 'Lang': lang, } return self._finish_ok(resultSet)
def get_url_for_location(location_code): lang = get_lang() term = VocabularyTerm.get(Vocabulary.VOCABULARY_M49_REGIONS, location_code) if not term: return h.url_for('search') label = term.get_label(lang).label or term.get_label('en').label qdict = {'fao_m49_regions_l{}_{}'.format(term.depth, lang): label} return h.url_for('search', **qdict)
def get_vocabulary_items_annotated(vocabulary_name, is_multiple=False, filters=None): return VocabularyTerm.get_terms(vocabulary_name, lang=get_lang(), include_dataset_count=True, is_multiple=is_multiple, filters=filters)
def get_vocabulary_items(vocabulary_name, is_multiple=False, filters=None, order_by=None): return VocabularyTerm.get_terms(vocabulary_name, lang=get_lang(), is_multiple=is_multiple, filters=filters, order_by=order_by)
def getLanguage(): lang = get_lang() if lang is not None: if isinstance(lang, list): lang = unicode(lang[0]) else: lang = unicode(lang) return lang
def ensure_translated(s): ts = type(s) if ts == unicode: return s elif ts == str: return unicode(s) elif ts == dict: language = i18n.get_lang() return ensure_translated(s.get(language, u""))
def get_translated(data_dict, field): translated = data_dict.get('%s_translated' % field) if isinstance(translated, dict): language = i18n.get_lang() if language in translated: return translated[language] dialects = [l for l in translated if l.startswith(language) or language.startswith(l)] if dialects: return translated[dialects[0]] return data_dict.get(field)
def _render_tree_node(node): html = '<div class="organization-row">' html += '<a href="/%s/organization/%s">%s</a>' % (i18n.get_lang(), node['name'], node['title']) # noqa html += '</div>' if node['children']: html += '<ul>' for child in node['children']: html += _render_tree_node(child) html += '</ul>' html = '<li id="node_%s" class="organization">%s</li>' % (node['name'], html) # noqa return html
def get_language(): try: lang = get_lang() except Exception as e: lang = config.get(u'ckan.locale_default', u'it') # log.debug(f'Exception while retrieving lang. Using [{lang}]', stack_info=True) if lang is not None: if isinstance(lang, list): lang = lang[0] return lang
def index(): context = {u'model': model, u'session': model.Session, u'lang': get_lang()} data_dict = {} try: pages = logic.get_action(u'marsavin_pages_list')(context, data_dict) except logic.NotAuthorized: base.abort(403, toolkit._(u'You must be logged in to view this page')) extra_vars = {u'pages': pages} u''' display privacy page''' return base.render(u'pages/index.html', extra_vars=extra_vars)
def delete(page): u'''Delete user with id passed as parameter''' context = {u'model': model, u'session': model.Session} data_dict = {u'page': page, u'lang': get_lang()} try: logic.get_action(u'marsavin_pages_delete')(context, data_dict) except logic.NotAuthorized: msg = toolkit._(u'Unauthorized to delete user with id "{user_id}".') base.abort(403, msg.format(user_id=id)) return toolkit.redirect_to(toolkit.url_for(u'marsavin_pages.index'))
def ogdch_get_organization_field_list(field): user = tk.get_action('get_site_user')({'ignore_auth': True}, {}) req_context = {'user': user['name']} orgs = tk.get_action('organization_list')( req_context, {'all_fields': True} ) return [{'value': org['name'], 'label': ogdch_localize_utils.get_localized_value_from_json( # noqa org['title'], i18n.get_lang() )} for org in orgs]
def get_sorted_orgs_by_translated_title(organizations): for organization in organizations: organization['title'] = ogdch_loc_utils.get_localized_value_from_json( organization['title'], i18n.get_lang()) # noqa if organization['children']: organization['children'] = get_sorted_orgs_by_translated_title( organization['children']) # noqa organizations.sort( key=lambda org: ogdch_loc_utils.strip_accents(org['title'].lower()), reverse=False) # noqa return organizations
def notify(self): prev_lang = i18n.get_lang() i18n.set_lang(config.get('ckan.locale_default', 'en')) comment_id = request.params.get('comment_id') comment = request.params.get('comment') pkg_id = request.params.get('pkg_id') recipient_name_field = config.get('disqus.notify.name_field') recipient_email_field = config.get('disqus.notify.email_field') mail_sent = False if pkg_id is not None: pkg = logic.get_action('package_show')({ 'ignore_auth': True }, { 'id': pkg_id }) if recipient_email_field in pkg and pkg.get( recipient_email_field, '') != '': url_path = toolkit.url_for(controller='package', action='read', id=pkg_id) url = '%s%s' % (request.host_url, url_path) msg_fields = { 'url': url, 'title': pkg["title"], 'comment': comment } title = _(u'New comment for dataset "%(title)s"') % msg_fields msg = _( u'The dataset "%(title)s" has received a new comment:\n\n%(comment)s\n\n<%(url)s>' ) % msg_fields recipient_name = pkg.get(recipient_name_field, _(u'Dataset maintainer')) recipient_email = pkg.get(recipient_email_field) try: mailer.mail_recipient(recipient_name, recipient_email, title, msg) mail_sent = True except mailer.MailerException as e: log.error('Could not send disqus notification mail: %s' % e) data = { 'comment_id': comment_id, 'pkg_id': pkg_id, 'mail_sent': mail_sent } result = render('disqus_callback.html', data) i18n.set_lang(prev_lang) return result
def get_translated_or_default_locale(data_dict, field): language = i18n.get_lang() if language in _LOCALE_ALIASES: language = _LOCALE_ALIASES[language] try: value = data_dict[field+'_translated'][language] if value: return value else: return data_dict[field+'_translated'][config.get('ckan.locale_default', 'en')] except KeyError: return data_dict.get(field, '')
def get_snippet_actor(activity, detail): user = authz.auth_is_loggedin_user() if not user: if i18n.get_lang() == 'fr': return literal( '''<span class="actor">%s</span>''' % "L'équipe de données ouvertes de l'Ontario".decode('utf8')) else: return literal('''<span class="actor">Ontario's Open Data Team \ </span>''') else: return literal('''<span class="actor">%s</span>''' % (helpers.linked_user(activity['user_id'], 0, 30)))
def dataset_facets(self, facets_dict, package_type): lang = get_lang() head_facets_dict = collections.OrderedDict([]) head_facets_dict['fao_datatype_{}'.format(lang)] = t._("Data type") facets_dict['fao_agrovoc_{}'.format(lang)] = t._("AGROVOC terms") for idx, l in ((0, t._("Regions"),), (1, t._("Countries"),)): facets_dict['fao_m49_regions_l{}_{}'.format(idx, lang)] = l facets_dict = collections.OrderedDict(list(head_facets_dict.items()) + list(facets_dict.items())) return facets_dict
def dataset_facets(self, facets_dict, package_type): lang = get_lang() facets_dict['fao_datatype_{}'.format(lang)] = t._("Data type") facets_dict['fao_agrovoc_{}'.format(lang)] = t._("AGROVOC terms") for idx, l in (( 0, t._("Regions"), ), ( 1, t._("Countries"), )): facets_dict['fao_m49_regions_l{}_{}'.format(idx, lang)] = l return facets_dict
def dataset_facets(self, facets_dict, package_type): if (package_type == 'showcase'): facets_dict = OrderedDict() lang = i18n.get_lang() if lang in self._LOCALE_ALIASES: lang = self._LOCALE_ALIASES[lang] facets_dict['vocab_category_' + lang] = _('Category') facets_dict.update({'vocab_platform': _('Platform')}) return facets_dict
def get_translated(data_dict, field, fallback_language=None): if (data_dict.get(field + '_translated')): language = i18n.get_lang() if language in _LOCALE_ALIASES: language = _LOCALE_ALIASES[language] try: return data_dict[field + '_translated'][language] or data_dict[ field + '_translated'][fallback_language] except KeyError: return data_dict.get(field, '') if data_dict.get('display_name', None) is not None: return data_dict.get('display_name') return data_dict.get(field)
def get_translated(json_str): """ Converts json to dict and gets the appropriate value for the current language :param json_str: the json with translates, example {'mk': '', 'en': '', 'sq': ''} :type json_str: str/unicode :returns: the appropriate value for the current language from the json :rtype: str """ try: lang = i18n.get_lang() d = json.loads(json_str) return d.get(lang, None) if isinstance(d, dict) else '' except: return ''
def send_comment_notification_mail(recipient_name, recipient_email, dataset, comment): from ckanext.ytp_comments import email_template # Fill out the message template url = str(g.site_url) + toolkit.url_for(controller='package', action='read', id=dataset.id) if comment.user_id: userobj = model.User.get(comment.user_id) commenter_email = userobj.email commenter_name = userobj.name subject_vars = { 'dataset': dataset.title } subject = email_template.subject.format(**subject_vars) message_vars = { 'user': commenter_name, 'email': commenter_email, 'dataset': dataset.title, 'link': url, 'comment_subject': helpers.markdown_extract(comment.subject).strip(), 'comment': helpers.markdown_extract(comment.comment).strip() } message = email_template.message.format(**message_vars) log.debug(subject) log.debug(message) # Locale fix current_locale = get_lang() locale = _get_safe_locale() if locale == 'en': _reset_lang() else: set_lang(locale) # Finally mail the user and reset locale try: log.debug("LOCALE: " + str(locale)) log.debug(subject) log.debug(message) mail_recipient(recipient_name, recipient_email, subject, message) except MailerException, e: log.error(e)
def get_translation(translated): if isinstance(translated, unicode): translated = get_json_value(translated) if isinstance(translated, dict): language = i18n.get_lang() if language in translated: return translated[language] dialects = [ l for l in translated if l.startswith(language) or language.startswith(l) ] if dialects: return translated[dialects[0]] return None
def post(self, page): context = self._prepare(page) try: data_dict = clean_dict( dict_fns.unflatten( tuplize_dict(parse_params(toolkit.request.form)))) data_dict[u"lang"] = get_lang() page = toolkit.get_action(u"marsavin_pages_edit")(context, data_dict) except (toolkit.ObjectNotFound) as e: base.abort(404, toolkit._(u'Page not found: %s' % e)) except dict_fns.DataError: base.abort(400, toolkit._(u'Integrity Error')) except toolkit.ValidationError as e: errors = e.error_dict error_summary = e.error_summary return self.get(data_dict, errors, error_summary) return toolkit.redirect_to(u"marsavin_pages.read", page=page.name)
def mail_new_membership_request(locale, admin, group_name, url, user_name, user_email): # TODO: Set admin locale. Admin/user locale is stored at drupal database so may be a bit challenging to fetch it. We default to finnish for the time being current_locale = get_lang() i18n.set_lang("fi") subject = _SUBJECT_MEMBERSHIP_REQUEST() % { 'organization': group_name } message = _MESSAGE_MEMBERSHIP_REQUEST() % { 'user': user_name, 'email': user_email, 'organization': group_name, 'link': url } try: mail_user(admin, subject, message) except Exception: log.exception("Mail could not be sent") finally: set_lang(current_locale)
def _mail_new_membership_request(locale, admin, group_name, url, user_name, user_email): current_locale = get_lang() if locale == 'en': _reset_lang() else: set_lang(locale) subject = _SUBJECT_MEMBERSHIP_REQUEST() % { 'organization': group_name } message = _MESSAGE_MEMBERSHIP_REQUEST() % { 'user': user_name, 'email': user_email, 'organization': group_name, 'link': url } try: mail_user(admin, subject, message) except MailerException, e: log.error(e)
c.pkg = context['package'] except NotAuthorized: abort(401, _('Unauthorized to read package %s') % '') except NotFound: abort(404, _('Dataset not found')) format = request.params.get('format', '') if format == 'atom': # Generate and return Atom 1.0 document. from webhelpers.feedgenerator import Atom1Feed feed = Atom1Feed( title=_(u'CKAN Dataset Revision History'), link=h.url_for(controller='revision', action='read', id=c.pkg_dict['name']), description=_(u'Recent changes to CKAN Dataset: ') + (c.pkg_dict['title'] or ''), language=unicode(get_lang()), ) for revision_dict in c.pkg_revisions: revision_date = h.date_str_to_datetime(revision_dict['timestamp']) try: dayHorizon = int(request.params.get('days')) except: dayHorizon = 30 dayAge = (datetime.datetime.now() - revision_date).days if dayAge >= dayHorizon: break if revision_dict['message']: item_title = u'%s' % revision_dict['message'].split('\n')[0] else: item_title = u'%s' % revision_dict['id'] item_link = h.url_for(controller='revision', action='read', id=revision_dict['id'])
def localised_number(number): ''' Returns a localised unicode representation of number ''' return numbers.format_number(number, locale=i18n.get_lang())
def history(package_type, id): if u'diff' in request.args or u'selected1' in request.args: try: params = { u'id': request.args.getone(u'pkg_name'), u'diff': request.args.getone(u'selected1'), u'oldid': request.args.getone(u'selected2'), } except KeyError: if u'pkg_name' in dict(request.args): id = request.args.getone(u'pkg_name') g.error = \ _(u'Select two revisions before doing the comparison.') else: params[u'diff_entity'] = u'package' return h.redirect_to( controller=u'revision', action=u'diff', **params ) context = { u'model': model, u'session': model.Session, u'user': g.user, u'auth_user_obj': g.userobj, u'for_view': True } data_dict = {u'id': id} try: g.pkg_dict = get_action(u'package_show')(context, data_dict) g.pkg_revisions = get_action(u'package_revision_list')( context, data_dict ) # TODO: remove # Still necessary for the authz check in group/layout.html g.pkg = context[u'package'] except NotAuthorized: return base.abort(403, _(u'Unauthorized to read package %s') % u'') except NotFound: return base.abort(404, _(u'Dataset not found')) format = request.args.get(u'format', u'') if format == u'atom': # Generate and return Atom 1.0 document. from webhelpers.feedgenerator import Atom1Feed feed = Atom1Feed( title=_(u'CKAN Dataset Revision History'), link=h.url_for( controller=u'revision', action=u'read', id=g.pkg_dict[u'name'] ), description=_(u'Recent changes to CKAN Dataset: ') + (g.pkg_dict[u'title'] or u''), language=text_type(i18n.get_lang()), ) for revision_dict in g.pkg_revisions: revision_date = h.date_str_to_datetime(revision_dict[u'timestamp']) try: dayHorizon = int(request.args.get(u'days')) except Exception: dayHorizon = 30 dayAge = (datetime.datetime.now() - revision_date).days if dayAge >= dayHorizon: break if revision_dict[u'message']: item_title = u'%s' % revision_dict[u'message'].\ split(u'\n')[0] else: item_title = u'%s' % revision_dict[u'id'] item_link = h.url_for( controller=u'revision', action=u'read', id=revision_dict[u'id'] ) item_description = _(u'Log message: ') item_description += u'%s' % (revision_dict[u'message'] or u'') item_author_name = revision_dict[u'author'] item_pubdate = revision_date feed.add_item( title=item_title, link=item_link, description=item_description, author_name=item_author_name, pubdate=item_pubdate, ) response = make_response(feed.writeString(u'utf-8')) response.headers[u'Content-Type'] = u'application/atom+xml' return response package_type = g.pkg_dict[u'type'] or u'dataset' return base.render( _get_pkg_template( u'history_template', g.pkg_dict.get(u'type', package_type) ), extra_vars={u'dataset_type': package_type} )
def ogdch_localised_number(number): # use swissgerman formatting rules when current language is german if i18n.get_lang() == 'de': return numbers.format_number(number, locale='de_CH') else: return localised_number(number)