def new(self, data=None, errors=None, error_summary=None): '''This is a modified version of the core user controller We have removed the lines redirecting the user the logout page if they are already logged in, this allows sysadmins to create users as we have disabled user registration unless they are sys admins''' context = {'model': model, 'session': model.Session, 'user': toolkit.c.user or toolkit.c.author, 'auth_user_obj': toolkit.c.userobj, 'schema': self._new_form_to_db_schema(), 'save': 'save' in toolkit.request.params} try: toolkit.check_access('user_create', context) except toolkit.NotAuthorized: toolkit.abort(401, toolkit._('Unauthorized to create a user')) if context['save'] and not data: return self._save_new(context) data = data or {} errors = errors or {} error_summary = error_summary or {} vars = {'data': data, 'errors': errors, 'error_summary': error_summary} toolkit.c.is_sysadmin = new_authz.is_sysadmin(toolkit.c.user) toolkit.c.form = toolkit.render(self.new_user_form, extra_vars=vars) return toolkit.render('user/new.html')
def admin(self): # Redirect to /news if not authorized: try: context = {'user': c.user} toolkit.check_access('blog_admin', context) except toolkit.NotAuthorized: h.redirect_to('/news') c.title = '' c.content = '' if request.method == 'POST': try: title, content = _validate_blog_post(request.POST) except ValidationError as err: return toolkit.render('blog/admin.html', extra_vars={'data_dict': request.POST, 'error': err.args}) # We assume nothing will go wrong here, since the data has been # validated. from ckanext.sweden.blog.model.post import Post newPost = Post(title, content, c.userobj.id) model.Session.add(newPost) model.Session.commit() flash_notice(toolkit._("Your blog post has been saved!")) controller = 'ckanext.sweden.blog.controllers.blog:BlogController' h.redirect_to(controller=controller, action='admin_index') return toolkit.render('blog/admin.html', extra_vars={'data_dict': {}, 'error': ''})
def edit(self, organization_name, inventory_entry_id, data=None, errors=None, error_summary=None): context = {'model': model, 'session': model.Session, 'user': c.user or c.author, 'organization_name': c.organization_name, 'save': 'save' in request.params, 'schema': default_inventory_entry_schema_create()} if context['save'] and not data: return self._save_edit(inventory_entry_id, context) try: old_data = get_action('inventory_entry_show')( context, {'id': inventory_entry_id}) data = data or old_data except NotFound: abort(404, _('Inventory Entry not found')) except NotAuthorized: abort(401, _('Unauthorized to read inventory entry')) data = data or {} errors = errors or {} error_summary = error_summary or {} vars = {'data': data, 'errors': errors, 'error_summary': error_summary, 'action': 'edit'} c.form = render('inventory/entry/inventory_entry_form.html', extra_vars=vars) return render('inventory/entry/edit.html')
def index(self): try: reports = t.get_action('report_list')({}, {}) except t.NotAuthorized: t.abort(401) return t.render('report/index.html', extra_vars={'reports': reports})
def index(self): from ckanext.sweden.blog.model.post import Post c.posts = model.Session.query(Post).\ filter(Post.visible == True).order_by('created desc') return toolkit.render('blog/index.html')
def authorize(self): context = self._get_context() try: tk.check_access('oauth2provider_token_create', context) except tk.NotAuthorized: return tk.abort(401) client_id = self._get_required_param('client_id') response_type = tk.request.params.get('redirect_uri', 'code') scopes = self._get_required_param('scope').split(' ') redirect_uri = tk.request.params.get('redirect_uri', '') state = tk.request.params.get('state', '') if state: session['oauth2provider_state'] = state session.save() client = Client.get(client_id=client_id) data = { 'client_name': client.name, 'client_id': client.client_id, 'response_type': response_type, 'redirect_uri': redirect_uri, 'scopes': scopes, } vars = {'data': data, 'action': 'authorize'} return tk.render('ckanext/oauth2provider/authorize.html', extra_vars=vars)
def new(self, data=None, errors=None, error_summary=None): context = self._get_context() try: tk.check_access('oauth2provider_client_create', context) except tk.NotAuthorized: tk.abort(401, _('Unauthorized to create an oauth2 client')) if tk.request.method == 'POST': data = dict(tk.request.params) try: data['user_id'] = model.User.by_name(data['username']).id except AttributeError: data['user_id'] = None client = tk.get_action('oauth2provider_client_create')(context, data) return tk.redirect_to('oauth2provider_client_list') data = data or {} errors = errors or {} error_summary = error_summary or {} vars = {'data': data, 'errors': errors, 'error_summary': error_summary, 'action': 'new'} return tk.render('ckanext/oauth2provider/client/new.html', extra_vars=vars)
def qa_openness_stars_dataset_html(dataset): qa = dataset.get('qa') if not qa: return tk.literal('<!-- No qa info for this dataset -->') extra_vars = copy.deepcopy(qa) return tk.literal( tk.render('qa/openness_stars_brief.html', extra_vars=extra_vars))
def broken_links_by_organization(self): report = toolkit.get_action( "ckanext_deadoralive_broken_links_by_organization")(data_dict={}) extra_vars = {"organizations": report} return toolkit.render("broken_links_by_organization.html", extra_vars=extra_vars)
def admin_edit(self, title): data_dict = dict(request.POST) # Redirect to /news if not authorized: try: context = {'user': c.user} toolkit.check_access('blog_admin', context) except toolkit.NotAuthorized: h.redirect_to('/news') try: from ckanext.sweden.blog.model.post import Post c.post = model.Session.query(Post).\ filter(Post.url == title).\ filter(Post.visible == True).\ one() if 'title' not in data_dict: data_dict['title'] = c.post.title if 'content' not in data_dict: data_dict['content'] = c.post.content except NoResultFound: abort(404) if request.method == 'POST': try: title, content = _validate_blog_post( request.POST, original_title=request.urlvars.get('title')) except ValidationError as err: return toolkit.render( 'blog/admin_edit.html', extra_vars={'data_dict': data_dict, 'error': err.args}) c.post.title = title c.post.content = content model.Session.commit() flash_notice(toolkit._("Your blog post has been updated!")) controller = 'ckanext.sweden.blog.controllers.blog:BlogController' h.redirect_to(controller=controller, action='admin_index') return toolkit.render( 'blog/admin_edit.html', extra_vars={'data_dict': data_dict, 'errors': ''})
def read(self, organization_name, inventory_entry_id): context = {'model': model, 'session': model.Session, 'user': c.user or c.author} c.entries = get_action('inventory_entry_list_items')( context, {'inventory_entry_id': inventory_entry_id}) return render('inventory/entry/read.html', extra_vars={'group_type': self._ensure_controller_matches_group_type(organization_name)})
def index(self): context = { 'model': model, 'user': toolkit.c.user } toolkit.c.taxonomies = logic.get_action('taxonomy_list')(context, {}) return toolkit.render('ckanext/taxonomy/index.html')
def archiver_is_resource_broken_html(resource): archival = resource.get('archiver') if not archival: return tk.literal('<!-- No archival info for this resource -->') extra_vars = {'resource': resource} extra_vars.update(archival) return tk.literal( tk.render('archiver/is_resource_broken.html', extra_vars=extra_vars))
def index(self, organization_name): context = {'model': model, 'user': c.user, 'session': model.Session} inventory_entries = get_action('inventory_entry_list')( context, {'name': organization_name}) c.inventory_entries = inventory_entries return render('inventory/entry/index.html', extra_vars={'group_type': self._ensure_controller_matches_group_type(organization_name)})
def feed(self): from ckanext.sweden.blog.model.post import Post c.posts = model.Session.query(Post).\ filter(Post.visible == True).order_by('created desc') response.headers['Content-Type'] = 'application/rss+xml' response.charset = 'utf-8' return toolkit.render('blog/rss.html')
def resources(self, id=None): try: c.include_sub_publishers = t.asbool(t.request.params.get('include_sub_publishers') or False) except ValueError: abort(400, 'include_sub_publishers parameter value must be boolean') c.publisher_name = id or 'department-for-culture-media-and-sport' c.query = reports.organisation_resources c.data = c.query(organisation_name=c.publisher_name, include_sub_organisations=c.include_sub_publishers) return t.render('reports/resources.html')
def qa_openness_stars_dataset_html(dataset): qa = dataset.get('qa') if not qa: return tk.literal('<!-- No qa info for this dataset -->') if not isinstance(qa, dict): return tk.literal('<!-- QA info was of the wrong type -->') extra_vars = copy.deepcopy(qa) return tk.literal( tk.render('qa/openness_stars_brief.html', extra_vars=extra_vars))
def bulk_new(self, data=None, errors=None, error_summary=None): context = {'model': model, 'session': model.Session, 'user': c.user or c.author, 'organization_name': c.organization_name, 'save': 'save' in request.params , 'schema': default_inventory_entry_schema_create()} if context['save'] and not data: return self._save_new_bulk(context) data = data or {} errors = errors or {} error_summary = error_summary or {} vars = {'data': data, 'errors': errors, 'error_summary': error_summary} c.form = render('inventory/entry/snippets/inventory_entry_bulk_form.html', extra_vars=vars) return render('inventory/entry/bulk_new.html')
def geo_resource_form(context, data): """ Returns the form for the OFI Manager create/edit of OFI resources. """ file_formats = toolkit.get_action(u'file_formats')({}, {}) data.update(file_formats=file_formats) return toolkit.render('ofi/snippets/geo_resource_form.html', extra_vars=data)
def geo_resource_form(context, data): """ Returns the form for the OFI Manager create/edit of OFI resource. """ if toolkit.asbool(ofi_config.get('convert_to_single_res', False)): file_formats = toolkit.get_action(u'file_formats')({}, {}) data.update(file_formats=file_formats) return toolkit.render('ofi/snippets/geo_resource_form.html', extra_vars=data)
def create_users(self): # extra_vars has to contain 'data' or the template will crash. # Replace data with a non-empty dict to pre-fill the form fields. extra_vars = {'data': {}} if toolkit.request.method == 'POST': params = dict(toolkit.request.params) try: if params['organisation']: organisation_id = toolkit.get_action('organization_show')( context={}, data_dict={'id': params['organisation']})['id'] except toolkit.ObjectNotFound: helpers.flash_error('Organization {} not found'.format( params['organisation'])) return toolkit.render('create_users/create_users.html', extra_vars=extra_vars) confirm = params.pop('confirm-password') if confirm != params['Password']: helpers.flash_error('passwords do not match') return toolkit.render('create_users/create_users.html', extra_vars=extra_vars) context = {'model': model, 'session': model.Session} data_dict = { 'UserName': params['Username'], 'Password': params['Password'], 'IsRegisteredUser': True, 'Email': params['Email'], 'FirstName': params['First Name'], 'LastName': params['Last Name'], 'DisplayName': params['Display Name'], 'About': params['About'], 'OrganisationId': params.get('organisation'), } try: request = toolkit.get_action('ec_user_create')(context, data_dict) except ECAPINotFound, e: helpers.flash_error('Error CTPEC platform returned an error: {}'.format(str(e))) return toolkit.render('create_users/create_users.html', extra_vars=extra_vars) except toolkit.ValidationError, e: helpers.flash_error('Error validating fields {}'.format(str(e))) return toolkit.render('create_users/create_users.html', extra_vars=extra_vars)
def _delete_attr(self, package_id, resource_id): '''Delete an attribute from a resource schema field. This is called when the user submits the resource edit form by clicking on one of the delete buttons next to the attributes. The attribute is deleted from the field, and the form is re-rendered without the deleted attribute. Note this doesn't actually delete the sttribute from the database, that doesn't happen until the user clicks Save. ''' data = _get_data() schema_fields = _extract_fields_from_data(data) # Find the index and key of the attribute we're trying to delete. _, index, key = request.params['delete-attr'].split('-', 2) index = int(index) # Delete the attribute. field = schema_fields[index] assert key in field del field[key] # The user may have edited the values of some existing field attributes # as well, so validate the field attributes and show any errors to the # user. # FIXME: This validates every resource schema field, but we only need # to validate the field for the currently selected column. schema_errors = _call_actions(schema_fields, resource_id, validate_only=True) # Setup template context variables. toolkit.c.pkg_dict = toolkit.get_action('package_show')( {'for_edit': True}, {'id': package_id}) toolkit.c.resource = toolkit.get_action('resource_show')( {'for_edit': True}, {'id': resource_id}) toolkit.c.form_action = helpers.url_for(controller='package', action='resource_edit', resource_id=resource_id, id=package_id) # Setup template extra_vars. extra_vars = { 'schema_fields': schema_fields, 'schema_errors': schema_errors, 'selected_column': index, 'data': _delete_form_logic_keys(data), 'errors': {}, 'error_summary': {}, 'action': 'new', } return toolkit.render('package/resource_edit.html', extra_vars=extra_vars)
def index(self, data=None, errors=None, error_summary=None): context = self._get_context() data = data or {} errors = errors or {} error_summary = error_summary or {} vars = {'data': data, 'errors': errors, 'error_summary': error_summary, 'action': 'index'} return tk.render('ckanext/oauth2provider/token/index.html')
def broken_links_by_email(self): try: report = toolkit.get_action( "ckanext_deadoralive_broken_links_by_email")(data_dict={}) except toolkit.NotAuthorized: toolkit.abort(401) extra_vars = {"report": report} return toolkit.render("broken_links_by_email.html", extra_vars=extra_vars)
def displayAgreement(self, id, resource_id): try: package_dict = toolkit.get_action('package_show')({}, {'id': id}) except NotFound: abort(404, 'Dataset not found') try: resource_dict = toolkit.get_action('resource_show')({}, {'id': resource_id}) except NotFound: abort(404, 'Resource not found') vars = {'package': package_dict, 'resource': resource_dict} return toolkit.render('ilripages/nda.html',extra_vars=vars)
def all_reported_issues(self, organization_id): '''show all issues over max_strikes and are not moderated''' try: issues, organization = all_reported_issues(organization_id) extra_vars = { 'issues': issues.get('results', []), 'organization': organization, } return toolkit.render("issues/moderation.html", extra_vars=extra_vars) except toolkit.ObjectNotFound: toolkit.abort(404, toolkit._('Organization not found'))
def qa_openness_stars_resource_html(resource): qa = resource.get('qa') if not qa: return tk.literal('<!-- No qa info for this resource -->') # Take a copy of the qa dict, because weirdly the renderer appears to add # keys to it like _ and app_globals. This is bad because when it comes to # render the debug in the footer those extra keys take about 30s to render, # for some reason. extra_vars = copy.deepcopy(qa) return tk.literal( tk.render('qa/openness_stars.html', extra_vars=extra_vars))
def _re_render_resource_edit_page(self, package_id, resource_id, data=None, errors=None, error_summary=None, schema_errors=None): '''Re-render the resource edit page, sending back the form data that the user submitted. This happens when the user clicks a button on the resource edit page that causes the page to be reloaded (e.g. clicking on one of the columns in the CSV preview). The page is re-rendered with any form values that the user had entered intact. ''' # Get the form data that the user submitted. data = data or _get_data() schema_fields = _extract_fields_from_data(data) # FIXME: This actually validates the data in all of the resource schema # fields, when we only really need to validate the field for the # currently selected column. schema_errors = schema_errors or _call_actions(schema_fields, resource_id, validate_only=True) # Setup template context variables. toolkit.c.pkg_dict = toolkit.get_action('package_show')( {'for_edit': True}, {'id': package_id}) toolkit.c.resource = toolkit.get_action('resource_show')( {'for_edit': True}, {'id': resource_id}) toolkit.c.form_action = helpers.url_for(controller='package', action='resource_edit', resource_id=resource_id, id=package_id) # We show the first column whose schema fields have any validation # errors, or if no columns have validation errors then we show the # column that the user clicked on. selected_column = _first_column_with_errors(schema_errors) if selected_column is None: selected_column = _selected_column() # Setup template extra_vars. extra_vars = { 'schema_fields': schema_fields, 'schema_errors': schema_errors, 'selected_column': selected_column, 'data': _delete_form_logic_keys(data) or toolkit.c.resource, 'errors': errors or {}, 'error_summary': error_summary or {}, 'action': 'new', } return toolkit.render('package/resource_edit.html', extra_vars=extra_vars)
def config(self): #在「/ckan-admin/dsp-integrate」頁面render之前,先確認使用者是否為sysadmin。 context = {'model': c.model, 'user': c.user, 'auth_user_obj': c.userobj} try: toolkit.check_access('sysadmin', context, {}) except toolkit.NotAuthorized: toolkit.abort(401, _('Need to be system administrator to administer') ) c.revision_change_state_allowed = True return toolkit.render('admin/sync.html')
def admin_index(self): # Redirect to /news if not authorized: try: context = {'user': c.user} toolkit.check_access('blog_admin', context) except toolkit.NotAuthorized: h.redirect_to('/news') from ckanext.sweden.blog.model.post import Post c.posts = model.Session.query(Post).\ filter(Post.visible == True).order_by('created desc') return toolkit.render('blog/admin_list.html')
def view(slug=u''): return toolkit.render(u'search/search.html', extra_vars={u'slug': slug})