def send(self, pkg_id): package = Package.get(pkg_id) url = h.url_for(controller='package', action="read", id=package.id) if c.user: userid = None for role in package.roles: if role.role == "admin": userid = role.user_id break if userid: owner = User.get(userid) msg = request.params.get('msg', '') if msg: send_contact_email(owner, c.userobj, package,\ msg) else: h.flash_error(_("No message")) return redirect(url) else: h.flash_error(_("No owner found")) return redirect(url) h.flash_notice(_("Message sent")) else: h.flash_error(_("Please login")) return redirect(url)
def _listener_route(self, action, id, resource_id): if not c.userobj or not c.userobj.sysadmin: base.abort(404) if action == 'terminate': task = session.query(model.TaskStatus)\ .filter( model.TaskStatus.task_type=='twitter_streaming', model.TaskStatus.entity_id==resource_id)\ .first() if not task: h.flash_error("Can't find listener") if task: pid = task.error or '' if not pid: h.flash_error("Can't get PID of process") else: h.flash_success('Success') toolkit.get_action('task_status_update')(None, { 'entity_id': resource_id, 'task_type': 'twitter_streaming', 'key': 'celery_task_id', 'state': 'Terminated', 'value': 'Ready for start', 'error': pid, 'last_updated': datetime.datetime.now().isoformat(), 'entity_type': 'resource' }) if os.system('kill -9 %s' % pid): toolkit.get_action('celery_revoke')(self.context, {'id': pid, 'resource': resource_id}) base.redirect(h.url_for('getting_tweets', id=id, resource_id=resource_id))
def delete_record(self, id): lc = ckanapi.LocalCKAN(username=c.user) filters = {} package_type = self._get_package_type(id) for f in primary_key_fields(package_type): filters[f['datastore_id']] = request.POST.get(f['datastore_id'], '') package = lc.action.package_show(id=id) result = lc.action.datastore_search( resource_id=package['resources'][0]['id'], filters=filters, rows=2) # only need two to know if there are multiple matches records = result['records'] x_vars = {'filters': filters, 'action': 'edit'} if not records: x_vars['delete_errors'] = [_('No matching records found')] elif len(records) > 1: x_vars['delete_errors'] = [_('Multiple matching records found')] if 'delete_errors' in x_vars: c.pkg_dict = package return render(self._edit_template(package_type), extra_vars=x_vars) # XXX: can't avoid the race here with the existing datastore API. # datastore_delete doesn't support _id filters lc.action.datastore_delete( resource_id=package['resources'][0]['id'], filters=filters, ) h.flash_success(_( "Record deleted." )) redirect(h.url_for(controller='package', action='read', id=id))
def refresh(self, id): try: context = { 'model': model, 'user': c.user, 'session': model.Session} p.toolkit.get_action('harvest_job_create')( context, {'source_id': id}) h.flash_success( _('Refresh requested, harvesting will take place within 15 minutes.')) except p.toolkit.ObjectNotFound: abort(404, _('Harvest source not found')) except p.toolkit.NotAuthorized: abort(401, self.not_auth_message) except Exception as e: if 'Can not create jobs on inactive sources' in str(e): h.flash_error(_('Cannot create new harvest jobs on inactive sources.' + ' First, please change the source status to \'active\'.')) elif 'There already is an unrun job for this source' in str(e): h.flash_notice( _('A harvest job has already been scheduled for this source')) else: msg = 'An error occurred: [%s]' % str(e) h.flash_error(msg) redirect(h.url_for('{0}_admin'.format(DATASET_TYPE_NAME), id=id))
def remove_pipe(self, id, pipeline_id): assert id assert pipeline_id try: data_dict = {'id': id} context = {'model': model, 'session': model.Session, 'user': c.user or c.author, 'auth_user_obj': c.userobj} check_access('package_update', context, data_dict) package = get_action('package_show')(context, data_dict) # id is most probably is package.name so we have to get actual id pipe = Pipelines(package['id'], pipeline_id).get() if not pipe: h.flash_error(_(u"Couldn't remove pipeline, because there is no such pipeline assigned to this dataset.")) base.redirect(h.url_for('pipe_assign', id=id)) else: pipe_id = pipe.pipeline_id pipe.delete() pipe.commit() h.flash_success(_(u'Pipeline removed from dataset successfully')) disable_schedules_for_pipe(pipe_id) except NotFound: abort(404, _(u'Dataset not found')) except NotAuthorized: abort(401, _(u'User {user} not authorized to edit {id}').format(user=c.user, id=id)) base.redirect(h.url_for('pipe_assign', id=id))
def upload(self, id): package_type = self._get_package_type(id) geno = get_geno(package_type) lc = ckanapi.LocalCKAN(username=c.user) dataset = lc.action.package_show(id=id) try: if request.POST['xls_update'] == '': raise BadExcelData('You must provide a valid file') _process_upload_file( lc, dataset, request.POST['xls_update'].file, geno) h.flash_success(_( "Your file was successfully uploaded into the central system." )) redirect(h.url_for(controller='package', action='read', id=id)) except BadExcelData, e: org = lc.action.organization_show(id=dataset['owner_org']) return self.preview_table( resource_name=dataset['resources'][0]['name'], owner_org=org['name'], errors=[e.message])
def read(self, id): context = { "model": model, "session": model.Session, "user": c.user or c.author, "extras_as_string": True, "for_view": True, } data_dict = {"id": id} try: logic.check_access("related_show", context, data_dict) except logic.NotAuthorized: abort(401, _("Not authorized to see this page")) related = model.Session.query(model.Related).filter(model.Related.id == id).first() if not related: abort(404, _("The requested related item was not found")) related.view_count = model.Related.view_count + 1 model.Session.add(related) model.Session.commit() base.redirect(related.url)
def resource_data(self, id, resource_id): if toolkit.request.method == "POST": try: toolkit.c.pkg_dict = p.toolkit.get_action("datapusher_submit")(None, {"resource_id": resource_id}) except logic.ValidationError: pass base.redirect( core_helpers.url_for( controller="ckanext.datapusher.plugin:ResourceDataController", action="resource_data", id=id, resource_id=resource_id, ) ) try: toolkit.c.pkg_dict = p.toolkit.get_action("package_show")(None, {"id": id}) toolkit.c.resource = p.toolkit.get_action("resource_show")(None, {"id": resource_id}) except logic.NotFound: base.abort(404, _("Resource not found")) except logic.NotAuthorized: base.abort(401, _("Unauthorized to edit this resource")) try: datapusher_status = p.toolkit.get_action("datapusher_status")(None, {"resource_id": resource_id}) except logic.NotFound: datapusher_status = {} except logic.NotAuthorized: base.abort(401, _("Not authorized to see this page")) return base.render("package/resource_data.html", extra_vars={"status": datapusher_status})
def bulk_process(self, id): """ Allow bulk processing of datasets for an organization. Make private/public or delete. For organization admins.""" group_type = self._get_group_type(id.split("@")[0]) if group_type != "organization": # FIXME: better error raise Exception("Must be an organization") # check we are org admin context = { "model": model, "session": model.Session, "user": c.user or c.author, "schema": self._db_to_form_schema(group_type=group_type), "for_view": True, "extras_as_string": True, } data_dict = {"id": id} try: c.group_dict = self._action("group_show")(context, data_dict) c.group = context["group"] except NotFound: abort(404, _("Group not found")) except NotAuthorized: abort(401, _("Unauthorized to read group %s") % id) # Search within group action = request.params.get("bulk_action") # If no action then just show the datasets if not action: # unicode format (decoded from utf8) limit = 500 self._read(id, limit) c.packages = c.page.items return render(self._bulk_process_template(group_type)) # process the action first find the datasets to perform the action on. # they are prefixed by dataset_ in the form data datasets = [] for param in request.params: if param.startswith("dataset_"): datasets.append(param[8:]) action_functions = { "private": "bulk_update_private", "public": "bulk_update_public", "delete": "bulk_update_delete", } data_dict = {"datasets": datasets, "org_id": c.group_dict["id"]} try: get_action(action_functions[action])(context, data_dict) except NotAuthorized: abort(401, _("Not authorized to perform bulk update")) base.redirect(h.url_for(controller="organization", action="bulk_process", id=id))
def index(self): # Avoid duplicate URLs for the same WMS service # (Only if it has not been checked before) if not request.params.get('deduped', False): urls = request.params.getall('url') deduped_urls = set(urls) if len(deduped_urls) < len(urls): # Redirect to the same location, but with the deduplicated # URLs. offset = url_for(controller='ckanext.os.controllers.widgets:PreviewWidget',action='index') query_string = urlencode([('url', u) for u in deduped_urls]) for key,value in request.params.iteritems(): if key != 'url': query_string += '&' + urlencode([(key, value)]) query_string += '&deduped=true' new_url = offset + '?' + query_string redirect(new_url) # Render the page c.libraries_base_url = 'http://%s/libraries' % LIBRARIES_HOST c.tiles_url_ckan = TILES_URL_CKAN c.wms_url_ckan = WMS_URL_CKAN c.wfs_url_ckan = WFS_URL_CKAN return render('os/map_preview.html')
def dataset_export_dcat(self, name_or_id): context = self._make_context() result = _get_action('dataset_export_dcat')(context, { 'id': name_or_id }) exported_url = result.get('url') redirect(exported_url) return
def sync_all_ext(self, id): self._load(id) if not c.pkg_dict['private']: sync(c.pkg_dict, False) h.flash_notice(_('Synchronization with external catalogs ended.')) else: h.flash_notice(_("Synchronization not started, the dataset isn't public.")) base.redirect(h.url_for('dataset_publishing', id=id))
def send(self): if not 'email' in request.params: abort(400,_('Please provide an email address')) email = request.params['email'] row = {'email':email,'signedup': datetime.now().isoformat()} self.table.AddRecord(row) h.flash_success(_('Your email has been stored. Thank you for your interest.')) redirect('/')
def bulk_process(self, id): ''' Allow bulk processing of datasets for an organization. Make private/public or delete. For organization admins.''' group_type = self._get_group_type(id.split('@')[0]) if group_type != 'organization': # FIXME: better error raise Exception('Must be an organization') # check we are org admin context = {'model': model, 'session': model.Session, 'user': c.user or c.author, 'schema': self._db_to_form_schema(group_type=group_type), 'for_view': True, 'extras_as_string': True} data_dict = {'id': id} try: c.group_dict = self._action('group_show')(context, data_dict) c.group = context['group'] except NotFound: abort(404, _('Group not found')) except NotAuthorized: abort(401, _('Unauthorized to read group %s') % id) # Search within group action = request.params.get('bulk_action') # If no action then just show the datasets if not action: # unicode format (decoded from utf8) limit = 500 self._read(id, limit) c.packages = c.page.items return render(self._bulk_process_template(group_type)) # process the action first find the datasets to perform the action on. # they are prefixed by dataset_ in the form data datasets = [] for param in request.params: if param.startswith('dataset_'): datasets.append(param[8:]) action_functions = { 'private': 'bulk_update_private', 'public': 'bulk_update_public', 'delete': 'bulk_update_delete', } data_dict = {'datasets': datasets, 'org_id': c.group_dict['id']} try: get_action(action_functions[action])(context, data_dict) except NotAuthorized: abort(401, _('Not authorized to perform bulk update')) base.redirect(h.url_for(controller='organization', action='bulk_process', id=id))
def flickr_update(self): log.warn('FLICKR UPDATE') # inits context self._init_context() toolkit.get_action('dfmp_flickr_update')(self.context, {}) # redirect to DFMP homepage base.redirect(c.environ.get('HTTP_REFERER', config.get('ckan.site_url','/')))
def import_metadata(self, **kwargs): if request.method == 'POST': redirect_url = self._import_metadata(request.params) redirect(redirect_url) else: c.group_id = request.params.get('group') c.error_summary = session.pop('error_summary', None) c.errors = session.pop('errors', None) c.result = session.pop('result', None) return render('package/import_metadata.html')
def translation_resources_delete(self, resource_id, id): #self._change_trans_res_status(resource_id, language) pkg_dict = self._check_pkg_access(id) res = self._check_res_access(resource_id) #self._check_trans_res_status(res, language) self._delete_all_resources(resource_id) self._setup_template_variables(pkg_dict, res) redirect(toolkit.url_for(controller='package', action='read', id=id))
def delete(self,id): try: context = {'model':model, 'user':c.user} p.toolkit.get_action('harvest_source_delete')(context, {'id':id}) h.flash_success(_('Harvesting source successfully inactivated')) redirect(h.url_for('harvest')) except p.toolkit.ObjectNotFound: abort(404,_('Harvest source not found')) except p.toolkit.NotAuthorized,e: abort(401,self.not_auth_message)
def comments(self, dataset_id, issue_number): # POST only if request.method != 'POST': abort(500, _('Invalid request')) dataset = self._before_dataset(dataset_id) auth_dict = { 'dataset_id': c.pkg['id'], 'issue_number': issue_number } # Are we not repeating stuff in logic ??? try: logic.check_access('issue_create', self.context, auth_dict) except logic.NotAuthorized: abort(401, _('Not authorized')) next_url = h.url_for('issues_show', dataset_id=c.pkg['name'], issue_number=issue_number) # TODO: (?) move validation somewhere better than controller comment = request.POST.get('comment') if not comment or comment.strip() == '': h.flash_error(_('Comment cannot be empty')) redirect(next_url) return # do this first because will error here if not allowed and do not want # comment created in that case if 'close' in request.POST or 'reopen' in request.POST: status = (issuemodel.ISSUE_STATUS.closed if 'close' in request.POST else issuemodel.ISSUE_STATUS.open) issue_dict = { 'issue_number': issue_number, 'dataset_id': dataset['id'], 'status': status } logic.get_action('issue_update')(self.context, issue_dict) if 'close' in request.POST: h.flash_success(_("Issue closed")) else: h.flash_success(_("Issue re-opened")) data_dict = { 'author_id': c.userobj.id, 'comment': comment.strip(), 'dataset_id': dataset['id'], 'issue_number': issue_number, } logic.get_action('issue_comment_create')(self.context, data_dict) redirect(next_url)
def new(self, data={}, errors={}, error_summary={}): if not self.authz.am_authorized(c, Action.PACKAGE_CREATE, System()): abort(401, _('Unauthorized to create an application')) if request.method == 'POST' and not errors: try: image = request.POST.get('image') data_dict = dict(request.params) app = create_application(data_dict, image) redirect('/app/%s' % app.name) except ValidationError, e: errors = e.error_dict error_summary = e.error_summary return self.new(data_dict, errors, error_summary)
def dataset_export_dcat(self, name_or_id): context = self._make_context() try: result = _get_action('dataset_export_dcat')(context, { 'id': name_or_id }) except toolkit.ObjectNotFound as ex: abort(404, detail=u'Package "%s" not found' % (name_or_id)) except toolkit.NotAuthorized as ex: abort(401, detail=u'Not authorized to export package "%s"' % (name_or_id)) exported_url = result.get('url') redirect(exported_url) return
def create_request(self, pkg_id): pkg = Package.get(pkg_id) user = c.userobj if c.userobj else None if user: req = KataAccessRequest(user.id, pkg.id) req.save() url = h.url_for(controller='package', action='read', id=pkg_id) h.flash_success(_("You now requested editor rights to package %s" % pkg.name)) redirect(url) else: url = h.url_for(controller='package', action='read', id=pkg_id) h.flash_error(_("Please log in!")) redirect(url)
def view(self, uuid, version=None): ''' View object. If this is normal HTTP request, this will redirect to the record, otherwise if the request is for RDF (content negotiation) return the rdf. :param uuid: the uuid of the object :param version: the version of the object, or None for current version ''' if uuid in ABYSSLINE_UUIDS: self.abyssline_object_redirect(uuid, version) try: # get the record at the given version record, resource = get_record_by_uuid(uuid, version) except TypeError: pass else: if record: # is the request for a particular format requested_format = check_access_header() if requested_format: # if so provide the kwargs necessary to build the object rdf url url_kwargs = dict( controller='ckanext.nhm.controllers.object:ObjectController', action='rdf', uuid=uuid, _format=requested_format, ) else: # otherwise, the user wants html so provide the kwargs necessary to build the # record page package_id = resource.get_package_id() package = get_action('package_show')(self.context, {'id': package_id}) url_kwargs = dict( controller='ckanext.nhm.controllers.record:RecordController', action='view', package_name=package['name'], resource_id=resource.id, record_id=record['_id'], ) # add the version if we have one if version is not None: url_kwargs['version'] = version # redirect using a 303 (as recommended by CETAF) base.redirect(h.url_for(**url_kwargs), code=303) abort(404, _('Record not found'))
def upload(self, id): package_type = self._get_package_type(id) try: if request.POST['xls_update'] == u'': raise ValidationError({'xls_update': 'You must provide a valid file'}) upload_data = read_xls('', file_contents = request.POST['xls_update'].file.read()) sheet_name, org_name = next(upload_data) lc = ckanapi.LocalCKAN(username = c.user) package = lc.action.package_show(id = id) owner_org = package['organization']['name'] #is this the right sheet for this organization? if org_name != owner_org: msg = ('Invalid sheet for this organization. Sheet must be labeled for{0}, ' + 'but you supplied a sheet for {1}').format(owner_org, org_name) raise ValidationError({'xls_update': msg}) for t in _get_tables(): if t['xls_sheet_name'] == sheet_name: break else: msg = "Sheet name '{0}' not found in list of valid tables".format(sheet_name) raise ValidationError({'xls_update': msg}) resource_id = package['resources'][0]['id'] records = [] fields = t['datastore_table']['fields'] for n, row in enumerate(upload_data): if len(row) != len(fields): msg = ("Row {0} of this sheet has {1} columns, " "expecting {2}").format(n+3, len(row), len(fields)) raise ValidationError({'xls_update': msg}) records.append(dict( (f['id'], v) for f, v in zip(fields, row))) lc.action.datastore_upsert(resource_id=resource_id, records=records) redirect(h.url_for(controller='package', action='read', id=id)) except ValidationError, e: errors = [] for error in e.error_dict.values(): errors.append(str(error).decode('utf-8')) vars = {'errors': errors, 'action': 'edit'} c.pkg_dict = package return render(self._edit_template(package_type), extra_vars = vars)
def new_metadata(self, id, data=None, errors=None, error_summary=None): import ckan.lib.base as base # Change the package state from draft to active and save it. context = {'model': model, 'session': model.Session, 'user': toolkit.c.user or toolkit.c.author, 'auth_user_obj': toolkit.c.userobj} data_dict = toolkit.get_action('package_show')(context, {'id': id}) data_dict['id'] = id data_dict['state'] = 'active' toolkit.get_action('package_update')(context, data_dict) base.redirect(helpers.url_for(controller='package', action='read', id=id))
def _save_edit(self,id): try: data_dict = dict(request.params) data_dict['id'] = id self._check_data_dict(data_dict) context = {'model':model, 'user':c.user, 'session':model.Session, 'schema':harvest_source_form_schema()} source = p.toolkit.get_action('harvest_source_update')(context,data_dict) h.flash_success(_('Harvest source edited successfully.')) redirect('/harvest/%s' %id) except p.toolkit.NotAuthorized,e: abort(401,self.not_auth_message)
def _form_save_redirect(self, pkgname, action): """This redirects the user to the CKAN package/read page, unless there is request parameter giving an alternate location, perhaps an external website. @param pkgname - Name of the package just edited @param action - What the action of the edit was """ assert action in ("new", "edit") url = request.params.get("return_to") or config.get("package_%s_return_url" % action) if url: url = url.replace("<NAME>", pkgname) else: url = h.url_for(controller="package", action="read", id=pkgname) redirect(url)
def new(self, package_id, resource_id=None): self._before(package_id) if not c.user: abort(401, _('Please login to add a new issue')) data_dict = { 'dataset_id': c.pkg['id'], 'creator_id': c.userobj.id } try: logic.check_access('issue_create', self.context, data_dict) except logic.NotAuthorized: abort(401, _('Not authorized to add a new issue')) resource = model.Resource.get(resource_id) if resource_id else None if resource: data_dict['resource_id'] = resource.id c.errors, c.error_summary = {}, {} if request.method == 'POST': # TODO: ? use dictization etc # data = logic.clean_dict( # df.unflatten( # logic.tuplize_dict( # logic.parse_params(request.params)))) data_dict.update({ 'title': request.POST.get('title'), 'description': request.POST.get('description') }) if not data_dict['title']: c.error_summary['title'] = ["Please enter a title"] c.errors = c.error_summary if not c.error_summary: # save and redirect issue_dict = logic.get_action('issue_create')( data_dict=data_dict ) h.flash_success(_('Your issue has been registered, ' 'thank you for the feedback')) redirect(h.url_for( 'issues_show', package_id=c.pkg['name'], id=issue_dict['id'] )) c.data_dict = data_dict return render("issues/add.html")
def _form_save_redirect(self, pkgname, action): '''This redirects the user to the CKAN package/read page, unless there is request parameter giving an alternate location, perhaps an external website. @param pkgname - Name of the package just edited @param action - What the action of the edit was ''' assert action in ('new', 'edit') url = request.params.get('return_to') or \ config.get('package_%s_return_url' % action) if url: url = url.replace('<NAME>', pkgname) else: url = h.url_for(controller='package', action='read', id=pkgname) redirect(url)
def sync_ext(self, id, cat_id): self._load(id) log.debug("syncronizing specific ext catalog {0}".format(cat_id)) if not c.pkg_dict['private']: from_ckan = CkanAPIWrapper(src_ckan, None) ext_cat = ExternalCatalog.by_id(cat_id) errors = [] errs = sync_ext_catalog(from_ckan, ext_cat, c.pkg_dict) for err in errs: errors.append('{0} - {1}'.format(ext_cat.url, err)) flash_errors_for_ext_cat(errors) h.flash_notice(_('Synchronization with public catalog ended.')) else: h.flash_notice(_("Synchronization not started, the dataset isn't public.")) base.redirect(h.url_for('dataset_publishing', id=id))
def type_redirect(self, resource_name): orgs = h.organizations_available('read') if not orgs: abort(404, _('No organizations found')) try: chromo = get_chromo(resource_name) except RecombinantException: abort(404, _('Recombinant resource_name not found')) # custom business logic if is_sysadmin(c.user): return redirect(h.url_for('recombinant_resource', resource_name=resource_name, owner_org='tbs-sct')) return redirect(h.url_for('recombinant_resource', resource_name=resource_name, owner_org=orgs[0]['name']))
def publish(self): lc = LocalCKAN(username=c.user) publish_date = date_str_to_datetime( request.str_POST['publish_date'] ).strftime("%Y-%m-%d %H:%M:%S") # get a list of package id's from the for POST data for key, package_id in request.str_POST.iteritems(): if key == 'publish': lc.action.package_patch( id=package_id, portal_release_date=publish_date, ) # return us to the publishing interface redirect(h.url_for('ckanadmin_publish'))
def identify_resource(self, resource_id, storer_type, parent): user_dict = self._check_access() self._setup_template_variables(user_dict) context, data_dict = self._get_context() resource = model.Session.query(model.Resource).get(resource_id) if storer_type == ResourceStorerType.VECTOR: # Fixme: adapt resource_actions.identify_resource(resource) if parent == 'dashboard': _action = 'show_dashboard_resources' else: _action = 'show_admin_page_resources' redirect( toolkit.url_for( controller= 'ckanext.publicamundi.controllers.user:UserController', action=_action))
def new_metadata(self, id, data=None, errors=None, error_summary=None): import ckan.lib.base as base # Change the package state from draft to active and save it. context = { 'model': model, 'session': model.Session, 'user': toolkit.c.user or toolkit.c.author, 'auth_user_obj': toolkit.c.userobj } data_dict = toolkit.get_action('package_show')(context, {'id': id}) data_dict['id'] = id data_dict['state'] = 'active' toolkit.get_action('package_update')(context, data_dict) base.redirect( helpers.url_for(controller='package', action='read', id=id))
def resource_spatialingest(self, resource_id): if toolkit.request.method == 'POST': try: resource_dict = toolkit.get_action('resource_show')( {}, { 'id': resource_id }) toolkit.get_action('spatialingestor_ingest_resource')( {}, resource_dict) except logic.ValidationError: pass base.redirect( core_helpers.url_for( controller= 'ckanext.spatialingestor.plugin:ResourceSpatialController', action='resource_spatialingest', resource_id=resource_id)) try: toolkit.c.resource = toolkit.get_action('resource_show')( None, { 'id': resource_id }) toolkit.c.pkg_dict = toolkit.get_action('package_show')( None, { 'id': toolkit.c.resource['package_id'] }) except logic.NotFound: base.abort(404, _('Resource not found')) except logic.NotAuthorized: base.abort(401, _('Unauthorized to edit this resource')) try: spatialingestor_status = toolkit.get_action( 'spatialingestor_status')(None, { 'resource_id': resource_id, 'job_type': 'spatial_ingest' }) except logic.NotFound: spatialingestor_status = {} except logic.NotAuthorized: base.abort(401, _('Not authorized to see this page')) return base.render('package/resource_spatialingest.html', extra_vars={'status': spatialingestor_status})
def reject(self, id): context = {'user': c.user, 'model': model} reason = request.POST.get('reason') tk.get_action('move_to_previous_stage')(context, { 'id': id, 'reason': reason }) endpoint = request.referrer or '/' return base.redirect(endpoint)
def _save_edit(self, id): try: data_dict = dict(request.params) data_dict['id'] = id self._check_data_dict(data_dict) context = { 'model': model, 'user': c.user, 'session': model.Session, 'schema': harvest_source_form_schema() } source = get_action('harvest_source_update')(context, data_dict) h.flash_success(_('Harvest source edited successfully.')) redirect('/harvest/%s' % id) except NotAuthorized, e: abort(401, self.not_auth_message)
def delete(self,id): try: context = {'model':model, 'user':c.user} context['clear_source'] = request.params.get('clear', '').lower() in (u'true', u'1') p.toolkit.get_action('harvest_source_delete')(context, {'id':id}) if context['clear_source']: h.flash_success(_('Harvesting source successfully cleared')) else: h.flash_success(_('Harvesting source successfully inactivated')) redirect(h.url_for('{0}_admin'.format(DATASET_TYPE_NAME), id=id)) except p.toolkit.ObjectNotFound: abort(404,_('Harvest source not found')) except p.toolkit.NotAuthorized: abort(401,self.not_auth_message)
def resource_data(self, id, resource_id): if toolkit.request.method == 'POST': try: toolkit.c.pkg_dict = p.toolkit.get_action('datapusher_submit')( None, { 'resource_id': resource_id }) except logic.ValidationError: pass base.redirect( core_helpers.url_for( controller= 'ckanext.datapusher.plugin:ResourceDataController', action='resource_data', id=id, resource_id=resource_id)) try: toolkit.c.pkg_dict = p.toolkit.get_action('package_show')(None, { 'id': id }) toolkit.c.resource = p.toolkit.get_action('resource_show')( None, { 'id': resource_id }) except logic.NotFound: base.abort(404, _('Resource not found')) except logic.NotAuthorized: base.abort(401, _('Unauthorized to edit this resource')) try: datapusher_status = p.toolkit.get_action('datapusher_status')( None, { 'resource_id': resource_id }) except logic.NotFound: datapusher_status = {} except logic.NotAuthorized: base.abort(401, _('Not authorized to see this page')) return base.render('package/resource_data.html', extra_vars={'status': datapusher_status})
def random_datasets(self): import random from routes import url_for seed = request.params.get('seed') sample_size = request.params.get('sample_size') needs_redirect = False if seed is None: seed = random.randint(1000, 9999) needs_redirect = True if sample_size is None: sample_size = 10 needs_redirect = True if needs_redirect: url_params = dict(seed=seed, **request.environ['pylons.routes_dict']) if sample_size: url_params['sample_size'] = sample_size redirect(url_for(**url_params)) try: seed = int(seed) except: abort(401, 'Bad seed') random.seed(seed) if sample_size: try: sample_size = int(sample_size) except: abort(401, 'Bad sample_size') dataset_q = \ model.Session.query(model.Package.name).\ filter_by(state='active').\ order_by(model.Package.name) num_datasets = dataset_q.count() dataset_indexes = random.sample(range(num_datasets), sample_size) dataset_names = dataset_q.all() datasets = [dataset_names[i][0] for i in dataset_indexes] else: datasets = None return render('data/random_datasets.html', extra_vars=dict(sample_size=sample_size, datasets=datasets, next_seed=random.randint(1000, 9999)))
def rescind(self, id): context = { 'user': c.user, 'model': model } tk.get_action('workflow_rescind_dataset')(context, { 'id': id }) return base.redirect( h.url_for(controller='package', action='read', id=id))
def redirect(self): ''' redirect to the url parameter. ''' url = base.request.params.get('url') if not url: base.abort(400, _('Missing Value') + ': url') if h.url_is_local(url) and '\r' not in url and '\n' not in url: return base.redirect(url) else: base.abort(403, _('Redirecting to external site is not allowed.'))
def reject_resource(self, resource_id, parent): user_dict = self._check_access() self._setup_template_variables(user_dict) res_identify_query = model.Session.query(ResourceIngest).filter( ResourceIngest.resource_id == resource_id).first() res_identify_query.status = IngestStatus.REJECTED model.Session.commit() if parent == 'dashboard': _action = 'show_dashboard_resources' else: _action = 'show_admin_page_resources' redirect( toolkit.url_for( controller= 'ckanext.publicamundi.controllers.user:UserController', action=_action))
def submit_terms(self): ua = UserAgreement() came_from = request.params.get('came_from', h.url('/')) if not came_from: base.abort(400, _('Missing Value') + ': url') if h.url_is_local(came_from): ua.insert_new_agreement() response.set_cookie('wprdc_user_terms', 'true') return base.redirect(came_from) else: base.abort(403, _('Redirecting to external site is not allowed.'))
def publish(self): lc = LocalCKAN(username=c.user) publish_date = date_str_to_datetime( request.str_POST['publish_date']).strftime("%Y-%m-%d %H:%M:%S") # get a list of package id's from the for POST data count = 0 for key, package_id in request.str_POST.iteritems(): if key == 'publish': lc.action.package_patch( id=package_id, portal_release_date=publish_date, ) count += 1 # flash notice that records are published h.flash_notice(str(count) + _(u' record(s) published.')) # return us to the publishing interface redirect(h.url_for('ckanadmin_publish'))
def merge_revision(self, id): context = { 'user': c.user, 'model': model } pkg = tk.get_action('merge_dataset_revision')(context, { 'id': id }) return base.redirect( h.url_for(controller='package', action='read', id=pkg['id']))
def delete_comment(self, datarequest_id, comment_id): try: context = self._get_context() data_dict = {'id': comment_id} tk.check_access(constants.DATAREQUEST_COMMENT_DELETE, context, data_dict) tk.get_action(constants.DATAREQUEST_COMMENT_DELETE)(context, data_dict) helpers.flash_notice(tk._('Comment has been deleted')) base.redirect( helpers.url_for( controller= 'ckanext.datarequests.controllers.ui_controller:DataRequestsUI', action='comment', id=datarequest_id)) except tk.ObjectNotFound as e: log.warn(e) tk.abort(404, tk._('Comment %s not found') % comment_id) except tk.NotAuthorized as e: log.warn(e) tk.abort(403, tk._('You are not authorized to delete this comment'))
def resource_download(self, id, resource_id): """ Provides a direct download by redirecting the user to the url stored against this resource. """ context = { 'model': model, 'session': model.Session, 'user': c.user or c.author } try: rsc = get_action('resource_show')(context, {'id': resource_id}) pkg = get_action('package_show')(context, {'id': id}) except NotFound: abort(404, _('Resource not found')) except NotAuthorized: abort(401, _('Unauthorized to read resource %s') % id) if not 'url' in rsc: abort(404, _('No download is available')) redirect(rsc['url'])
def remove_pipe(self, id, pipeline_id): assert id assert pipeline_id try: data_dict = {'id': id} context = { 'model': model, 'session': model.Session, 'user': c.user or c.author, 'auth_user_obj': c.userobj } check_access('package_update', context, data_dict) package = get_action('package_show')(context, data_dict) # id is most probably is package.name so we have to get actual id pipe = Pipelines(package['id'], pipeline_id).get() if not pipe: h.flash_error( _(u"Couldn't remove pipeline, because there is no such pipeline assigned to this dataset." )) base.redirect(h.url_for('pipe_assign', id=id)) else: pipe_id = pipe.pipeline_id pipe.delete() pipe.commit() h.flash_success( _(u'Pipeline removed from dataset successfully')) disable_schedules_for_pipe(pipe_id) except NotFound: abort(404, _(u'Dataset not found')) except NotAuthorized: abort( 401, _(u'User {user} not authorized to edit {id}').format( user=c.user, id=id)) base.redirect(h.url_for('pipe_assign', id=id))
def read(self, id): context = {'model': model, 'session': model.Session, 'user': c.user or c.author, 'extras_as_string': True, 'for_view': True} data_dict = {'id': id} try: logic.check_access('related_show', context, data_dict) except logic.NotAuthorized: base.abort(401, _('Not authorized to see this page')) related = model.Session.query(model.Related).\ filter(model.Related.id == id).first() if not related: base.abort(404, _('The requested related item was not found')) related.view_count = model.Related.view_count + 1 model.Session.add(related) model.Session.commit() base.redirect(related.url)
def _form_save_redirect(self, pkgname, action): '''This redirects the user to the CKAN package/read page, unless there is request parameter giving an alternate location, perhaps an external website. @param pkgname - Name of the package just edited @param action - What the action of the edit was ''' assert action in ('new', 'edit') if action == 'new': msg = _('<span class="new-dataset">Congratulations, your dataset has been created. ' \ '<a href="%s">Upload or link ' \ 'some data now »</a></span>') msg = msg % h.url_for(controller='package', action='edit', id=pkgname, anchor='section-resources') h.flash_success(msg,allow_html=True) url = request.params.get('return_to') or \ config.get('package_%s_return_url' % action) if url: url = url.replace('<NAME>', pkgname) else: url = h.url_for(controller='package', action='read', id=pkgname) redirect(url)
def resource_download(self, id, resource_id, filename=None): context = { 'model': model, 'session': model.Session, 'user': c.user or c.author, 'auth_user_obj': c.userobj } try: resource = logic.get_action('resource_show')(context, { 'id': resource_id }) except logic.NotFound: base.abort(404, _('Resource not found')) except logic.NotAuthorized: base.abort(401, _('Unauthorized to read resource {0}'.format(id))) # This isn't a file upload, so either redirect to the source # (if available) or error out. if resource.get('url_type') != 'upload': url = resource.get('url') if not url: base.abort(404, _('No download is available')) base.redirect(url) if filename is None: # No filename was provided so we'll try to get one from the url. filename = os.path.basename(resource['url']) upload = uploader.get_resource_uploader(resource) uploaded_url = upload.get_url_from_filename(resource['id'], filename) # The uploaded file is missing for some reason, such as the # provider being down. if uploaded_url is None: base.abort(404, _('No download is available')) base.redirect(uploaded_url)
def purge(self, id): context = {'model': model, 'user': c.user, 'auth_user_obj': c.userobj} try: pkg = tk.get_action('package_show')(context, {'id': id}) del context['package'] tk.check_access('purge_unpublished_dataset', context, pkg) except tk.NotAuthorized: base.abort(401, _('Not authorized to see this page')) context['ignore_auth'] = True tk.get_action('dataset_purge')(context, {'id': id}) return base.redirect(h.url_for(controller='package', action='search'))
def type_redirect(self, resource_name): orgs = h.organizations_available('read') if not orgs: abort(404, _('No organizations found')) try: chromo = get_chromo(resource_name) except RecombinantException: abort(404, _('Recombinant resource_name not found')) return redirect( h.url_for('recombinant_resource', resource_name=resource_name, owner_org=orgs[0]['name']))
def upload(self, id): package_type = self._get_package_type(id) geno = get_geno(package_type) lc = ckanapi.LocalCKAN(username=c.user) dataset = lc.action.package_show(id=id) try: if request.POST['xls_update'] == '': raise BadExcelData('You must provide a valid file') _process_upload_file(lc, dataset, request.POST['xls_update'].file, geno) h.flash_success( _("Your file was successfully uploaded into the central system." )) redirect(h.url_for(controller='package', action='read', id=id)) except BadExcelData, e: org = lc.action.organization_show(id=dataset['owner_org']) return self.preview_table( resource_name=dataset['resources'][0]['name'], owner_org=org['name'], errors=[e.message])
class ViewController(BaseController): not_auth_message = p.toolkit._('Not authorized to see this page') def __before__(self, action, **params): super(ViewController,self).__before__(action, **params) c.dataset_type = DATASET_TYPE_NAME def delete(self,id): try: context = {'model':model, 'user':c.user} context['clear_source'] = request.params.get('clear', '').lower() in (u'true', u'1') p.toolkit.get_action('harvest_source_delete')(context, {'id':id}) if context['clear_source']: h.flash_success(_('Harvesting source successfully cleared')) else: h.flash_success(_('Harvesting source successfully inactivated')) redirect(h.url_for('{0}_admin'.format(DATASET_TYPE_NAME), id=id)) except p.toolkit.ObjectNotFound: abort(404,_('Harvest source not found')) except p.toolkit.NotAuthorized: abort(401,self.not_auth_message) def refresh(self, id): try: context = {'model':model, 'user':c.user, 'session':model.Session} p.toolkit.get_action('harvest_job_create')(context,{'source_id':id}) h.flash_success(_('Refresh requested, harvesting will take place within 15 minutes.')) except p.toolkit.ObjectNotFound: abort(404,_('Harvest source not found')) except p.toolkit.NotAuthorized: abort(401,self.not_auth_message) except Exception, e: if 'Can not create jobs on inactive sources' in str(e): h.flash_error(_('Cannot create new harvest jobs on inactive sources.' + ' First, please change the source status to \'active\'.')) elif 'There already is an unrun job for this source' in str(e): h.flash_notice(_('A harvest job has already been scheduled for this source')) else: msg = 'An error occurred: [%s]' % str(e) h.flash_error(msg) redirect(h.url_for('{0}_admin'.format(DATASET_TYPE_NAME), id=id))
def delete_record(self, id, resource_id): lc = ckanapi.LocalCKAN(username=c.user) filters = {} res = lc.action.resource_show(id=resource_id) for f in recombinant_primary_key_fields(res['name']): filters[f['datastore_id']] = request.POST.get(f['datastore_id'], '') result = lc.action.datastore_search( resource_id=resource_id, filters=filters, rows=2) # only need two to know if there are multiple matches records = result['records'] x_vars = {'filters': filters, 'action': 'edit'} if not records: x_vars['delete_errors'] = [_('No matching records found')] elif len(records) > 1: x_vars['delete_errors'] = [_('Multiple matching records found')] if 'delete_errors' in x_vars: c.pkg_dict = dataset = lc.action.package_show(id=id) return render('recombinant/resource_edit.html', extra_vars=dict(x_vars, dataset=dataset, resource=res)) # XXX: can't avoid the race here with the existing datastore API. # datastore_delete doesn't support _id filters lc.action.datastore_delete( resource_id=resource_id, filters=filters, ) h.flash_success(_( "Record deleted." )) redirect(h.url_for( controller='ckanext.recombinant.controller:PreviewController', action='preview_table', id=id, resource_id=resource_id))
def _save_new(self): try: data_dict = dict(request.params) self._check_data_dict(data_dict) context = { 'model': model, 'user': c.user, 'session': model.Session, 'schema': harvest_source_form_schema() } source = get_action('harvest_source_create')(context, data_dict) # Create a harvest job for the new source get_action('harvest_job_create')(context, { 'source_id': source['id'] }) h.flash_success( _('New harvest source added successfully.' 'A new harvest job for the source has also been created.')) redirect('/harvest/%s' % source['id']) except NotAuthorized, e: abort(401, self.not_auth_message)
def upload(self, id): package_type = self._get_package_type(id) geno = get_geno(package_type) lc = ckanapi.LocalCKAN(username=c.user) dataset = lc.action.package_show(id=id) try: if request.POST['xls_update'] == '': raise BadExcelData('You must provide a valid file') _process_upload_file( lc, dataset, request.POST['xls_update'].file, geno) h.flash_success(_( "Your file was successfully uploaded into the central system." )) redirect(h.url_for(controller='package', action='read', id=id)) except BadExcelData, e: x_vars = {'errors': [e.message], 'action': 'edit'} c.pkg_dict = dataset return render(self._edit_template(package_type), extra_vars=x_vars)
def approve(self, id): """Approve common dataset. If package just've got its last stage and it looks like revision - redirects to merge_revision route """ context = { 'user': c.user, 'model': model } endpoint = request.referrer or '/' pkg = tk.get_action('package_show')(context, {'id': id}) wf, _ = wh.get_workflow_from_package(pkg) next = str(wh.get_stage_from_package(pkg).approve()) if next == str(wf.finish) and wh.is_revision(pkg): endpoint = h.url_for('merge_dataset_revision', id=id) tk.get_action('move_to_next_stage')(context, {'id': id}) return base.redirect(endpoint)