def notify_ckan_user_create(email, fullname, username, phoneno, dept): """ Send an e-mail notification about new users that register on the site to the configured recipient and to the new user """ import ckan.lib.mailer try: if 'canada.notification_new_user_email' in config: xtra_vars = { 'email': email, 'fullname': fullname, 'username': username, 'phoneno': phoneno, 'dept': dept } ckan.lib.mailer.mail_recipient( config.get( 'canada.notification_new_user_name', config['canada.notification_new_user_email'] ), config['canada.notification_new_user_email'], ( u'New Registry Account Created / Nouveau compte' u' cr\u00e9\u00e9 dans le registre de Gouvernement ouvert' ), render( 'user/new_user_email.html', extra_vars=xtra_vars ) ) except ckan.lib.mailer.MailerException as m: log = getLogger('ckanext') log.error(m.message) try: xtra_vars = { 'email': email, 'fullname': fullname, 'username': username, 'phoneno': phoneno, 'dept': dept } ckan.lib.mailer.mail_recipient( fullname or email, email, ( u'Welcome to the Open Government Registry / ' u'Bienvenue au Registre de Gouvernement Ouvert' ), render( 'user/user_welcome_email.html', extra_vars=xtra_vars ) ) except (ckan.lib.mailer.MailerException, socket.error) as m: log = getLogger('ckanext') log.error(m.message)
def pages_list_pages(page_type): data_dict = {'org_id': None, 'page_type': page_type} if page_type == 'blog': data_dict['order_publish_date'] = True tk.c.pages_dict = tk.get_action('ckanext_pages_list')(data_dict=data_dict) tk.c.page = helpers.Page(collection=tk.c.pages_dict, page=tk.request.params.get('page', 1), url=helpers.pager_url, items_per_page=21) if page_type == 'blog': return tk.render('ckanext_pages/blog_list.html') return tk.render('ckanext_pages/pages_list.html')
def organization_index(self): context = {'model': model, 'session': model.Session, 'user': c.user or c.author, 'for_view': True, 'with_private': False} data_dict = {'all_fields': True} try: check_access('site_read', context) except NotAuthorized: abort(401, _('Not authorized to see this page')) # pass user info to context as needed to view private datasets of # orgs correctly if c.userobj: context['user_id'] = c.userobj.id context['user_is_admin'] = c.userobj.sysadmin results = get_action('organization_list')(context, data_dict) def org_key(org): title = org['title'].split(' | ')[-1 if c.language == 'fr' else 0] return normalize_strip_accents(title) results.sort(key=org_key) c.page = Page( collection=results, page=request.params.get('page', 1), url=h.pager_url, items_per_page=1000 ) return render('organization/index.html')
def group_show(id, group_type, page=None): if page and page.startswith('/'): page = page[1:] _template_setup_group(id, group_type) context = {'for_view': True} action = 'organization_show' if group_type == 'organization' else 'group_show' group_dict = tk.get_action(action)(context, {'id': id}) if not page: return group_list_pages(id, group_type, group_dict) _page = tk.get_action('ckanext_pages_show')(data_dict={ 'org_id': tk.c.group_dict['id'], 'page': page }) if _page is None: return group_list_pages(id, group_type, group_dict) tk.c.page = _page return tk.render('ckanext_pages/{}_page.html'.format(group_type), { 'group_type': group_type, 'group_dict': group_dict })
def pages_delete(page, page_type='pages'): if page.startswith('/'): page = page[1:] if 'cancel' in tk.request.params: if ckan_29_or_higher: return tk.redirect_to('pages.%s_edit' % page_type, page=page) else: tk.redirect_to('%s_edit' % page_type, page='/' + page) try: if tk.request.method == 'POST': tk.get_action('ckanext_pages_delete')({}, {'page': page}) if ckan_29_or_higher: endpoint = 'index' if page_type in ( 'pages', 'page') else '%s_index' % page_type return tk.redirect_to('pages.%s' % endpoint) else: tk.redirect_to('%s_index' % page_type) else: return tk.abort(404, _('Page Not Found')) except tk.NotAuthorized: return tk.abort(401, _('Unauthorized to delete page')) except tk.ObjectNotFound: return tk.abort(404, _('Group not found')) return tk.render('ckanext_pages/confirm_delete.html', {'page': page})
def read_view(id): context = { 'model': model, 'session': model.Session, 'user': c.user or c.author, 'for_view': True, 'auth_user_obj': c.userobj } data_dict = {'id': id} # check if showcase exists try: c.pkg_dict = tk.get_action('package_show')(context, data_dict) except tk.ObjectNotFound: return tk.abort(404, _('Showcase not found')) except tk.NotAuthorized: return tk.abort(401, _('Unauthorized to read showcase')) # get showcase packages c.showcase_pkgs = tk.get_action('ckanext_showcase_package_list')( context, { 'showcase_id': c.pkg_dict['id'] }) package_type = DATASET_TYPE_NAME return tk.render('showcase/read.html', extra_vars={'dataset_type': package_type})
def organization_index(self): context = { 'model': model, 'session': model.Session, 'user': c.user or c.author, 'for_view': True, 'with_private': False } data_dict = {'all_fields': True} try: check_access('site_read', context) except NotAuthorized: abort(401, _('Not authorized to see this page')) # pass user info to context as needed to view private datasets of # orgs correctly if c.userobj: context['user_id'] = c.userobj.id context['user_is_admin'] = c.userobj.sysadmin results = get_action('organization_list')(context, data_dict) def org_key(org): title = org['title'].split(' | ')[-1 if c.language == 'fr' else 0] return normalize_strip_accents(title) results.sort(key=org_key) c.page = Page(collection=results, page=request.params.get('page', 1), url=h.pager_url, items_per_page=1000) return render('organization/index.html')
def delete_view(id): if 'cancel' in tk.request.params: tk.redirect_to('showcase_blueprint.edit' if tk.check_ckan_version( min_version='2.9.0') else 'showcase_edit', id=id) context = { 'model': model, 'session': model.Session, 'user': c.user or c.author, 'auth_user_obj': c.userobj } try: tk.check_access('ckanext_showcase_delete', context, {'id': id}) except tk.NotAuthorized: return tk.abort(401, _('Unauthorized to delete showcase')) if tk.check_ckan_version(min_version='2.9.0'): index_route = 'showcase_blueprint.index' else: index_route = 'showcase_index' try: if tk.request.method == 'POST': tk.get_action('ckanext_showcase_delete')(context, {'id': id}) h.flash_notice(_('Showcase has been deleted.')) return tk.redirect_to(index_route) c.pkg_dict = tk.get_action('package_show')(context, {'id': id}) except tk.NotAuthorized: tk.abort(401, _('Unauthorized to delete showcase')) except tk.ObjectNotFound: tk.abort(404, _('Showcase not found')) return tk.render('showcase/confirm_delete.html', extra_vars={'dataset_type': DATASET_TYPE_NAME})
def register(self, data=None, errors=None, error_summary=None): '''GET to display a form for registering a new user. or POST the form data to actually do the user registration. The bulk of this code is pulled directly from ckan/controlllers/user.py ''' context = { 'model': model, 'session': model.Session, 'user': c.user or c.author, 'schema': schema.user_new_form_schema(), 'save': 'save' in request.params } try: check_access('user_create', context) except NotAuthorized: abort(401, _('Unauthorized to create a user')) if context['save'] and not data: try: return self._save_new(context) except HTTPFound: # redirected after successful user create notify_ckan_user_create( email=request.params.get('email', ''), fullname=request.params.get('fullname', ''), username=request.params.get('name', ''), phoneno=request.params.get('phoneno', ''), dept=request.params.get('department', '')) notice_no_access() raise if c.user and not data and not is_sysadmin(c.user): # #1799 Don't offer the registration form if already logged in return render('user/logout_first.html') data = data or {} errors = errors or {} error_summary = error_summary or {} d = {'data': data, 'errors': errors, 'error_summary': error_summary} c.is_sysadmin = is_sysadmin(c.user) c.form = render('user/new_user_form.html', extra_vars=d) return render('user/new.html')
def view_help(self): def _get_help_text(language): return pkg_resources.resource_string( __name__, '/'.join([ 'public', 'static', 'faq_{language}.md'.format(language=language) ])) try: # Try to load FAQ text for the user's language. faq_text = _get_help_text(c.language) except IOError: # Fall back to using English if no local language could be found. faq_text = _get_help_text(u'en') # Convert the markdown to HTML ... faq_html = render_markdown(faq_text.decode("utf-8"), allow_html=True) h = html.fromstring(faq_html) # Get every FAQ point header. for faq_section in h.xpath('.//h2'): details = ET.Element('details') summary = ET.Element('summary') # Place the new details tag where the FAQ section header used to # be. faq_section.addprevious(details) # Get all the text that follows the FAQ header. while True: next_node = faq_section.getnext() if next_node is None or next_node.tag in ('h1', 'h2'): break # ... and add it to the details. details.append(next_node) # Move the FAQ header to the top of the summary tag. summary.insert(0, faq_section) # Move the summary tag to the top of the details tag. details.insert(0, summary) # We don't actually want the FAQ headers to be headings, so strip # the tags and just leave the text. faq_section.drop_tag() # Get FAQ group header and set it as heading 2 to comply with # accessible heading ranks for faq_group in h.xpath('//h1'): faq_group.tag = 'h2' return render( 'help.html', extra_vars={ 'faq_html': html.tostring(h), # For use with the inline debugger. 'faq_text': faq_text })
def group_list_pages(id, group_type, group_dict=None): tk.c.pages_dict = tk.get_action('ckanext_pages_list')( data_dict={ 'org_id': tk.c.group_dict['id'] }) return tk.render('ckanext_pages/{}_page_list.html'.format(group_type), extra_vars={ 'group_type': group_type, 'group_dict': group_dict })
def register(self, data=None, errors=None, error_summary=None): '''GET to display a form for registering a new user. or POST the form data to actually do the user registration. The bulk of this code is pulled directly from ckan/controlllers/user.py ''' context = {'model': model, 'session': model.Session, 'user': c.user or c.author, 'schema': schema.user_new_form_schema(), 'save': 'save' in request.params} try: check_access('user_create', context) except NotAuthorized: abort(401, _('Unauthorized to create a user')) if context['save'] and not data: try: return self._save_new(context) except HTTPFound: # redirected after successful user create notify_ckan_user_create( email=request.params.get('email', ''), fullname=request.params.get('fullname', ''), username=request.params.get('name', ''), phoneno=request.params.get('phoneno', ''), dept=request.params.get('department', '')) notice_no_access() raise if c.user and not data and not is_sysadmin(c.user): # #1799 Don't offer the registration form if already logged in return render('user/logout_first.html') data = data or {} errors = errors or {} error_summary = error_summary or {} d = {'data': data, 'errors': errors, 'error_summary': error_summary} c.is_sysadmin = is_sysadmin(c.user) c.form = render('user/new_user_form.html', extra_vars=d) return render('user/new.html')
def get(self): types = tk.get_action("ctp_list_types")({"user": tk.c.user}, {}) extra_vars = { "form_snippet": "package/snippets/ctp_select_dataset_type_form.html", "pkg_dict": {}, "form_vars": { "package_types": [{"value": t, "text": tk._(t)} for t in types] }, } return tk.render("package/ctp_select_dataset_type.html", extra_vars)
def error(errors): return render('recombinant/resource_edit.html', extra_vars={ 'create_errors': errors, 'create_data': form_data, 'delete_errors': [], 'dataset': dataset, 'resource': res, 'organization': org, 'filters': {}, 'action': 'edit'})
def error(errors): return render('recombinant/resource_edit.html', extra_vars={ 'create_errors': errors, 'create_data': form_data, 'delete_errors': [], 'dataset': dataset, 'resource': res, 'organization': org, 'filters': {}, 'action': 'edit' })
def view_help(self): def _get_help_text(language): return pkg_resources.resource_string( __name__, '/'.join(['public', 'static', 'faq_{language}.md'.format( language=language )]) ) try: # Try to load FAQ text for the user's language. faq_text = _get_help_text(c.language) except IOError: # Fall back to using English if no local langauge could be found. faq_text = _get_help_text(u'en') # Convert the markdown to HTML ... faq_html = render_markdown(faq_text.decode("utf-8"), allow_html=True) h = html.fromstring(faq_html) # Get every FAQ point header. for faq_section in h.xpath('.//h2'): details = ET.Element('details') summary = ET.Element('summary') # Place the new details tag where the FAQ section header used to # be. faq_section.addprevious(details) # Get all the text that follows the FAQ header. while True: next_node = faq_section.getnext() if next_node is None or next_node.tag in ('h1', 'h2'): break # ... and add it to the details. details.append(next_node) # Move the FAQ header to the top of the summary tag. summary.insert(0, faq_section) # Move the summary tag to the top of the details tag. details.insert(0, summary) # We don't actaully want the FAQ headers to be headings, so strip # the tags and just leave the text. faq_section.drop_tag() return render('help.html', extra_vars={ 'faq_html': html.tostring(h), # For use with the inline debugger. 'faq_text': faq_text })
def reports(self, id=None): context = {'model': model, 'session': model.Session, 'user': c.user or c.author, 'auth_user_obj': c.userobj, 'for_view': True} data_dict = {'id': id, 'user_obj': c.userobj, 'include_datasets': True, 'include_num_followers': True} context['with_related'] = True self._setup_template_variables(context, data_dict) if c.is_myself: return render('user/reports.html') abort(403)
def pages_show(page=None, page_type='page'): tk.c.page_type = page_type if page.startswith('/'): page = page[1:] if not page: return pages_list_pages(page_type) _page = tk.get_action('ckanext_pages_show')(data_dict={ 'org_id': None, 'page': page }) if _page is None: return pages_list_pages(page_type) tk.c.page = _page _inject_views_into_page(_page) return tk.render('ckanext_pages/%s.html' % page_type)
def remove_showcase_admin(): ''' Remove a user from the Showcase Admin list. ''' context = { 'model': model, 'session': model.Session, 'user': c.user or c.author } try: tk.check_access('sysadmin', context, {}) except tk.NotAuthorized: return tk.abort(401, _('User not authorized to view page')) form_data = tk.request.form if tk.check_ckan_version( '2.9') else tk.request.params if tk.check_ckan_version(min_version='2.9.0'): admins_route = 'showcase_blueprint.admins' else: admins_route = 'showcase_admins' if 'cancel' in form_data: return tk.redirect_to(admins_route) user_id = tk.request.params['user'] if tk.request.method == 'POST' and user_id: user_id = tk.request.params['user'] try: tk.get_action('ckanext_showcase_admin_remove')(data_dict={ 'username': user_id }) except tk.NotAuthorized: return tk.abort(401, _('Unauthorized to perform that action')) except tk.ObjectNotFound: h.flash_error(_('The user is not a Showcase Admin')) else: h.flash_success(_('The user is no longer a Showcase Admin')) return tk.redirect_to(h.url_for(admins_route)) c.user_dict = tk.get_action('user_show')(data_dict={'id': user_id}) c.user_id = user_id return tk.render('admin/confirm_remove_showcase_admin.html')
def group_delete(id, group_type, page): _template_setup_group(id, group_type) if page.startswith('/'): page = page[1:] if 'cancel' in tk.request.params: if ckan_29_or_higher: return tk.redirect_to('pages.%s_edit' % group_type, id=tk.c.group_dict['name'], page=page) else: tk.redirect_to('%s_edit' % group_type, id=tk.c.group_dict['name'], page='/' + page) try: if tk.request.method == 'POST': action = 'ckanext_org_pages_delete' if group_type == 'organization' else 'ckanext_group_pages_delete' action = tk.get_action(action) action({}, {'org_id': tk.c.group_dict['id'], 'page': page}) if ckan_29_or_higher: endpoint = 'pages.{}_pages_index'.format(group_type) return tk.redirect_to(endpoint, id=id) else: tk.redirect_to('{}_pages_index'.format(group_type), id=id) else: tk.abort(404, _('Page Not Found')) except tk.NotAuthorized: tk.abort(401, _('Unauthorized to delete page')) except tk.ObjectNotFound: tk.abort(404, _('{} not found'.format(group_type.title()))) context = {'for_view': True} action = 'organization_show' if group_type == 'organization' else 'group_show' group_dict = tk.get_action(action)(context, {'id': id}) return tk.render('ckanext_pages/confirm_delete.html', { 'page': page, 'group_type': group_type, 'group_dict': group_dict })
def show_schema(dataset_id, resource_id): data_dict = {'id': resource_id} context = _get_context() try: check_access(RESOURCE_SHOW, context, data_dict) resource = get_action(RESOURCE_SHOW)(context, data_dict) schema_data = resource.get('schema') c.schema_data = json.dumps(schema_data, indent=2, sort_keys=True) return render('schema/show.html') except ObjectNotFound as e: log.warn(e) return abort(404, _('Resource %s not found') % resource_id) except NotAuthorized as e: log.warn(e) return abort( 403, _('You are not authorized to view the Data Scheme for the resource %s' % resource_id))
def validation(resource_id, id=None): try: validation = get_action(u'resource_validation_show')( { u'user': c.user }, { u'resource_id': resource_id }) resource = get_action(u'resource_show')({ u'user': c.user }, { u'id': resource_id }) package_id = resource[u'package_id'] if id and id != package_id: raise ObjectNotFound("Resource {} not found in package {}".format( resource_id, id)) dataset = get_action(u'package_show')( { u'user': c.user }, { u'id': id or resource[u'package_id'] }) # Needed for core resource templates c.package = c.pkg_dict = dataset c.resource = resource return render(u'validation/validation_read.html', extra_vars={ u'validation': validation, u'resource': resource, u'pkg_dict': dataset, u'dataset': dataset, }) except NotAuthorized: return abort(403, _(u'Unauthorized to read this validation report')) except ObjectNotFound: return abort(404, _(u'No validation report exists for this resource'))
def manage_showcase_admins(): context = { 'model': model, 'session': model.Session, 'user': c.user or c.author } try: tk.check_access('sysadmin', context, {}) except tk.NotAuthorized: return tk.abort(401, _('User not authorized to view page')) form_data = tk.request.form if tk.check_ckan_version( '2.9') else tk.request.params if tk.check_ckan_version(min_version='2.9.0'): admins_route = 'showcase_blueprint.admins' else: admins_route = 'showcase_admins' # We're trying to add a user to the showcase admins list. if tk.request.method == 'POST' and form_data['username']: username = form_data['username'] try: tk.get_action('ckanext_showcase_admin_add')(data_dict={ 'username': username }) except tk.NotAuthorized: abort(401, _('Unauthorized to perform that action')) except tk.ObjectNotFound: h.flash_error( _("User '{user_name}' not found.").format(user_name=username)) except tk.ValidationError as e: h.flash_notice(e.error_summary) else: h.flash_success(_("The user is now a Showcase Admin")) return tk.redirect_to(h.url_for(admins_route)) c.showcase_admins = tk.get_action('ckanext_showcase_admin_list')() return tk.render('admin/manage_showcase_admins.html')
def validation(self, resource_id): try: validation = get_action(u'resource_validation_show')( { u'user': c.user }, { u'resource_id': resource_id }) resource = get_action(u'resource_show')({ u'user': c.user }, { u'id': resource_id }) dataset = get_action(u'package_show')( { u'user': c.user }, { u'id': resource[u'package_id'] }) # Needed for core resource templates c.package = c.pkg_dict = dataset c.resource = resource return render(u'validation/validation_read.html', extra_vars={ u'validation': validation, u'resource': resource, u'pkg_dict': dataset, }) except NotAuthorized: abort(403, _(u'Unauthorized to read this validation report')) except ObjectNotFound: abort(404, _(u'No validation report exists for this resource'))
def update_pd_record(self, owner_org, resource_name, pk): pk = [url_part_unescape(p) for p in pk.split(',')] lc = LocalCKAN(username=c.user) try: chromo = h.recombinant_get_chromo(resource_name) rcomb = lc.action.recombinant_show( owner_org=owner_org, dataset_type=chromo['dataset_type']) [res ] = [r for r in rcomb['resources'] if r['name'] == resource_name] check_access('datastore_upsert', { 'user': c.user, 'auth_user_obj': c.userobj }, {'resource_id': res['id']}) except NotAuthorized: abort(403, _('Unauthorized')) choice_fields = { f['datastore_id']: [{ 'value': k, 'label': v } for (k, v) in f['choices']] for f in h.recombinant_choice_fields(resource_name) } pk_fields = aslist(chromo['datastore_primary_key']) pk_filter = dict(zip(pk_fields, pk)) records = lc.action.datastore_search(resource_id=res['id'], filters=pk_filter)['records'] if len(records) == 0: abort(404, _('Not found')) if len(records) > 1: abort(400, _('Multiple records found')) record = records[0] if request.method == 'POST': post_data = parse_params(request.POST, ignore_keys=['save'] + pk_fields) if 'cancel' in post_data: return redirect( h.url_for( controller= 'ckanext.recombinant.controller:UploadController', action='preview_table', resource_name=resource_name, owner_org=rcomb['owner_org'], )) data, err = clean_check_type_errors(post_data, chromo['fields'], pk_fields, choice_fields) # can't change pk fields for f_id in data: if f_id in pk_fields: data[f_id] = record[f_id] try: lc.action.datastore_upsert( resource_id=res['id'], #method='update', FIXME not raising ValidationErrors records=[{ k: None if k in err else v for (k, v) in data.items() }], dry_run=bool(err)) except ValidationError as ve: err = dict( { k: [_(e) for e in v] for (k, v) in ve.error_dict['records'][0].items() }, **err) if err: return render('recombinant/update_pd_record.html', extra_vars={ 'data': data, 'resource_name': resource_name, 'chromo_title': chromo['title'], 'choice_fields': choice_fields, 'pk_fields': pk_fields, 'owner_org': rcomb['owner_org'], 'errors': err, }) h.flash_notice(_(u'Record %s Updated') % u','.join(pk)) return redirect( h.url_for( controller= 'ckanext.recombinant.controller:UploadController', action='preview_table', resource_name=resource_name, owner_org=rcomb['owner_org'], )) data = {} for f in chromo['fields']: if not f.get('import_template_include', True): continue val = record[f['datastore_id']] data[f['datastore_id']] = val return render('recombinant/update_pd_record.html', extra_vars={ 'data': data, 'resource_name': resource_name, 'chromo_title': chromo['title'], 'choice_fields': choice_fields, 'pk_fields': pk_fields, 'owner_org': rcomb['owner_org'], 'errors': {}, })
def create_pd_record(self, owner_org, resource_name): lc = LocalCKAN(username=c.user) try: chromo = h.recombinant_get_chromo(resource_name) rcomb = lc.action.recombinant_show( owner_org=owner_org, dataset_type=chromo['dataset_type']) [res ] = [r for r in rcomb['resources'] if r['name'] == resource_name] check_access('datastore_upsert', { 'user': c.user, 'auth_user_obj': c.userobj }, {'resource_id': res['id']}) except NotAuthorized: return abort(403, _('Unauthorized')) choice_fields = { f['datastore_id']: [{ 'value': k, 'label': v } for (k, v) in f['choices']] for f in h.recombinant_choice_fields(resource_name) } pk_fields = aslist(chromo['datastore_primary_key']) if request.method == 'POST': post_data = parse_params(request.POST, ignore_keys=['save']) if 'cancel' in post_data: return redirect( h.url_for( controller= 'ckanext.recombinant.controller:UploadController', action='preview_table', resource_name=resource_name, owner_org=rcomb['owner_org'], )) data, err = clean_check_type_errors(post_data, chromo['fields'], pk_fields, choice_fields) try: lc.action.datastore_upsert(resource_id=res['id'], method='insert', records=[{ k: None if k in err else v for (k, v) in data.items() }], dry_run=bool(err)) except ValidationError as ve: if 'records' in ve.error_dict: err = dict( { k: [_(e) for e in v] for (k, v) in ve.error_dict['records'][0].items() }, **err) elif ve.error_dict.get('info', {}).get('pgcode', '') == '23505': err = dict( { k: [_("This record already exists")] for k in pk_fields }, **err) if err: return render('recombinant/create_pd_record.html', extra_vars={ 'data': data, 'resource_name': resource_name, 'chromo_title': chromo['title'], 'choice_fields': choice_fields, 'owner_org': rcomb['owner_org'], 'errors': err, }) h.flash_notice(_(u'Record Created')) return redirect( h.url_for( controller= 'ckanext.recombinant.controller:UploadController', action='preview_table', resource_name=resource_name, owner_org=rcomb['owner_org'], )) return render('recombinant/create_pd_record.html', extra_vars={ 'data': {}, 'resource_name': resource_name, 'chromo_title': chromo['title'], 'choice_fields': choice_fields, 'owner_org': rcomb['owner_org'], 'errors': {}, })
def fgpv_vpgf(self, pkg_id): return render('fgpv_vpgf/index.html', extra_vars={ 'pkg_id': pkg_id, })
def view_guidelines(self): return render('guidelines.html')
def registry_menu(self): return render("menu.html")
def get_deletion_reason_template(): return toolkit.render('package/snippets/deletion_reason.html')
def links(self): return render('home/quick_links.html')
def datarequests(org_id, metric): """Displays a list of data requests for the given organisation based on the desired metric""" request_helper = helpers.RequestHelper(request) report_type = request_helper.get_first_query_param('report_type', '') try: report_permission = _get_report_type_permission(report_type) reporting_helpers.check_user_org_access(org_id, report_permission) error = _valid_report_type(report_type) if error: return error get_validator('group_id_exists')(org_id, reporting_helpers.get_context()) start_date, end_date = reporting_helpers.get_report_date_range(request) utc_start_date, \ utc_end_date, \ utc_reply_expected_by_date, \ utc_expected_closure_date = reporting_helpers.get_utc_dates( start_date, end_date, COMMENT_NO_REPLY_MAX_DAYS, DATAREQUEST_OPEN_MAX_DAYS) circumstance = request_helper.get_first_query_param('circumstance') org = get_action('organization_show')({}, {'id': org_id}) data_dict = { 'org_id': org_id, 'org_title': org['title'], 'start_date': start_date, 'end_date': end_date, 'metric': metric, 'circumstance': circumstance, 'datarequest_open_max_days': DATAREQUEST_OPEN_MAX_DAYS, 'comment_no_reply_max_days': COMMENT_NO_REPLY_MAX_DAYS, 'user_dict': helpers.get_user().as_dict(), 'utc_start_date': utc_start_date, 'utc_end_date': utc_end_date, 'utc_reply_expected_by_date': utc_reply_expected_by_date, 'utc_expected_closure_date': utc_expected_closure_date, 'report_type': report_type } if metric == 'no-reply': datarequests_comments = get_action( 'datarequests_no_replies_after_x_days')({}, data_dict) # Action `datarequests_no_replies_after_x_days` returns a collection of comments with no replies # On this page we only need to display distinct datarequests containing those comments distinct_datarequests = [] comment_ids = {} for datarequest in datarequests_comments: if datarequest.datarequest_id in comment_ids: comment_ids[datarequest.datarequest_id].append( datarequest.comment_id) else: comment_ids[datarequest.datarequest_id] = [ datarequest.comment_id ] distinct_datarequests.append(datarequest) datarequests = distinct_datarequests data_dict.update({ 'total_comments': len(datarequests_comments), 'comment_ids': comment_ids }) elif metric == 'no-comments': datarequests = get_action( 'open_datarequests_no_comments_after_x_days')({}, data_dict) elif metric == 'open-max-days': datarequests = get_action('datarequests_open_after_x_days')( {}, data_dict) else: closing_circumstances = [ x['circumstance'] for x in reporting_helpers.get_closing_circumstance_list() ] if circumstance not in closing_circumstances: raise Invalid( _('Circumstance {0} is not valid'.format(circumstance))) datarequests = get_action('datarequests_for_circumstance')( {}, data_dict) data_dict['datarequests'] = datarequests return render('reporting/datarequests.html', extra_vars=data_dict) except Invalid as e: # Exception raised from get_validator('group_id_exists') log.warn(e) return abort(404, _('Organisation %s not found') % org_id) except NotAuthorized as e: # Exception raised from check_user_access log.warn(e) return abort( 403, _('You are not authorised to view the datarequest {0} report for organisation {1}' .format(report_type, org_id)))
def header_snippet(): return render('header.html')
def update_pd_record(self, owner_org, resource_name, pk): pk = [url_part_unescape(p) for p in pk.split(',')] lc = LocalCKAN(username=c.user) try: chromo = h.recombinant_get_chromo(resource_name) rcomb = lc.action.recombinant_show( owner_org=owner_org, dataset_type=chromo['dataset_type']) [res] = [r for r in rcomb['resources'] if r['name'] == resource_name] check_access( 'datastore_upsert', {'user': c.user, 'auth_user_obj': c.userobj}, {'resource_id': res['id']}) except NotAuthorized: abort(403, _('Unauthorized')) choice_fields = { f['datastore_id']: [ {'value': k, 'label': v} for (k, v) in f['choices']] for f in h.recombinant_choice_fields(resource_name)} pk_fields = aslist(chromo['datastore_primary_key']) pk_filter = dict(zip(pk_fields, pk)) records = lc.action.datastore_search( resource_id=res['id'], filters=pk_filter)['records'] if len(records) == 0: abort(404, _('Not found')) if len(records) > 1: abort(400, _('Multiple records found')) record = records[0] if request.method == 'POST': post_data = parse_params(request.POST, ignore_keys=['save'] + pk_fields) if 'cancel' in post_data: return redirect(h.url_for( controller='ckanext.recombinant.controller:UploadController', action='preview_table', resource_name=resource_name, owner_org=rcomb['owner_org'], )) data = {} for f in chromo['fields']: f_id = f['datastore_id'] if not f.get('import_template_include', True): continue if f_id in pk_fields: data[f_id] = record[f_id] else: val = post_data.get(f['datastore_id'], '') if isinstance(val, list): val = u','.join(val) val = canonicalize( val, f['datastore_type'], primary_key=False, choice_field=f_id in choice_fields) data[f['datastore_id']] = val try: lc.action.datastore_upsert( resource_id=res['id'], #method='update', FIXME not raising ValidationErrors records=[data]) except ValidationError as ve: err = { k: [_(e) for e in v] for (k, v) in ve.error_dict['records'][0].items()} return render('recombinant/update_pd_record.html', extra_vars={ 'data': data, 'resource_name': resource_name, 'chromo_title': chromo['title'], 'choice_fields': choice_fields, 'pk_fields': pk_fields, 'owner_org': rcomb['owner_org'], 'errors': err, }) h.flash_notice(_(u'Record %s Updated') % u','.join(pk) ) return redirect(h.url_for( controller='ckanext.recombinant.controller:UploadController', action='preview_table', resource_name=resource_name, owner_org=rcomb['owner_org'], )) data = {} for f in chromo['fields']: if not f.get('import_template_include', True): continue val = record[f['datastore_id']] data[f['datastore_id']] = val return render('recombinant/update_pd_record.html', extra_vars={ 'data': data, 'resource_name': resource_name, 'chromo_title': chromo['title'], 'choice_fields': choice_fields, 'pk_fields': pk_fields, 'owner_org': rcomb['owner_org'], 'errors': {}, })
# encoding: utf-8 from flask import Blueprint from ckantoolkit import render blueprint = Blueprint(u'publications_qld', __name__) blueprint.add_url_rule(u'/header.html', 'header', view_func=lambda: render('header.html')) blueprint.add_url_rule(u'/robots.txt', 'robots', view_func=lambda: render('robots.txt'))