def post(self, group_type, is_organization, id=None): set_org(is_organization) context = self._prepare(id, is_organization) try: data_dict = clean_dict( dict_fns.unflatten(tuplize_dict(parse_params(request.form)))) data_dict.update(clean_dict( dict_fns.unflatten(tuplize_dict(parse_params(request.files))) )) context['message'] = data_dict.get(u'log_message', u'') data_dict['id'] = context['id'] context['allow_partial_update'] = True group = _action(u'group_update')(context, data_dict) if id != group['name']: _force_reindex(group) except (NotFound, NotAuthorized) as e: base.abort(404, _(u'Group not found')) except dict_fns.DataError: base.abort(400, _(u'Integrity Error')) except ValidationError as e: errors = e.error_dict error_summary = e.error_summary return self.get(id, group_type, is_organization, data_dict, errors, error_summary) return h.redirect_to(group[u'type'] + u'.read', id=group[u'name'])
def post(self, group_type, is_organization): set_org(is_organization) context = self._prepare() try: data_dict = clean_dict( dict_fns.unflatten(tuplize_dict(parse_params(request.form)))) data_dict.update(clean_dict( dict_fns.unflatten(tuplize_dict(parse_params(request.files))) )) data_dict['type'] = group_type or u'group' context['message'] = data_dict.get(u'log_message', u'') data_dict['users'] = [{u'name': g.user, u'capacity': u'admin'}] group = _action(u'group_create')(context, data_dict) except (NotFound, NotAuthorized) as e: base.abort(404, _(u'Group not found')) except dict_fns.DataError: base.abort(400, _(u'Integrity Error')) except ValidationError as e: errors = e.error_dict error_summary = e.error_summary return self.get(group_type, is_organization, data_dict, errors, error_summary) return h.redirect_to(group['type'] + u'.read', id=group['name'])
def post(self, package_type, id, resource_id): context = self._prepare(id) data = clean_dict( dict_fns.unflatten(tuplize_dict(parse_params(request.form))) ) data.update(clean_dict( dict_fns.unflatten(tuplize_dict(parse_params(request.files))) )) # we don't want to include save as it is part of the form del data[u'save'] data[u'package_id'] = id try: if resource_id: data[u'id'] = resource_id get_action(u'resource_update')(context, data) else: get_action(u'resource_create')(context, data) except ValidationError as e: errors = e.error_dict error_summary = e.error_summary return self.get( package_type, id, resource_id, data, errors, error_summary ) except NotAuthorized: return base.abort(403, _(u'Unauthorized to edit this resource')) return h.redirect_to(u'resource.read', id=id, resource_id=resource_id)
def _save_new(self, context): try: data_dict = logic.clean_dict(df.unflatten( logic.tuplize_dict(logic.parse_params(request.params)))) context['message'] = data_dict.get('log_message', '') captcha.check_recaptcha(request) # Extra: Create username from email email = data_dict.get('email', '') email_user = email.split('@')[0] name = re.sub('[^a-z0-9_\-]', '_', email_user) # Append num so it becames unique (search inside deleted as well) session = context['session'] user_names = model.User.search(name, session.query(model.User)).all() user_names = map(lambda u: u.name, user_names) while name in user_names: m = re.match('^(.*?)(\d+)$', name) if m: name = m.group(1) + str(int(m.group(2)) + 1) else: name = name + '2' data_dict['name'] = name user = get_action('user_create')(context, data_dict) except NotAuthorized: abort(401, _('Unauthorized to create user %s') % '') except NotFound, e: abort(404, _('User not found'))
def config(self): items = self._get_config_form_items() data = request.POST if 'save' in data: try: # really? data_dict = logic.clean_dict( dict_fns.unflatten( logic.tuplize_dict( logic.parse_params( request.POST, ignore_keys=CACHE_PARAMETERS)))) del data_dict['save'] data = logic.get_action('config_option_update')( {'user': c.user}, data_dict) except logic.ValidationError, e: errors = e.error_dict error_summary = e.error_summary vars = {'data': data, 'errors': errors, 'error_summary': error_summary, 'form_items': items} return base.render('admin/config.html', extra_vars=vars) h.redirect_to(controller='admin', action='config')
def new_resource(self, id, data=None, errors=None, error_summary=None): ''' Before creating a resource into CKAN, we request WSO2 ESB to add a subscription to the related Topic. ''' if request.method == 'POST' and not data: # Recogniced new resource form POST, extract variables. postdata = data or clean_dict(unflatten(tuplize_dict(parse_params(request.POST)))) if 'save' in postdata and 'url' in postdata: package_data = get_action('package_show')({'model': model, 'session': model.Session, 'user': c.user or c.author, 'auth_user_obj': c.userobj}, {'id': id}) topic = getTopicFromPackageData(package_data) # Add a new subscription for the topic named after the dataset, pointing to the URL given. brokerclient = getBrokerClient() try: result = brokerclient.subscribe(topic, postdata['url']) except Py4JJavaError, e: # Errors are propagated to the CKAN controller below to prevent new resource creation. error_message = str(e) if 'Error While Subscribing :Cannot initialize URI with empty parameters.' in error_message: error_message = _('Error While Subscribing: Cannot initialize URI with empty parameters.') if 'org.apache.axis2.AxisFault: Connection refused' in error_message: error_message = _('Error While Subscribing: Cannot connect to WSO2 ESB.') if errors and isinstance(errors, dict): errors.update({ 'error': error_message }) else: errors = { 'error': error_message } data = postdata error_summary = { _('WSO2 ESB'): error_message }
def new(self, data=None, errors=None, error_summary=None): """ This overrides the PackageController method to redirect to the show dataset view without having to go to the add data view. The difference is the inclusion of what the dataset type is. If the save param finish is included or any exceptions happen it will either abort or call the super new method. """ log.info('ckanext-bcgov.controllers.package:EDCPackageController.new overriding ckan\'s method') save_action = toolkit.request.params.get('save') data_dict = logic.clean_dict( dict_fns.unflatten( logic.tuplize_dict( logic.parse_params(toolkit.request.POST, ignore_keys=CACHE_PARAMETERS)))) log.debug('EDCPackageController data from toolkit.request.POST %s' % data_dict) is_an_update = data_dict.get('pkg_name', False) if data and 'type' in data and data['type']: package_type = data['type'] else: package_type = self._guess_package_type(True) if save_action == 'finish' and not is_an_update and package_type == 'Geographic': return self._new_dataset_only(package_type, data_dict, errors, error_summary) elif save_action == 'add_data' and not is_an_update and package_type == 'Geographic': return self._new_dataset_only(package_type, data_dict, errors, error_summary, 'resources') else: return super(EDCPackageController, self).new(data, errors, error_summary)
def test_just_additional_resources(self): """ Tests with just one set of additional resources defined. """ data = { 'resources': [ {'description': 'Description 1', 'url': 'Url1', 'resource_type': 'documentation'}, {'description': 'Description 2', 'url': 'Url2', 'resource_type': 'documentation'} ] } flattened_data = flatten_dict(data) ignored = {} errors = dict([(k, []) for k in flattened_data.keys()]) unmerge_resources(('__after',), flattened_data, errors, ignored) result_data = unflatten(flattened_data) expected_data = { 'additional_resources': [ {'description': 'Description 1', 'url': 'Url1', 'resource_type': 'documentation'}, {'description': 'Description 2', 'url': 'Url2', 'resource_type': 'documentation'} ] } expected_data.update(data) assert_equal(result_data, expected_data)
def _save_edit(self, id, context): try: data_dict = clean_dict(unflatten( tuplize_dict(parse_params(request.POST)))) self._check_data_dict(data_dict) context['message'] = data_dict.get('log_message', '') if not context['moderated']: context['pending'] = False data_dict['id'] = id pkg = get_action('package_update')(context, data_dict) if request.params.get('save', '') == 'Approve': update.make_latest_pending_package_active(context, data_dict) c.pkg = context['package'] c.pkg_dict = pkg if context['preview']: c.is_preview = True PackageSaver().render_package(pkg, context) c.preview = render('package/read_core.html') return self.edit(id, data_dict) self._form_save_redirect(pkg['name'], 'edit') except NotAuthorized: abort(401, _('Unauthorized to read package %s') % id) except NotFound, e: abort(404, _('Package not found'))
def _compare_datadicts(self, original, output): ''' Compare a CKAN generated datadict to original datadict. Returns True if identical, otherwise throws an exception with useful output of differences. :param original: original datadict :param output: a datadict received from CKAN API ''' def convert_tags(tag_string): ''' Convert tag_string to tags dict. Copy-paste from ckan.tests.legacy.logic.test_tag_vocab.py. ''' key = 'vocab_tags' data = {key: tag_string} errors = [] context = {'model': model, 'session': model.Session} kata_tag_string_convert(key, data, errors, context) del data[key] return data data_dict = copy.deepcopy(original) # name (data pid), title and notes are generated so they shouldn't match data_dict.pop('name', None) data_dict.pop('title', None) data_dict.pop('notes', None) # lang* fields are converted to translation JSON strings and # after that they are not needed anymore data_dict.pop('langtitle', None) data_dict.pop('langnotes', None) # Terms of usage acceptance is checked but not saved data_dict.pop('accept-terms', None) # Create tags from original dataset's tag_string if not data_dict.get('tags'): data_dict['tags'] = df.unflatten(convert_tags(data_dict.get('tag_string'))).get('tags') data_dict.pop('tag_string', None) print data_dict['tags'] for tag_dict in output.get('tags'): # These are not provided from kata_tag_string_convert, so remove them tag_dict.pop('display_name') tag_dict.pop('id') tag_dict.pop('state') tag_dict.pop('vocabulary_id') # Remove xpaths because xpath-json converter not yet implemented data_dict.pop('xpaths', None) # Remove all values that are not present in the original data_dict output = dict((k, v) for k, v in output.items() if k in data_dict.keys()) # Take out automatically added distributor (CKAN user) output['agent'] = filter(lambda x: x.get('name') not in ['testsysadmin', 'tester'], output['agent']) testfixtures.compare(output, data_dict) return True
def handle_submit(self, id): data = clean_dict(dict_fns.unflatten(tuplize_dict(parse_params( request.params)))) data['dataset_url'] = toolkit.url_for( controller='package', action='read', id=id, qualified=True ) package = get_action('package_show')(None, {'id': id}) self.fail_if_private(package, data['dataset_url']) # Comma separated config var to_addrs = config['ckanext.ands.support_emails'].split(',') subject = 'DataPortal Support: Request to publish dataset' body = base.render( 'package/doi_email.text', extra_vars=data) for email in to_addrs: mail_recipient('Dataportal support', email, subject, body) data['package_id'] = package['id'] data['user_id'] = c.userobj.id doi_request = DoiRequest(**data) Session.add(doi_request) Session.commit() h.flash_success("DOI Request sent") return toolkit.redirect_to(data['dataset_url'])
def _save_new(self, context, group_type=None): try: data_dict = clean_dict(dict_fns.unflatten( tuplize_dict(parse_params(request.params)))) data_dict['type'] = group_type or 'group' context['message'] = data_dict.get('log_message', '') data_dict['users'] = [{'name': c.user, 'capacity': 'admin'}] group = self._action('group_create')(context, data_dict) log.info('::::: Persisting localised metadata locale :::::') lang = get_lang()[0] session = model.Session try: session.add_all([ GroupMultilang(group_id=group.get('id'), name=group.get('name'), field='title', lang=lang, text=group.get('title')), GroupMultilang(group_id=group.get('id'), name=group.get('name'), field='description', lang=lang, text=group.get('description')), ]) session.commit() except Exception, e: # on rollback, the same closure of state # as that of commit proceeds. session.rollback() log.error('Exception occurred while persisting DB objects: %s', e) raise # Redirect to the appropriate _read route for the type of group h.redirect_to(group['type'] + '_read', id=group['name'])
def convert_to_extras_custom(key, data, errors, context): # get the current number of extras field unflattened = df.unflatten(data) if("extras" in unflattened): extras_count = len(unflattened['extras']) else: extras_count = 0 data.update({ ('extras', (extras_count), 'id') : [tk.get_validator('ignore')], ('extras', (extras_count), 'revision_timestamp') : [tk.get_validator('ignore')], ('extras', (extras_count), 'state') : [tk.get_validator('ignore')], ('extras', (extras_count), 'deleted') : [], # this needs to be blank so the fields won't be deleted }) if key[-1] == "pkg_creator": data.update({ ('extras', (extras_count), 'key') : key[-1], ('extras', (extras_count), 'value') : c.userobj.id }) elif key[-1] == "pkg_invisible": if data[key] != ( "True" or "False" ): data[key] = "False" data.update({ ('extras', (extras_count), 'key') : key[-1], ('extras', (extras_count), 'value') : data[key] }) pass
def _add_or_reply(self, dataset_id): """ Allows the user to add a comment to an existing dataset """ context = {'model': model, 'user': c.user} # Auth check to make sure the user can see this package data_dict = {'id': dataset_id} check_access('package_show', context, data_dict) try: c.pkg_dict = get_action('package_show')(context, {'id': dataset_id}) c.pkg = context['package'] except: abort(403) if request.method == 'POST': data_dict = clean_dict(unflatten( tuplize_dict(parse_params(request.POST)))) data_dict['parent_id'] = c.parent.id if c.parent else None data_dict['url'] = '/dataset/%s' % c.pkg.name success = False try: res = get_action('comment_create')(context, data_dict) success = True except ValidationError, ve: log.debug(ve) except Exception, e: log.debug(e) abort(403)
def member_new(self, id): context = {'model': model, 'session': model.Session, 'user': c.user or c.author} #self._check_access('group_delete', context, {'id': id}) try: if request.method == 'POST': data_dict = clean_dict(dict_fns.unflatten( tuplize_dict(parse_params(request.params)))) data_dict['id'] = id c.group_dict = self._action('group_member_create')(context, data_dict) self._redirect_to(controller='group', action='members', id=id) else: user = request.params.get('user') if user: c.user_dict = get_action('user_show')(context, {'id': user}) c.user_role = new_authz.users_role_for_group_or_org(id, user) or 'member' else: c.user_role = 'member' c.group_dict = self._action('group_show')(context, {'id': id}) c.roles = self._action('member_roles_list')(context, {}) except NotAuthorized: abort(401, _('Unauthorized to add member to group %s') % '') except NotFound: abort(404, _('Group not found')) except ValidationError, e: h.flash_error(e.error_summary)
def _save_new(self, context, package_type=None): ''' The showcase is created then redirects to the manage_dataset page to associated packages with the new showcase. ''' data_dict = clean_dict(dict_fns.unflatten( tuplize_dict(parse_params(request.POST)))) data_dict['type'] = package_type context['message'] = data_dict.get('log_message', '') try: pkg_dict = get_action('ckanext_showcase_create')(context, data_dict) except ValidationError as e: errors = e.error_dict error_summary = e.error_summary data_dict['state'] = 'none' return self.new(data_dict, errors, error_summary) # redirect to manage datasets url = h.url_for( controller='ckanext.showcase.controller:ShowcaseController', action='manage_datasets', id=pkg_dict['name']) redirect(url)
def _save_edit(self, name_or_id, context, package_type=None): from ckan.lib.search import SearchIndexError log.debug('Package save request name: %s POST: %r', name_or_id, request.POST) try: data_dict = clean_dict(dict_fns.unflatten( tuplize_dict(parse_params(request.POST)))) if '_ckan_phase' in data_dict: # we allow partial updates to not destroy existing resources context['allow_partial_update'] = True data_dict['tags'] = self._tag_string_to_list( data_dict['tag_string']) del data_dict['_ckan_phase'] del data_dict['save'] context['message'] = data_dict.get('log_message', '') if not context['moderated']: context['pending'] = False data_dict['id'] = name_or_id #log.fatal("-----------> data dict: % s" % data_dict) pkg = get_action('package_update')(context, data_dict) if request.params.get('save', '') == 'Approve': get_action('make_latest_pending_package_active')( context, data_dict) c.pkg = context['package'] c.pkg_dict = pkg self._form_save_redirect(pkg['name'], 'edit', package_type=package_type) except NotAuthorized: abort(401, _('Unauthorized to read package %s') % id) except NotFound, e: abort(404, _('Dataset not found'))
def _add_or_reply(self, dataset_name): """ Allows the user to add a comment to an existing dataset """ context = {'model': model, 'user': c.user} # Auth check to make sure the user can see this package ctx = context ctx['id'] = dataset_name check_access('package_show', ctx, {'id': dataset_name}) try: c.pkg_dict = get_action('package_show')(context, {'id': dataset_name}) c.pkg = context['package'] except: abort(401, _('Unauthorized')) if not c.user: abort(401, _('Unauthorized')) errors = {} if request.method == 'POST': data_dict = clean_dict(unflatten( tuplize_dict(parse_params(request.POST)))) data_dict['parent_id'] = c.parent.id if c.parent else None data_dict['url'] = '/dataset/%s' % c.pkg.name data_dict['comment'] = data_dict['comment'][:5000] success = False try: res = get_action('comment_create')(context, data_dict) success = True except ValidationError, ve: errors = ve.error_dict except Exception, e: abort(401, _('Unauthorized'))
def resource_edit(self, id, resource_id, data=None, errors=None, error_summary=None): if request.method == 'POST' and not data: data = data or clean_dict(dict_fns.unflatten(tuplize_dict(parse_params( request.POST)))) # we don't want to include save as it is part of the form del data['save'] context = {'model': model, 'session': model.Session, 'api_version': 3, 'for_edit': True, 'user': c.user or c.author, 'auth_user_obj': c.userobj} data['package_id'] = id try: if resource_id: data['id'] = resource_id get_action('resource_update')(context, data) else: get_action('resource_create')(context, data) except ValidationError, e: errors = e.error_dict error_summary = e.error_summary return self.resource_edit(id, resource_id, data, errors, error_summary) except NotAuthorized: abort(401, _('Unauthorized to edit this resource'))
def test_mixture_of_resource_types(self): """ Test with a mixture of additional and individual resources """ data = { 'resources': [ {'description': 'Description 1', 'url': 'Url1', 'resource_type': 'documentation'}, {'description': 'Description 2', 'url': 'Url2', 'resource_type': 'api'}, {'description': 'Description 3', 'url': 'Url3', 'resource_type': 'file'}, {'description': 'Description 4', 'url': 'Url4', 'resource_type': 'documentation'} ] } flattened_data = flatten_dict(data) ignored = {} errors = dict([(k, []) for k in flattened_data.keys()]) unmerge_resources(('__after',), flattened_data, errors, ignored) result_data = unflatten(flattened_data) expected_data = { 'additional_resources': [ {'description': 'Description 1', 'url': 'Url1', 'resource_type': 'documentation'}, {'description': 'Description 4', 'url': 'Url4', 'resource_type': 'documentation'} ], 'individual_resources': [ {'description': 'Description 2', 'url': 'Url2', 'resource_type': 'api'}, {'description': 'Description 3', 'url': 'Url3', 'resource_type': 'file'}, ] } expected_data.update(data) assert_equal(result_data, expected_data)
def post(self): try: req = request.form.copy() req.update(request.files.to_dict()) data_dict = logic.clean_dict( dict_fns.unflatten( logic.tuplize_dict( logic.parse_params( req, ignore_keys=CACHE_PARAMETERS)))) del data_dict['save'] data = logic.get_action(u'config_option_update')({ u'user': g.user }, data_dict) except logic.ValidationError as e: items = _get_config_options() data = request.form errors = e.error_dict error_summary = e.error_summary vars = dict( data=data, errors=errors, error_summary=error_summary, form_items=items, **items) return base.render(u'admin/config.html', extra_vars=vars) return h.redirect_to(u'admin.config')
def form(self): context = {'model': model, 'user': c.user} if not ckan.new_authz.is_authorized('sysadmin', context, {})['success']: base.abort(401, _('Need to be system administrator')) # get one and only one entry from our extlink table entry = model.Session.query(extlinkmodel.ExtLink).first() if not entry: # create the empty entry for the first time entry = extlinkmodel.ExtLink() entry.save() if base.request.method == "POST": data = logic.clean_dict( df.unflatten( logic.tuplize_dict( logic.parse_params(base.request.params) ))) entry.domains = data.get('white-list') entry.message = data.get('message') entry.save() h.flash_success(_('External link changes saved.')) h.redirect_to(controller=self.controller_path, action='form') c.extlinkdata = { 'white-list': entry.domains, 'message': entry.message, 'placeholder': msg_default, } return p.toolkit.render("form.html")
def edit(self, dataset_id, comment_id): context = {'model': model, 'user': c.user} # Auth check to make sure the user can see this package data_dict = {'id': dataset_id} check_access('package_show', context, data_dict) try: c.pkg_dict = get_action('package_show')(context, {'id': dataset_id}) c.pkg = context['package'] except: abort(403) if request.method == 'POST': data_dict = clean_dict(unflatten( tuplize_dict(parse_params(request.POST)))) data_dict['id'] = comment_id success = False try: res = get_action('comment_update')(context, data_dict) success = True except ValidationError, ve: log.debug(ve) except Exception, e: log.debug(e) abort(403)
def test_with_two_resource_types_defined(self): """ Tests with two resource types defined. """ data = { 'additional_resources': [ {'description': 'Additional 1', 'url': 'A_Url 1'}, {'description': 'Additional 2', 'url': 'A_Url 2'}, ], 'timeseries_resources': [ {'description': 'Timeseries 1', 'url': 'T_Url 1', 'date': 'T_Date 1'}, {'description': 'Timeseries 2', 'url': 'T_Url 2', 'date': 'T_Date 2'}, {'description': 'Timeseries 3', 'url': 'T_Url 3', 'date': 'T_Date 3'}, ] } flattened_data = flatten_dict(data) ignored = {} errors = dict([(k, []) for k in flattened_data.keys()]) merge_resources(('__after',), flattened_data, errors, ignored) result_data = unflatten(flattened_data) expected_data = { 'resources': [ {'description': 'Additional 1', 'url': 'A_Url 1'}, {'description': 'Additional 2', 'url': 'A_Url 2'}, {'description': 'Timeseries 1', 'url': 'T_Url 1', 'date': 'T_Date 1'}, {'description': 'Timeseries 2', 'url': 'T_Url 2', 'date': 'T_Date 2'}, {'description': 'Timeseries 3', 'url': 'T_Url 3', 'date': 'T_Date 3'}, ] } assert_equal(result_data, expected_data)
def importpackagedata(self,bulk_upload_id,file_path=None,resource_dir=None,ckanclient=None): """ This is an entry point for bulk upload of one record(bulk uploaded template). Loads the package details as dict from xls file and loads them into CKAN. Each successfully loaded pacakges are referenced against bulk upload record for tracking. """ from ckan.lib.navl.dictization_functions import unflatten from ckan.logic import (tuplize_dict,clean_dict ) from ckanext.ngds.importer.loader import ResourceLoader if ckanclient is None: ckanclient = self._get_ckanclient() loader = ResourceLoader(ckanclient,field_keys_to_find_pkg_by=['name'],resource_dir=resource_dir) package_import = NGDSPackageImporter(filepath=file_path) pkg_dicts = [pkg_dict for pkg_dict in package_import.pkg_dict()] for pkg_dict in pkg_dicts: try: returned_package = loader.load_package(clean_dict(unflatten(tuplize_dict(pkg_dict)))) # Upload bulk upload to package relationship. self._create_bulk_upload_package(bulk_upload_id,returned_package['name'],returned_package['title']) except Exception , e: log.info(e) log.info("Skipping this record and proceeding with next one....") raise
def post(self, group_type, is_organization, id=None): set_org(is_organization) context = self._prepare(id) data_dict = clean_dict( dict_fns.unflatten(tuplize_dict(parse_params(request.form)))) data_dict['id'] = id email = data_dict.get(u'email') if email: user_data_dict = { u'email': email, u'group_id': data_dict['id'], u'role': data_dict['role'] } del data_dict['email'] user_dict = _action(u'user_invite')(context, user_data_dict) data_dict['username'] = user_dict['name'] try: group_dict = _action(u'group_member_create')(context, data_dict) except NotAuthorized: base.abort(403, _(u'Unauthorized to add member to group %s') % u'') except NotFound: base.abort(404, _(u'Group not found')) except ValidationError as e: h.flash_error(e.error_summary) # TODO: Remove g.group_dict = group_dict return h.redirect_to(u'{}.members'.format(group_type), id=id)
def new(self, data=None, errors=None, error_summary=None): package_type = self._guess_package_type(True) context = {'model': model, 'session': model.Session, 'user': c.user or c.author, 'extras_as_string': True, 'save': 'save' in request.params,} # Package needs to have a organization group in the call to check_access # and also to save it try: check_access('package_create',context) except NotAuthorized: abort(401, _('Unauthorized to create a package')) if context['save'] and not data: return self._save_new(context) data = data or clean_dict(unflatten(tuplize_dict(parse_params( request.params, ignore_keys=[CACHE_PARAMETER])))) c.resources_json = json.dumps(data.get('resources',[])) errors = errors or {} error_summary = error_summary or {} vars = {'data': data, 'errors': errors, 'error_summary': error_summary} c.errors_json = json.dumps(errors) self._setup_template_variables(context, {'id': id}) # TODO: This check is to maintain backwards compatibility with the old way of creating # custom forms. This behaviour is now deprecated. if hasattr(self, 'package_form'): c.form = render(self.package_form, extra_vars=vars) else: c.form = render(self._package_form(package_type=package_type), extra_vars=vars) return render( self._new_template(package_type))
def _add_users( self, group, parameters ): if not group: h.flash_error(_("There was a problem with your submission, " "please correct it and try again")) errors = {"reason": ["No reason was supplied"]} return self.apply(group.id, errors=errors, error_summary=action.error_summary(errors)) data_dict = logic.clean_dict(dict_func.unflatten( logic.tuplize_dict(logic.parse_params(request.params)))) data_dict['id'] = group.id # Temporary fix for strange caching during dev l = data_dict['users'] for d in l: d['capacity'] = d.get('capacity','editor') context = { "group" : group, "schema": schema.default_group_schema(), "model": model, "session": model.Session } # Temporary cleanup of a capacity being sent without a name users = [d for d in data_dict['users'] if len(d) == 2] data_dict['users'] = users model.repo.new_revision() model_save.group_member_save(context, data_dict, 'users') model.Session.commit() h.redirect_to( controller='group', action='edit', id=group.name)
def validator(key, data, errors, context): # if there was an error before calling our validator # don't bother with our validation if errors[key]: return try: data_dict = df.unflatten(data[('__junk',)]) value = data_dict[key[0]] if value is not missing: if isinstance(value, basestring): value = [value] elif not isinstance(value, list): errors[key].append( _('Expecting list of strings, got "%s"') % str(value) ) return else: value = [] if not errors[key]: data[key] = json.dumps(value) # remove from junk del data_dict[key[0]] data[('__junk',)] = df.flatten_dict(data_dict) except KeyError: pass
def test_simple_case(self): """ Tests with just one set of resources defined. """ data = { 'additional_resources': [ {'description': 'Description 1', 'url': 'Url 1'}, {'description': 'Description 2', 'url': 'Url 2'}, ] } flattened_data = flatten_dict(data) ignored = {} errors = dict([(k, []) for k in flattened_data.keys()]) merge_resources(('__after',), flattened_data, errors, ignored) result_data = unflatten(flattened_data) expected_data = { 'resources': [ {'description': 'Description 1', 'url': 'Url 1'}, {'description': 'Description 2', 'url': 'Url 2'}, ] } assert_equal(result_data, expected_data)
def dictionary(self, id, resource_id): u'''data dictionary view: show/edit field labels and descriptions''' try: # resource_edit_base template uses these c.pkg_dict = get_action('package_show')(None, {'id': id}) c.resource = get_action('resource_show')(None, {'id': resource_id}) rec = get_action('datastore_search')(None, { 'resource_id': resource_id, 'limit': 0 }) except (ObjectNotFound, NotAuthorized): abort(404, _('Resource not found')) fields = [f for f in rec['fields'] if not f['id'].startswith('_')] if request.method == 'POST': data = dict_fns.unflatten( tuplize_dict(parse_params(request.params))) info = data.get(u'info') if not isinstance(info, list): info = [] info = info[:len(fields)] get_action('datastore_create')(None, { 'resource_id': resource_id, 'force': True, 'fields': [{ 'id': f['id'], 'type': f['type'], 'info': fi if isinstance(fi, dict) else {} } for f, fi in izip_longest(fields, info)] }) h.redirect_to( controller='ckanext.datastore.controller:DatastoreController', action='dictionary', id=id, resource_id=resource_id) return render('datastore/dictionary.html', extra_vars={'fields': fields})
def post(self, package_type, id): context = self._prepare(id) package_type = _get_package_type(id) or package_type log.debug(u'Package save request name: %s POST: %r', id, request.form) try: data_dict = clean_dict( dict_fns.unflatten(tuplize_dict(parse_params(request.form))) ) except dict_fns.DataError: return base.abort(400, _(u'Integrity Error')) try: if u'_ckan_phase' in data_dict: # we allow partial updates to not destroy existing resources context[u'allow_partial_update'] = True if u'tag_string' in data_dict: data_dict[u'tags'] = _tag_string_to_list( data_dict[u'tag_string'] ) del data_dict[u'_ckan_phase'] del data_dict[u'save'] context[u'message'] = data_dict.get(u'log_message', u'') data_dict['id'] = id pkg_dict = get_action(u'package_update')(context, data_dict) return _form_save_redirect( pkg_dict[u'name'], u'edit', package_type=package_type ) except NotAuthorized: return base.abort(403, _(u'Unauthorized to read package %s') % id) except NotFound as e: return base.abort(404, _(u'Dataset not found')) except SearchIndexError as e: try: exc_str = str(repr(e.args)) except Exception: # We don't like bare excepts exc_str = str(str(e)) return base.abort( 500, _(u'Unable to update search index.') + exc_str ) except ValidationError as e: errors = e.error_dict error_summary = e.error_summary return self.get(package_type, id, data_dict, errors, error_summary)
def initiate_search(self): """ This function is responsible for processing requests from the home page to initiate either a map search or library search with a search query that is provided to it. """ data = clean_dict(unflatten(tuplize_dict(parse_params( request.params)))) query = '' if 'query' in data: query = data['query'] if data['search-type'] == 'library': return redirect(h.url_for(controller='package', action='search', q=query, _tags_limit=0)) else: return redirect( h.url_for(controller='ckanext.ngds.ngdsui.controllers.home:HomeController', action='render_map', query=query))
def new(self, data=None, errors=None, error_summary=None): package_type = self._guess_package_type(True) context = { 'model': model, 'session': model.Session, 'user': c.user or c.author, 'extras_as_string': True, 'save': 'save' in request.params, } # Package needs to have a organization group in the call to check_access # and also to save it try: check_access('package_create', context) except NotAuthorized: abort(401, _('Unauthorized to create a package')) if context['save'] and not data: return self._save_new(context) data = data or clean_dict( unflatten( tuplize_dict( parse_params(request.params, ignore_keys=[CACHE_PARAMETER ])))) c.resources_json = json.dumps(data.get('resources', [])) errors = errors or {} error_summary = error_summary or {} vars = {'data': data, 'errors': errors, 'error_summary': error_summary} c.errors_json = json.dumps(errors) self._setup_template_variables(context, {'id': id}) # TODO: This check is to maintain backwards compatibility with the old way of creating # custom forms. This behaviour is now deprecated. if hasattr(self, 'package_form'): c.form = render(self.package_form, extra_vars=vars) else: c.form = render(self._package_form(package_type=package_type), extra_vars=vars) return render(self._new_template(package_type))
def post(self, package_type, id, resource_id, view_id=None): context, extra_vars = self._prepare(id, resource_id) to_preview = request.POST.pop(u'preview', False) if to_preview: context[u'preview'] = True to_delete = request.POST.pop(u'delete', None) data = clean_dict( dict_fns.unflatten( tuplize_dict( parse_params(request.form, ignore_keys=CACHE_PARAMETERS) ) ) ) data.pop(u'save', None) data[u'resource_id'] = resource_id try: if to_delete: data[u'id'] = view_id get_action(u'resource_view_delete')(context, data) elif view_id: data[u'id'] = view_id data = get_action(u'resource_view_update')(context, data) else: data = get_action(u'resource_view_create')(context, data) except ValidationError as e: # Could break preview if validation error to_preview = False extra_vars[u'errors'] = e.error_dict, extra_vars[u'error_summary'] = e.error_summary except NotAuthorized: # This should never happen unless the user maliciously changed # the resource_id in the url. return base.abort(403, _(u'Unauthorized to edit resource')) else: if not to_preview: return h.redirect_to( u'resource.views', id=id, resource_id=resource_id ) extra_vars[u'data'] = data extra_vars[u'to_preview'] = to_preview return self.get(package_type, id, resource_id, view_id, extra_vars)
def _save_attr_map_new(self, id, context): try: data_dict = clean_dict( dict_fns.unflatten( tuplize_dict(parse_params(tk.request.params)))) data_dict['metadata_standard_id'] = id data_dict.setdefault('is_key', False) tk.get_action('metadata_json_attr_map_create')(context, data_dict) tk.h.redirect_to('metadata_standard_attr_maps', id=id) except tk.NotAuthorized: tk.abort(403, tk._('Not authorized to create attribute mappings')) except tk.ObjectNotFound: tk.abort(404, tk._('Metadata standard not found')) except dict_fns.DataError: tk.abort(400, tk._(u'Integrity Error')) except tk.ValidationError, e: errors = e.error_dict error_summary = e.error_summary return self.attr_map_new(id, data_dict, errors, error_summary)
def _add_users(self, group, parameters): from ckan.logic.schema import default_group_schema from ckan.logic.action import error_summary from ckan.lib.dictization.model_save import group_member_save if not group: h.flash_error(_("There was a problem with your submission, \ please correct it and try again")) errors = {"reason": ["No reason was supplied"]} return self.apply(group.id, errors=errors, error_summary=error_summary(errors)) data_dict = clean_dict(unflatten( tuplize_dict(parse_params(request.params)))) data_dict['id'] = group.id # Temporary fix for strange caching during dev l = data_dict['users'] for d in l: # Form javascript creates d['capacity'] == 'undefined' for # newly added users. # If javascript in users form is not working (such as in tests) # it will not create a capacity value. if 'capacity' not in d or d['capacity'] == 'undefined': # default to 'editor' d['capacity'] = 'editor' context = { "group" : group, "schema": default_group_schema(), "model": model, "session": model.Session } # Temporary cleanup of a capacity being sent without a name users = [d for d in data_dict['users'] if len(d) == 2] data_dict['users'] = users model.repo.new_revision() group_member_save(context, data_dict, 'users') model.Session.commit() h.redirect_to('/publisher/%s' % str(group.name))
def _save_edit(self, id, context): try: data_dict = logic.clean_dict( df.unflatten( logic.tuplize_dict(logic.parse_params(request.params)))) context['message'] = data_dict.get('log_message', '') data_dict['id'] = id # MOAN: Do I really have to do this here? if 'activity_streams_email_notifications' not in data_dict: data_dict['activity_streams_email_notifications'] = False user = get_action('user_update')(context, data_dict) h.flash_success(_('Profile updated')) h.redirect_to('user_datasets', id=user['name']) except NotAuthorized: abort(401, _('Unauthorized to edit user %s') % id) except NotFound, e: abort(404, _('User not found'))
def _prepare_data_for_saving(self, context, data_dict, package_type): data_dict = clean_dict( dict_fns.unflatten(tuplize_dict(parse_params(request.POST)))) data_dict['type'] = package_type or data_dict.get('type') del data_dict['save'] context['message'] = data_dict.get('log_message', '') self.process_tag_string(data_dict) self.process_locations(data_dict) self.process_dataset_date(data_dict) self.process_expected_update_frequency(data_dict) self.process_methodology(data_dict) self.process_maintainer(context, data_dict) self.process_dataset_preview_save(data_dict) if 'private' not in data_dict: data_dict['private'] = 'True' return data_dict
def jupload_resource(self, id): if not request.method == 'POST': abort(400, _('Only POST is supported')) data = clean_dict( df.unflatten(tuplize_dict(parse_params(request.POST)))) package_name = data.get('name') file = data.get('file') if package_name == None or file == None: abort(400, _('Missing dataset name or file.')) context = { 'model': model, 'session': model.Session, 'user': c.user or c.author, 'auth_user_obj': c.userobj } try: package_dict = get_action('package_show')(context, { 'id': package_name }) except NotAuthorized: abort(401, _('Unauthorized to update dataset')) except NotFound: abort( 404, _('The dataset {id} could not be found.').format( id=package_name)) resource_dict = { 'package_id': package_name, 'upload': file, 'name': file.filename } try: resource = get_action('resource_create')(context, resource_dict) except ValidationError, e: errors = e.error_dict error_summary = e.error_summary return self.new_resource(id, data, errors, error_summary)
def _save_new(self, context, group_type=None): try: data_dict = clean_dict( dict_fns.unflatten(tuplize_dict(parse_params(request.params)))) data_dict['type'] = group_type or 'group' context['message'] = data_dict.get('log_message', '') data_dict['users'] = [{'name': c.user, 'capacity': 'admin'}] group = self._action('group_create')(context, data_dict) # Redirect to the appropriate _read route for the type of group h.redirect_to(group['type'] + '_read', id=group['name']) except (NotFound, NotAuthorized) as e: abort(404, _('Group not found')) except dict_fns.DataError: abort(400, _(u'Integrity Error')) except ValidationError as e: errors = e.error_dict error_summary = e.error_summary return self.new(data_dict, errors, error_summary)
def duplicate_extras_key(key, data, errors, context): ''' Test for a custom extra key being a duplicate of an existing (schema) key. ''' unflattened = df.unflatten(data) extras = unflattened.get('extras', []) extras_keys = [] for extra in extras: if not extra.get('deleted'): extras_keys.append(extra['key']) for extra_key in set(extras_keys): extras_keys.remove(extra_key) if extras_keys: for extra_key in extras_keys: errors[(extra_key, )] = [ _('Duplicate key for "%s" given') % extra_key ]
def metadata_record_infrastructures_not_missing(key, data, errors, context): """ Checks that the infrastructures list is not missing. For use with the '__before' schema key; CKAN puts empty lists into __extras, so we check that and pop as necessary. """ unflattened = df.unflatten(data) infrastructures = unflattened.get('infrastructures') if infrastructures is not None: return extras = data.get(key[:-1] + ('__extras', ), {}) infrastructures = extras.get('infrastructures') if infrastructures is not None: extras.pop('infrastructures') return errors.setdefault(key[:-1] + ('infrastructures', ), []) errors[key[:-1] + ('infrastructures', )].append(_('Missing parameter'))
def member_new(self, id): context = {'model': model, 'session': model.Session, 'user': c.user or c.author} #self._check_access('group_delete', context, {'id': id}) try: if request.method == 'POST': data_dict = clean_dict(dict_fns.unflatten( tuplize_dict(parse_params(request.params)))) data_dict['id'] = id email = data_dict.get('email') if email: user_data_dict = { 'email': email, 'group_id': data_dict['id'], 'role': data_dict['role'] } del data_dict['email'] user_dict = self._action('user_invite')(context, user_data_dict) data_dict['username'] = user_dict['name'] c.group_dict = self._action('group_member_create')(context, data_dict) self._redirect_to(controller='group', action='members', id=id) else: user = request.params.get('user') if user: c.user_dict = get_action('user_show')(context, {'id': user}) c.user_role = new_authz.users_role_for_group_or_org(id, user) or 'member' else: c.user_role = 'member' c.group_dict = self._action('group_show')(context, {'id': id}) group_type = 'organization' if c.group_dict['is_organization'] else 'group' c.roles = self._action('member_roles_list')( context, {'group_type': group_type} ) except NotAuthorized: abort(401, _('Unauthorized to add member to group %s') % '') except NotFound: abort(404, _('Group not found')) except ValidationError, e: h.flash_error(e.error_summary)
def test_error_keys_are_merged_over(self): """ Test that the items in errors are merged over correctly too. Note - errors may contain key values not present in the data """ data = { 'additional_resources': [ {'description': 'Additional 1', 'url': 'A_Url 1'}, {'description': 'Additional 2', 'url': 'A_Url 2'}, ], 'timeseries_resources': [ {'description': 'Timeseries 1', 'url': 'T_Url 1', 'date': 'T_Date 1'}, {'description': 'Timeseries 2', 'url': 'T_Url 2', 'date': 'T_Date 2'}, {'description': 'Timeseries 3', 'url': 'T_Url 3', 'date': 'T_Date 3'}, ] } flattened_data = flatten_dict(data) errors = dict([(k, []) for k in flattened_data.keys()]) # Add some extra fields into errors errors[('additional_resources', 0, 'resource_type')] = [] errors[('additional_resources', 1, 'resource_type')] = [] errors[('timeseries_resources', 0, 'resource_type')] = [] errors[('timeseries_resources', 1, 'resource_type')] = [] errors[('timeseries_resources', 2, 'resource_type')] = [] ignored = {} merge_resources(('__after',), flattened_data, errors, ignored) result_errors = unflatten(errors) expected_errors = { 'resources': [ {'description': [], 'url': [], 'resource_type': []}, {'description': [], 'url': [], 'resource_type': []}, {'description': [], 'url': [], 'date': [], 'resource_type': []}, {'description': [], 'url': [], 'date': [], 'resource_type': []}, {'description': [], 'url': [], 'date': [], 'resource_type': []}, ] } assert_equal(result_errors, expected_errors)
def contact(self, name=None): """ This action allows users to create an issue by filling in a contact form. """ import ckan.model as model from ckanext.redmine.client import RedmineClient print name extra_vars = {"data": {}, "errors": {}} client = RedmineClient(name) c.categories = client.load_categories() c.name = name if request.method == 'POST': data = clean_dict( unflatten(tuplize_dict(parse_params(request.POST)))) context = { 'model': model, 'session': model.Session, 'user': c.user } # Create the issue with the data we were passed. try: newid = get_action('issue_create')(context, data) if newid is None: self._save_on_fail(data) h.flash_success( _('Thank you for contacting us'.format(newid))) else: h.flash_success( _('Thank you for contacting us, please quote #{0} in future correspondence' .format(newid))) h.redirect_to(str(data.get('referer', '/'))) except ValidationError, e: extra_vars["errors"] = e.error_dict extra_vars["error_summary"] = e.error_summary extra_vars["data"] = data c.category_id = data.get('category', '')
def post(self, group_type, is_organization, id=None): set_org(is_organization) context = self._prepare(id) data_dict = clean_dict( dict_fns.unflatten(tuplize_dict(parse_params(request.form)))) data_dict['id'] = id email = data_dict.get(u'email') if email: user_data_dict = { u'email': email, u'group_id': data_dict['id'], u'role': data_dict['role'] } del data_dict['email'] try: user_dict = _action(u'user_invite')(context, user_data_dict) except ValidationError as e: for _key, error in e.error_summary.items(): h.flash_error(error) return h.redirect_to(u'{}.member_new'.format(group_type), id=id) data_dict['username'] = user_dict['name'] try: group_dict = _action(u'group_member_create')(context, data_dict) except NotAuthorized: base.abort(403, _(u'Unauthorized to add member to group %s') % u'') except NotFound: base.abort(404, _(u'Group not found')) except ValidationError as e: for _key, error in e.error_summary.items(): h.flash_error(error) return h.redirect_to(u'{}.member_new'.format(group_type), id=id) # TODO: Remove g.group_dict = group_dict return h.redirect_to(u'{}.members'.format(group_type), id=id)
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 _save_edit(self, id, context): try: data_dict = clean_dict( dict_fns.unflatten(tuplize_dict(parse_params(request.params)))) context['message'] = data_dict.get('log_message', '') data_dict['id'] = id context['allow_partial_update'] = True group = self._action('group_update')(context, data_dict) if id != group['name']: self._force_reindex(group) h.redirect_to('%s_read' % group['type'], id=group['name']) except (NotFound, NotAuthorized) as e: abort(404, _('Group not found')) except dict_fns.DataError: abort(400, _(u'Integrity Error')) except ValidationError as e: errors = e.error_dict error_summary = e.error_summary return self.edit(id, data_dict, errors, error_summary)
def kwh_data_type_validator(key, data, errors, context): clean_data = df.unflatten(data) entity_type = clean_data.get('type') entity_types = [ 'search_query', 'theme', 'sub_theme', 'research_question', 'dataset', 'visualization', 'dashboard' ] if entity_type not in entity_types: errors[key].append( p.toolkit._( 'Allowed KWH data types are: %s' % ', '.join(entity_types) ) )
def _save_edit(self, id, context): try: data_dict = clean_dict(unflatten( tuplize_dict(parse_params(request.POST)))) self._check_data_dict(data_dict) context['message'] = data_dict.get('log_message', '') if not context['moderated']: context['pending'] = False data_dict['id'] = id pkg = get_action('package_update')(context, data_dict) if request.params.get('save', '') == 'Approve': get_action('make_latest_pending_package_active')(context, data_dict) c.pkg = context['package'] c.pkg_dict = pkg self._form_save_redirect(pkg['name'], 'edit') except NotAuthorized: abort(401, _('Unauthorized to read package %s') % id) except NotFound, e: abort(404, _('Package not found'))
def test_merging_does_not_occur_when_there_have_been_validation_errors(self): """ Test resources aren't merged when there have been other validation errors. This is so that we don't throw out the user's original input. """ data = { 'additional_resources': [ {'description': 'Description 1', 'url': 'Url 1'}, {'description': 'Description 2', 'url': 'Url 2'}, ] } flattened_data = flatten_dict(data) errors = {('some_error',): ['Oh dear, you did something wrong!']} ignored = {} merge_resources(('__after',), flattened_data, errors, ignored) result_data = unflatten(flattened_data) assert_equal(data, result_data)
def iati_dataset_name(key,data,errors,context): unflattened = unflatten(data) value = data[key] if not unflattened.get('owner_org'): org_key = ("owner_org", ) errors[org_key] = errors.get("owner_org", []) errors[org_key].append('Publisher name missing. Please select a publisher from the list.') return org = get_action('organization_show')(context,{'id': unflattened['owner_org']}) org_name = org['name'] org_regex = re.compile(r'{org_name}-{any_code}'.format( org_name=re.escape(org_name), any_code='.+' )) if not org_regex.match(value): errors[key].append('Dataset name does not follow the convention <publisher>-<code>: "%s" (using publisher %s)' % (value, org_name))
def post(self, name): context = self._prepare(name) try: data_dict = clean_dict( dict_fns.unflatten(tuplize_dict(parse_params(request.form)))) data_dict['id'] = context['id'] theme = get_action(u'theme_update')(context, data_dict) h.flash_notice(_(u'Theme has been updated.')) except dict_fns.DataError: base.abort(400, _(u'Integrity Error')) except ValidationError as e: errors = e.error_dict error_summary = e.error_summary return self.get(name, data_dict, errors, error_summary) return h.redirect_to(u'theme.read', name=theme['name'])
def manageTokens(self): if userResourceAccess(c.user, 3) == False: abort(404, 'Page not found') data = {} errors = {} if toolkit.request.method == 'POST': formdata = clean_dict( dict_fns.unflatten( tuplize_dict(parse_params(toolkit.request.POST)))) if "removeToken" in formdata.keys(): data["tokenID"] = formdata['tokenID'] print data["tokenID"] deleted, message = deleteToken(data["tokenID"]) if deleted: h.flash_success(_("Successfully removed the token")) else: h.flash_error(message) if "allocateToken" in formdata.keys(): data["requestID"] = formdata['requestID'] added, tokenID = createToken(c.userobj.name) if added: updated, message = setTokenToRequest( data["requestID"], tokenID) if updated: h.flash_success( _("Successfully assigned the token. Search for token %s and edit its dataset access" ) % tokenID) else: h.flash_error(message) else: h.flash_error(message) requests = getTokenRequests(toolkit) vars = { 'error_summary': errors, 'action_type': 'manageRequests', 'action_group': 'tokens', 'requests': requests } return toolkit.render('ilriuser/resource_edit.html', extra_vars=vars)
def get(self, group_type: str, is_organization: bool, data: Optional[dict[str, Any]] = None, errors: Optional[dict[str, Any]] = None, error_summary: Optional[dict[str, Any]] = None) -> str: extra_vars = {} set_org(is_organization) context = self._prepare() data = data or clean_dict( dict_fns.unflatten( tuplize_dict( parse_params(request.args, ignore_keys=CACHE_PARAMETERS) ) ) ) if not data.get(u'image_url', u'').startswith(u'http'): data.pop(u'image_url', None) errors = errors or {} error_summary = error_summary or {} extra_vars: dict[str, Any] = { u'data': data, u'errors': errors, u'error_summary': error_summary, u'action': u'new', u'group_type': group_type } _setup_template_variables( context, data, group_type=group_type) form = base.render( _get_group_template(u'group_form', group_type), extra_vars) # TODO: Remove # ckan 2.9: Adding variables that were removed from c object for # compatibility with templates in existing extensions g.form = form extra_vars["form"] = form return base.render( _get_group_template(u'new_template', group_type), extra_vars)
def app_add(self): if c.userobj is None: tk.redirect_to( tk.url_for(controller='user', action='login', came_from=full_current_url())) form = CreateAppForm(tk.request.POST) data_dict = clean_dict( dict_fns.unflatten(tuplize_dict(parse_params(tk.request.params)))) upload = uploader.get_uploader('apps') if tk.request.POST: if form.validate(): # Upload[load image upload.update_data_dict(data_dict, 'image_url', 'image_upload', 'clear_upload') try: upload.upload(uploader.get_max_image_size()) except logic.ValidationError as err: flash_error(err.error_dict['image_upload'][0]) else: app = App() form.populate_obj(app) app.author_id = c.userobj.id app.content = strip_tags(app.content) app.status = "pending" app.image_url = data_dict.get('image_url') app.save() log.debug("App data is valid. Content: %s", strip_tags(app.name)) flash_success(tk._('You successfully create app')) jobs.enqueue( send_notifications_on_change_app_status, [app, 'pending', tk.request.environ.get('CKAN_LANG')]) tk.redirect_to(app.get_absolute_url()) else: flash_error(tk._('You have errors in form')) log.info("Validate errors: %s", form.errors) context = {'form': form, 'active_boards': Board.filter_active()} log.debug('ForumController.thread_add context: %s', context) return self.__render('create_app.html', context)
def RateDataset(self): context = { 'model': model, 'session': model.Session, 'user': c.user or c.author, 'auth_user_obj': c.userobj, 'for_view': True } rating = logic.clean_dict( df.unflatten( logic.tuplize_dict(logic.parse_params(base.request.params)))) logging.warning(rating) try: dataset_id = rating['dataset_id'] except KeyError: base.abort(400, _('Missing value') + ": dataset_id") try: rating = rating['value'] except KeyError: base.abort(400, _('Missing value') + ": value") if rating < 1: rating = 1 if rating > 5: rating = 5 if c.userobj == None: base.abort(401) data_dict = { 'rating': rating, 'dataset_id': dataset_id, 'user_id': c.userobj.id } if can_rate(c.userobj.id): new_rating(context, data_dict) return h.redirect_to(controller='package', action='read', id=dataset_id)
def _save_new(self, context): try: data_dict = logic.clean_dict(dict_fns.unflatten( logic.tuplize_dict(logic.parse_params(request.params)))) data_dict['group'] = data_dict['organization'] # TODO: Do we need info message at the UI level when e-mail could # not be sent? member = toolkit.get_action( 'member_request_create')(context, data_dict) helpers.redirect_to('organizations_index', id="newrequest", membership_id=member['id']) except dict_fns.DataError: abort(400, _(u'Integrity Error')) except logic.NotFound: abort(404, _('Item not found')) except logic.NotAuthorized: abort(405, self.not_auth_message) except logic.ValidationError, e: errors = e.error_dict error_summary = e.error_summary return self.new(errors, error_summary)
def _save_edit(self, name_or_id, context, package_type=None): ''' Edit a showcase's details, then redirect to the showcase read page. ''' data_dict = clean_dict( dict_fns.unflatten(tuplize_dict(parse_params(request.POST)))) data_dict['id'] = name_or_id try: pkg = get_action('ckanext_showcase_update')(context, data_dict) except ValidationError as e: errors = e.error_dict error_summary = e.error_summary return self.edit(name_or_id, data_dict, errors, error_summary) c.pkg_dict = pkg # redirect to showcase details page url = h.url_for(controller=SC_CTRL_NAME, action='read', id=pkg['name']) redirect(url)