def get_survey_codesets(context, data_dict): """ Returns all survey codesets. :param limit: Number of results to return. :type limit: int :param start: Number of results to skip. :type start: int :rtype: list of dicts """ lc = ckanapi.LocalCKAN(context=context) # Sort would perform better, but this will be easier # for client to implement. limit = int(logic.get_or_bust(data_dict, "limit")) start = int(logic.get_or_bust(data_dict, "start")) results = lc.action.package_search(q="dataset_type:survey", rows=limit, start=start, fl=("name", "title")) return { "count": results["count"], "limit": limit, "start": start, "results": [{"survey_code": r["product_id_new"], "title": r["title"]} for r in results["results"]], }
def taxonomy_update(context, data_dict): """ Updates an existing taxonomy. title, name and uri are required :returns: The newly updated taxonomy :rtype: A dictionary """ _check_access('taxonomy_update', context, data_dict) model = context['model'] id = logic.get_or_bust(data_dict, 'id') name = logic.get_or_bust(data_dict, 'name') title = logic.get_or_bust(data_dict, 'title') uri = logic.get_or_bust(data_dict, 'uri') tax = Taxonomy.get(id) if not tax: raise logic.NotFound() tax.name = name tax.title = title tax.uri = uri model.Session.add(tax) model.Session.commit() return tax.as_dict()
def taxonomy_term_create(context, data_dict): """ Allows for the creation of a new taxonomy term. :returns: The newly updated term :rtype: A dictionary """ _check_access('taxonomy_term_create', context, data_dict) model = context['model'] taxonomy_id = logic.get_or_bust(data_dict, 'taxonomy_id') taxonomy = logic.get_action('taxonomy_show')(context, {'id': taxonomy_id}) label = logic.get_or_bust(data_dict, 'label') uri = logic.get_or_bust(data_dict, 'uri') description = data_dict.get('description') if model.Session.query(TaxonomyTerm).\ filter(TaxonomyTerm.uri == uri).\ filter(TaxonomyTerm.taxonomy_id == taxonomy_id ).count() > 0: raise logic.ValidationError("Term uri already used in this taxonomy") term = TaxonomyTerm(**data_dict) model.Session.add(term) model.Session.commit() return term.as_dict()
def taxonomy_create(context, data_dict): """ Creates a new taxonomy. Terms are not created here, they must be created using taxonomy_term_create with the taxonomy id from this call. :param owner_org: the id of the dataset's owning organization, see :returns: The newly created taxonomy :rtype: A dictionary. """ _check_access('taxonomy_create', context, data_dict) model = context['model'] name = data_dict.get('name') title = logic.get_or_bust(data_dict, 'title') uri = logic.get_or_bust(data_dict, 'uri') if not name: name = munge_name(title) # Check the name has not been used if model.Session.query(Taxonomy).filter(Taxonomy.name == name).count() > 0: raise logic.ValidationError("Name is already in use") t = Taxonomy(name=name, title=title, uri=uri) model.Session.add(t) model.Session.commit() return t.as_dict()
def taxonomy_create(context, data_dict): """ Creates a new taxonomy. Terms are not created here, they must be created using taxonomy_term_create with the taxonomy id from this call. :param owner_org: the id of the dataset's owning organization, see :returns: The newly created taxonomy :rtype: A dictionary. """ _check_access('taxonomy_create', context, data_dict) model = context['model'] name = data_dict.get('name') title = logic.get_or_bust(data_dict, 'title') uri = logic.get_or_bust(data_dict, 'uri') if not name: name = munge_name(title) # Check the name has not been used if model.Session.query(Taxonomy).filter(Taxonomy.name == name).count() > 0: raise logic.ValidationError("Name is already in use") t = Taxonomy(name=name, title=title, uri=uri) model.Session.add(t) model.Session.commit() return t.as_dict()
def taxonomy_term_update(context, data_dict): """ Allows a taxonomy term to be updated. :returns: The newly updated term :rtype: A dictionary """ _check_access('taxonomy_term_update', context, data_dict) model = context['model'] id = logic.get_or_bust(data_dict, 'id') term = TaxonomyTerm.get(id) if not term: raise logic.NotFound() term.label = data_dict.get('label', term.label) term.parent_id = data_dict.get('parent_id', term.parent_id) term.uri = logic.get_or_bust(data_dict, 'uri') term.description = data_dict.get('description', '') term.extras = data_dict.get('extras', '') model.Session.add(term) model.Session.commit() return term.as_dict()
def taxonomy_update(context, data_dict): """ Updates an existing taxonomy. title, name and uri are required :returns: The newly updated taxonomy :rtype: A dictionary """ _check_access('taxonomy_update', context, data_dict) model = context['model'] id = logic.get_or_bust(data_dict, 'id') name = logic.get_or_bust(data_dict, 'name') title = logic.get_or_bust(data_dict, 'title') uri = logic.get_or_bust(data_dict, 'uri') tax = Taxonomy.get(id) if not tax: raise logic.NotFound() tax.name = name tax.title = title tax.uri = uri model.Session.add(tax) model.Session.commit() return tax.as_dict()
def taxonomy_term_create(context, data_dict): """ Allows for the creation of a new taxonomy term. :returns: The newly updated term :rtype: A dictionary """ _check_access('taxonomy_term_create', context, data_dict) model = context['model'] taxonomy_id = logic.get_or_bust(data_dict, 'taxonomy_id') taxonomy = logic.get_action('taxonomy_show')(context, {'id': taxonomy_id}) label = logic.get_or_bust(data_dict, 'label') uri = logic.get_or_bust(data_dict, 'uri') description = data_dict.get('description') if model.Session.query(TaxonomyTerm).\ filter(TaxonomyTerm.uri == uri).\ filter(TaxonomyTerm.taxonomy_id == taxonomy_id ).count() > 0: raise logic.ValidationError("Term uri already used in this taxonomy") term = TaxonomyTerm(**data_dict) model.Session.add(term) model.Session.commit() return term.as_dict()
def taxonomy_term_update(context, data_dict): """ Allows a taxonomy term to be updated. :returns: The newly updated term :rtype: A dictionary """ _check_access('taxonomy_term_update', context, data_dict) model = context['model'] id = logic.get_or_bust(data_dict, 'id') term = TaxonomyTerm.get(id) if not term: raise logic.NotFound() term.label = data_dict.get('label', term.label) term.parent_id = data_dict.get('parent_id', term.parent_id) term.uri = logic.get_or_bust(data_dict, 'uri') term.description = data_dict.get('description', '') term.extras = data_dict.get('extras', '') model.Session.add(term) model.Session.commit() return term.as_dict()
def get_themes(context, data_dict): # noinspection PyUnresolvedReferences """ Returns all subject codesets. :param limit: Number of results to return. :type limit: int :param start: Number of results to skip. :type start: int :rtype: list of dicts """ lc = ckanapi.LocalCKAN(context=context) # Sort would perform better, but this will be easier # for client to implement. limit = int(logic.get_or_bust(data_dict, 'limit')) start = int(logic.get_or_bust(data_dict, 'start')) results = lc.action.package_search( q='dataset_type:subject', rows=limit, start=start, fl=( 'name', 'title' ) ) def _massage(s): chunked = textwrap.wrap(s['subject_code'], 2) return ( s['subject_code'], chunked[-2] if len(chunked) > 1 else None, s['title'], dict((k, v.split('/')[-1]) for k, v in s['title'].iteritems()) ) return { 'count': results['count'], 'limit': limit, 'start': start, 'results': [{ 'subject_code': rr[0], 'parent_subject_code': rr[1], 'title': rr[2], 'subject_title': rr[3] } for rr in (_massage(r) for r in results['results'])] }
def taxonomy_delete(context, data_dict): """ Delete the specific taxonomy, and as a result, all of the terms within it. :returns: The newly deleted taxonomy :rtype: A dictionary """ _check_access('taxonomy_delete', context, data_dict) model = context['model'] name = logic.get_or_bust(data_dict, 'id') taxonomy = Taxonomy.get(name) if not taxonomy: raise logic.NotFound() terms = model.Session.query(TaxonomyTerm)\ .filter(TaxonomyTerm.taxonomy == taxonomy) map(model.Session.delete, terms.all()) model.Session.delete(taxonomy) model.Session.commit() return taxonomy.as_dict()
def create_access_request(context, data_dict): if not context.get('ignore_auth'): check_access('create_access_request', context, data_dict) pkg_id_or_name, reason, user = get_or_bust(data_dict, ['id', 'reason', 'user']) user_org = data_dict.get('user_org') # check if the package with such id exists to use it's ID pkg = Package.get(pkg_id_or_name) if not pkg: raise ObjectNotFound() req = AccessRequest.create_or_get_request(user_id=user, package_id=pkg.id, reason=reason, org_id=pkg.owner_org) # send email notifications to org custodians if config.get('spc.access_request.send_admin_notification'): notify_org_members(pkg.owner_org, { 'pkg': pkg, 'user': user, 'reason': reason, 'user_org': user_org }) return req.as_dict()
def resource_update_check(context, new_dict): # get the current resource dict show_context = { 'model': context['model'], 'session': context['session'], 'user': context['user'], 'auth_user_obj': context['auth_user_obj'], } old_dict = logic.get_action('resource_show')( show_context, { 'id': logic.get_or_bust(new_dict, 'id') }) # only allow updating the description... allowed_keys = ["description"] # ...and "sp:*" keys allowed_keys += rss.get_composite_item_list() invalid = [] for key in new_dict: if key in allowed_keys: continue elif key in old_dict and new_dict[key] == old_dict[key]: continue else: invalid.append(f"{key}={new_dict[key]}") if invalid: return { 'success': False, 'msg': f'Editing not allowed: {", ".join(invalid)}' } return {'success': True}
def active_resource_filters(context, data_dict): """Fetch all active filters for a given resource. :param resource_id: id of the resource. :type resource_id: string :returns: available filters for the resource :rtype: list of dictionaries """ resource_id = l.get_or_bust(data_dict, 'resource_id') resource = context['model'].Resource.get(resource_id) filters = [] out = [] if 'filters' in resource.extras: filters = json.loads(resource.extras['filters']) filter_names = map(lambda val: val['name'], filters) resource_meta = l.get_action('datastore_search')(context, {'resource_id': resource_id, 'limit': 1}) fields = resource_meta['fields'] # Make sure the order is always like the order of the metadata fields for _f in fields: if _f['id'] in filter_names: out.append( {'name': _f['id'], 'type': _f['type']} ) return out
def resource_filters(context, data_dict): """Fetch all available filters for a given resource. :param resource_id: id of the resource. :type resource_id: string :returns: available filters for the resource :rtype: list of dictionaries """ resource_id = l.get_or_bust(data_dict, 'resource_id') ds = l.get_action('datastore_info')(context, {'id': resource_id}) # Loop through datastore schema and validate filters available_filters = map(lambda val: val, ds.get('schema').keys()) _ = l.get_action('active_resource_filters')(context, {'resource_id': resource_id}) used_filters = map(lambda _f: _f['name'], _) out = [] diff = list(set(available_filters) - set(used_filters)) resource_meta = l.get_action('datastore_search')(context, {'resource_id': resource_id, 'limit': 1}) fields = resource_meta['fields'] # Make sure the order is always like the order of the metadata fields for _f in fields: if _f['id'] in diff: out.append( {'name': _f['id'], 'type': _f['type']} ) return out
def taxonomy_update(context, data_dict): """ Updates an existing taxonomy. title, name and uri are required :returns: The newly updated taxonomy :rtype: A dictionary """ _check_access('taxonomy_update', context, data_dict) model = context['model'] id = logic.get_or_bust(data_dict, 'id') tax = Taxonomy.get(id) if not tax: raise logic.NotFound() tax.name = data_dict.get('name', tax.name) tax.title = data_dict.get('title', tax.title) tax.uri = data_dict.get('name', tax.uri) last_modified = data_dict.get('last_modified', tax.last_modified) if tax.last_modified != last_modified: tax.last_modified = isodate(last_modified, context) model.Session.add(tax) model.Session.commit() return tax.as_dict()
def comment_update(context, data_dict): user = context['user'] model = context['model'] userobj = model.User.get(user) if not userobj: log.debug("User is not logged in") return { 'success': False, 'msg': _('You must be logged in to add a comment') } cid = logic.get_or_bust(data_dict, 'id') comment = comment_model.Comment.get(cid) if not comment: return {'success': False, 'msg': _('Comment does not exist')} if comment.user_id != userobj.id: return { 'success': False, 'msg': _('User is not the author of the comment') } return {'success': True}
def comment_delete(context, data_dict): model = context['model'] user = context['user'] userobj = model.User.get(user) # If sysadmin. if authz.is_sysadmin(user): return {'success': True} cid = logic.get_or_bust(data_dict, 'id') comment = comment_model.Comment.get(cid) if not comment: return {'success': False, 'msg': _('Comment does not exist')} content_type = data_dict.get('content_type', None) content_item_id = data_dict.get('content_item_id', None) if content_type and content_item_id: if not helpers.user_can_manage_comments(content_type, content_item_id): return { 'success': False, 'msg': _('User is not authorised to delete this comment') } else: return {'success': True} if comment.user_id is not userobj.id: return { 'success': False, 'msg': _('User is not the author of the comment') } return {'success': True}
def comment_update(context, data_dict): model = context['model'] #logic.check_access("comment_update", context, data_dict) cid = logic.get_or_bust(data_dict, 'id') comment = comment_model.Comment.get(cid) if not comment: abort(404) # Validate that we have the required fields. if not all([data_dict.get('comment')]): raise logic.ValidationError("Comment text is required") # Cleanup the comment cleaned_comment = util.clean_input(data_dict.get('comment')) comment.subject = data_dict.get('subject') comment.email = data_dict.get('email') comment.comment = cleaned_comment comment.modified_date = datetime.datetime.now() model.Session.add(comment) model.Session.commit() return comment.as_dict()
def harvest_source_reindex(context, data_dict): '''Reindex a single harvest source''' harvest_source_id = logic.get_or_bust(data_dict, 'id') defer_commit = context.get('defer_commit', False) if 'extras_as_string'in context: del context['extras_as_string'] context.update({'ignore_auth': True}) package_dict = logic.get_action('harvest_source_show')( context, {'id': harvest_source_id}) log.debug('Updating search index for harvest source: %s', package_dict.get('name') or harvest_source_id) # Remove configuration values new_dict = {} if package_dict.get('config'): config = json.loads(package_dict['config']) for key, value in package_dict.iteritems(): if key not in config: new_dict[key] = value package_index = PackageSearchIndex() package_index.index_package(new_dict, defer_commit=defer_commit) return True
def sub_theme_delete(context, data_dict): ''' Deletes a sub-theme :param id: the sub-theme's ID :type id: string :returns: OK :rtype: string ''' try: check_access('sub_theme_delete', context, data_dict) except NotAuthorized: raise NotAuthorized( _(u'Need to be system ' u'administrator to administer')) id = logic.get_or_bust(data_dict, 'id') questions = Session.query(ResearchQuestion) \ .filter_by(sub_theme=id) \ .count() if questions: raise logic.ValidationError( _('Sub-Theme cannot be deleted while it ' 'still has research questions')) try: filter = {'id': id} SubThemes.delete(filter) except NotFound: raise NotFound(_(u'Sub-theme')) return 'OK'
def resource_acl_patch(context, data_dict): '''Patch the resource acl :param id: the id of the resource acl :param auth_type: user, org :param auth_id: the id of user or organization :param permission: none, read ''' reference = get_or_bust(data_dict, 'id') acl = ResourceAcl.get(reference) if not acl: raise NotFound('acl <{id}> was not found.'.format(id=reference)) data_dict['resource_id'] = acl.resource_id check_access('resource_acl_patch', context, data_dict) data, errors = validate(data_dict, resource_acl_patch_schema(), context) if errors: raise ValidationError(errors) acl.auth_type = data.get('auth_type', acl.auth_type) acl.auth_id = data.get('auth_id', acl.auth_id) acl.permission = data.get('permission', acl.permission) acl.last_modified = datetime.datetime.utcnow() acl.modifier_user_id = context.get('user') acl.commit() return acl.as_dict()
def comment_update(context, data_dict): model = context['model'] logic.check_access("comment_update", context, data_dict) cid = logic.get_or_bust(data_dict, 'id') comment = comment_model.Comment.get(cid) if not comment: abort(404) # Validate that we have the required fields. if not all([data_dict.get('comment')]): raise logic.ValidationError("Comment text is required") # Cleanup the comment cleaned_comment = util.clean_input(data_dict.get('comment')) # Run profanity check if toolkit.asbool(config.get('ckan.comments.check_for_profanity', False)) \ and (helpers.profanity_check(cleaned_comment) or helpers.profanity_check(data_dict.get('subject', ''))): raise logic.ValidationError("Comment blocked due to profanity.") comment.subject = data_dict.get('subject') comment.comment = cleaned_comment comment.modified_date = datetime.datetime.utcnow() comment.flagged = data_dict.get('flagged') model.Session.add(comment) model.Session.commit() return comment.as_dict()
def comment_update_moderation(context, data_dict): import ckanext.comments.model as comment_model model = context['model'] user = context['user'] cid = logic.get_or_bust(data_dict, 'id') comment = comment_model.Comment.get(cid) if not comment: abort(404) # If sysadmin, then remove instantly if c.userobj.sysadmin: if len(comment.children) > 0: txt = config.get('ckan.comments.deleted.text', 'This message was deleted') comment.comment = txt else: comment.state = 'deleted' comment.approval_status = comment_model.COMMENT_PENDING comment.moderated_by = c.userobj.id comment.moderation_date = datetime.datetime.now() model.Session.add(comment) model.Session.commit() elif not comment.moderated_by: comment.spam_votes = comment.spam_votes + 1 comment.approval_status = comment_model.COMMENT_PENDING model.Session.add(comment) model.Session.commit() return {}
def forum_create_post(context, data_dict): """ Create post in thread data_dict: thread_id - Thread id for post content - Post text :return Post """ thread = Thread.get_by_id(logic.get_or_bust(data_dict, 'thread_id')) if thread.can_post: post = Post() post.thread = thread post.content = strip_tags(logic.get_or_bust(data_dict, 'content')) post.author_id = logic.get_or_bust(context, 'auth_user_obj').id post.save() return post
def harvest_source_reindex(context, data_dict): '''Reindex a single harvest source''' harvest_source_id = logic.get_or_bust(data_dict, 'id') defer_commit = context.get('defer_commit', False) if 'extras_as_string' in context: del context['extras_as_string'] context.update({'ignore_auth': True}) package_dict = logic.get_action('harvest_source_show')( context, { 'id': harvest_source_id }) log.debug('Updating search index for harvest source: %s', package_dict.get('name') or harvest_source_id) # Remove configuration values new_dict = {} try: config = json.loads(package_dict.get('config', '')) except ValueError: config = {} for key, value in package_dict.items(): if key not in config: new_dict[key] = value package_index = PackageSearchIndex() package_index.index_package(new_dict, defer_commit=defer_commit) return True
def purge_publisher_datasets(context, data_dict): ''' Purge all deleted datasets belonging to a given publisher. :returns: number of revisions purged. :rtype: dictionary ''' logic.check_access('purge_publisher_datasets', context, data_dict) model = context['model'] engine = model.meta.engine publisher_name = logic.get_or_bust(data_dict, 'name') group = model.Group.get(publisher_name) if not group: raise logic.NotFound('Publisher {0} not found'.format(publisher_name)) deleted_datasets = ''' SELECT package.id FROM package INNER JOIN member ON (member.table_name='package' AND member.table_id=package.id) INNER JOIN "group" ON ("group".id=member.group_id) WHERE "group".name='{publisher_name}' AND package.state='deleted'; '''.format(publisher_name=publisher_name) try: datasets = engine.execute(deleted_datasets) num_deleted_datasets = datasets.rowcount except Exception, e: raise logic.ActionError('Error executing sql: %s' % e)
def report_data_get(context=None, data_dict=None): """ Returns the data for the report The data may have been cached in the database or may have been generated on demand so the date when the data was generated is also returned :param id: The name of the report :type id: string :param options: Dictionary of options to pass to the report (optional) :type options: dict :returns: A list containing the data and the date on which it was created :rtype: list """ logic.check_access('report_data_get', context, data_dict) id = logic.get_or_bust(data_dict, 'id') options = data_dict.get('options', {}) report = ReportRegistry.instance().get_report(id) data, date = report.get_fresh_report(**options) return data, date.isoformat()
def taxonomy_delete(context, data_dict): """ Delete the specific taxonomy, and as a result, all of the terms within it. :returns: The newly deleted taxonomy :rtype: A dictionary """ _check_access('taxonomy_delete', context, data_dict) model = context['model'] name = logic.get_or_bust(data_dict, 'id') taxonomy = Taxonomy.get(name) if not taxonomy: raise logic.NotFound() terms = model.Session.query(TaxonomyTerm)\ .filter(TaxonomyTerm.taxonomy == taxonomy) map(model.Session.delete, terms.all()) model.Session.delete(taxonomy) model.Session.commit() return taxonomy.as_dict()
def harvest_send_job_to_gather_queue(context, data_dict): ''' Sends a harvest job to the gather queue. :param id: the id of the harvest job :type id: string ''' log.info('Send job to gather queue: %r', data_dict) job_id = logic.get_or_bust(data_dict, 'id') job = toolkit.get_action('harvest_job_show')(context, {'id': job_id}) check_access('harvest_send_job_to_gather_queue', context, job) # gather queue publisher = get_gather_publisher() # Check the source is active source = harvest_source_show(context, {'id': job['source_id']}) if not source['active']: raise toolkit.ValidationError('Source is not active') job_obj = HarvestJob.get(job['id']) job_obj.status = job['status'] = u'Running' job_obj.save() publisher.send({'harvest_job_id': job['id']}) log.info('Sent job %s to the gather queue', job['id']) return harvest_job_dictize(job_obj, context)
def harvest_send_job_to_gather_queue(context, data_dict): ''' Sends a harvest job to the gather queue. :param id: the id of the harvest job :type id: string ''' log.info('Send job to gather queue: %r', data_dict) job_id = logic.get_or_bust(data_dict, 'id') job = toolkit.get_action('harvest_job_show')( context, {'id': job_id}) check_access('harvest_send_job_to_gather_queue', context, job) # gather queue publisher = get_gather_publisher() # Check the source is active source = harvest_source_show(context, {'id': job['source_id']}) if not source['active']: raise toolkit.ValidationError('Source is not active') job_obj = HarvestJob.get(job['id']) job_obj.status = job['status'] = u'Running' job_obj.save() publisher.send({'harvest_job_id': job['id']}) log.info('Sent job %s to the gather queue', job['id']) return harvest_job_dictize(job_obj, context)
def comment_update(context, data_dict): model = context['model'] logic.check_access("comment_update", context, data_dict) cid = logic.get_or_bust(data_dict, 'id') comment = comment_model.Comment.get(cid) if not comment: abort(404) # Validate that we have the required fields. if not all([data_dict.get('comment')]): raise logic.ValidationError("Comment text is required") # Cleanup the comment cleaned_comment = util.clean_input(data_dict.get('comment')) comment.subject = data_dict.get('subject') comment.comment = cleaned_comment comment.modified_date = datetime.datetime.now() model.Session.add(comment) model.Session.commit() return comment.as_dict()
def dge_organization_publisher(context, data_dict=None): try: model = context['model'] id = logic.get_or_bust(data_dict, 'id') group = model.Group.get(id) context['group'] = group if group is None: raise NotFound if not group.is_organization: raise NotFound group_dict = model_dictize.group_dictize( group, context, packages_field='dataset_count', include_tags=False, include_extras=True, include_groups=False, include_users=False, ) group_plugin = lib_plugins.lookup_group_plugin(group_dict['type']) schema = logic.schema.default_show_group_schema() group_dict, errors = lib_plugins.plugin_validate( group_plugin, context, group_dict, schema, 'organization_show') return group_dict except: return {}
def get_products_by_survey(context, data_dict): # noinspection PyUnresolvedReferences """ Find published products which have the given survey as a source :param surveyID: ID of the survey for which to find related products :type surveyID: str :return: list of related products """ survey_id = get_or_bust(data_dict, 'surveyID') lc = ckanapi.LocalCKAN(context=context) results = lc.action.package_search( q='survey_source_codes:{survey_id} AND ' 'last_publish_status_code:12'.format(survey_id=survey_id), rows=1000) products = [] results = results.get('results', []) for result in results: title = result.get(u'title') title = title if title else {u'en': u'', u'fr': u''} product_id = result.get(u'product_id_new') try: url = get_product_url(context, {u'productId': product_id}) except NotFound: url = {u'en': u'', u'fr': u''} products.append({ u'product_id': product_id, u'title': title, u'url': url }) return products
def get_subject_codesets(context, data_dict): """ Returns all subject codesets. :param limit: Number of results to return. :type limit: int :param start: Number of results to skip. :type start: int :rtype: list of dicts """ lc = ckanapi.LocalCKAN(context=context) # Sort would perform better, but this will be easier # for client to implement. limit = int(logic.get_or_bust(data_dict, 'limit')) start = int(logic.get_or_bust(data_dict, 'start')) results = lc.action.package_search( q='dataset_type:subject', rows=limit, start=start, fl=( 'name', 'title' ) ) def _massage(s): chunked = textwrap.wrap(s['subject_code'], 2) return ( chunked[-1], chunked[-2] if len(chunked) > 1 else None, s['title'] ) return { 'count': results['count'], 'limit': limit, 'start': start, 'results': [{ 'subject_code': rr[0], 'parent_subject_code': rr[1], 'title': rr[2], } for rr in (_massage(r) for r in results['results'])] }
def get_comment(data_dict): import ckan.logic as logic import ckanext.ytp.comments.model as model comment = data_dict.get('comment') if not comment: id = logic.get_or_bust(data_dict, 'id') comment = model.Comment.get(id) return comment
def comment_show(context, data_dict): id = logic.get_or_bust(data_dict, 'id') comment = comment_model.Comment.get(id) if not comment: abort(404) logic.check_access("comment_show", context, data_dict) data_dict['comment'] = comment return comment.as_dict()
def get_comment(data_dict): import ckan.logic as logic import ckanext.ceh_comment.model as model comment = data_dict.get('comment') if not comment: id = logic.get_or_bust(data_dict, 'id') comment = model.Comment.get(id) return comment
def comment_show(context, data_dict): id = logic.get_or_bust(data_dict, 'id') comment = comment_model.Comment.get(id) if not comment: abort(404) logic.check_access("comment_show", context, data_dict) data_dict['comment'] = comment return comment.as_dict()
def spatialingestor_job_submit(context, data): res_id, job_type = get_or_bust(data, ['resource_id', 'job_type']) if job_type == 'ingest': return auth_create.resource_create(context, {'id': res_id}) elif job_type == 'purge': return auth_delete.resource_delete(context, {'id': res_id}) else: return False
def forum_create_thread(context, data_dict): """ Create new thread on board data_dict: board_id - Board id for thread name - Thread name content - Thread text can_post - False if thread is closed for posts :return Thread """ thread = Thread() thread.board_id = logic.get_or_bust(data_dict, 'board_id') thread.name = logic.get_or_bust(data_dict, 'name') thread.content = strip_tags(logic.get_or_bust(data_dict, 'content')) thread.author = logic.get_or_bust(context, 'auth_user_obj') if 'can_post' in data_dict and not data_dict['can_post']: thread.can_post = False thread.save() return thread
def _action_find_dataset(context, data_dict): ''' common code for actions that need to check for a dataset based on the dataset type and organization name or id ''' dataset_type = get_or_bust(data_dict, 'dataset_type') owner_org = get_or_bust(data_dict, 'owner_org') try: geno = get_geno(dataset_type) except RecombinantException: raise ValidationError( {'dataset_type': _("Recombinant dataset type not found")}) lc = LocalCKAN(username=context['user']) result = lc.action.package_search(q="type:%s organization:%s" % (dataset_type, owner_org), rows=2) return lc, geno, result['results']
def _action_find_dataset(context, data_dict): ''' common code for actions that need to check for a dataset based on the dataset type and organization name or id ''' dataset_type = get_or_bust(data_dict, 'dataset_type') owner_org = get_or_bust(data_dict, 'owner_org') try: geno = get_geno(dataset_type) except RecombinantException: raise ValidationError({'dataset_type': _("Recombinant dataset type not found")}) lc = LocalCKAN(username=context['user']) result = lc.action.package_search( q="type:%s organization:%s" % (dataset_type, owner_org), rows=2) return lc, geno, result['results']
def comment_show(context, data_dict): id = logic.get_or_bust(data_dict, 'id') comment = comment_model.Comment.get(id) if not comment: abort(404) # Se comenta para evitar que solo usuarios conectados puedan realizar comentarios #logic.check_access("comment_show", context, data_dict) data_dict['comment'] = comment return comment.as_dict()
def purge_revision_history(context, data_dict): ''' Purge a given publisher's unused revision history. :param group: the name or id of the publisher :type group: string :returns: number of resources and revisions purged. :rtype: dictionary ''' logic.check_access('purge_revision_history', context, data_dict) model = context['model'] engine = model.meta.engine group_id = logic.get_or_bust(data_dict, 'group') group = model.Group.get(group_id) if not group: raise logic.NotFound('Publisher {0} not found'.format(group_id)) RESOURCE_IDS_SQL = ''' SELECT resource.id FROM resource JOIN resource_group ON resource.resource_group_id = resource_group.id JOIN member ON member.table_id = resource_group.package_id JOIN "group" ON "group".id = member.group_id WHERE "group".name = %s AND "group".type = 'organization' AND member.table_name = 'package' AND resource.state = 'deleted' ''' DELETE_REVISIONS_SQL = ''' DELETE FROM resource_revision WHERE id IN ({sql}) '''.format(sql=RESOURCE_IDS_SQL) # Not necessary to use a sub-select, but it allows re-use of sql statement # and this isn't performance critical code. DELETE_RESOURCES_SQL = ''' DELETE FROM resource WHERE id IN ({sql}) '''.format(sql=RESOURCE_IDS_SQL) try: number_revisions_deleted = engine.execute( DELETE_REVISIONS_SQL, group.name ).rowcount number_resources_deleted = engine.execute( DELETE_RESOURCES_SQL, group.name ).rowcount except Exception, e: raise logic.ActionError('Error executing sql: %s' % e)
def activity_list_from_user_since(context, data_dict): '''Return the activity stream of all recently added or changed packages. :param since_time: starting date/time Limited to 31 records (configurable via the ckan.activity_list_hard_limit setting) but may be called repeatedly with the timestamp of the last record to collect all activities. :param user_id: the user of the requested activity list :rtype: list of dictionaries ''' since = get_or_bust(data_dict, 'since_time') user_id = get_or_bust(data_dict, 'user_id') try: since_time = isodate(since, None) except Invalid, e: raise ValidationError({'since_time':e.error})
def report_data_get(context=None, data_dict=None): id = get_or_bust(data_dict, 'id') report = ReportRegistry.instance().get_report(id) if hasattr(report, 'authorize'): user = context['auth_user_obj'] options = data_dict['options'] if not report.authorize(user, options): return {'success': False} return {'success': True}
def get_survey_codesets(context, data_dict): # noinspection PyUnresolvedReferences """ Returns all survey codesets. :param limit: Number of results to return. :type limit: int :param start: Number of results to skip. :type start: int :rtype: list of dicts """ lc = ckanapi.LocalCKAN(context=context) # Sort would perform better, but this will be easier # for client to implement. limit = int(get_or_bust(data_dict, 'limit')) start = int(get_or_bust(data_dict, 'start')) results = lc.action.package_search( q='dataset_type:survey', rows=limit, start=start, sort='survey_code asc', fl=( 'name', 'title' ) ) return { 'count': results['count'], 'limit': limit, 'start': start, 'results': [{ 'survey_code': r['product_id_new'], 'title': r['title'], } for r in results['results']] }
def doc_show(context, data_dict): """ Show the extra information about the user. Paramenters: id the user id or username :returns: a user_extra dict containing basic and extra information """ id = get_or_bust(data_dict, 'id') googledoc = GoogleDoc.get(id) if googledoc: return googledoc.as_dict() else: raise NotFound
def certificate_assign(context, data_dict): # Raises toolkit.NotAuthorized if user is not authorised to perform an update. # Avoiding it for now as a disabled sysadmin user is detected. toolkit.check_access('package_update', context, {'id': data_dict['id']}) # `get_or_bust` documentation: # http://ckan.readthedocs.org/en/ckan-2.3.1/extensions/plugins-toolkit.html#ckan.plugins.toolkit.get_or_bust id = logic.get_or_bust(data_dict, 'id') certificate = logic.get_or_bust(data_dict, 'certificate') model = context['model'] package = model.Package.get(id) # Raising 404s should ideally not be handled manually, but couldn't find # something like get_or_404. if not package: raise toolkit.ObjectNotFound model.repo.new_revision() package.extras['certificate'] = json.dumps(certificate) model.Session.commit() return logic.get_action('package_show')(context, {'id': id})
def publisher_show(context, data_dict): """Shows publisher details. Based on group_show, but has parent group, as well as the child groups. May raise NotFound or NotAuthorized. """ group_dict = group_show(context, data_dict) model = context["model"] id = get_or_bust(data_dict, "id") group = model.Group.get(id) parent_groups = publisher_lib.get_parents(group) group_dict["parent_group"] = parent_groups[0].name if parent_groups else None return group_dict
def get_available_roles(context, data_dict=None): roles = logic.get_action("member_roles_list")(context, {}) # Remove member role from the list roles = [role for role in roles if role['value'] != 'member'] # If organization has no associated admin, then role editor is not # available organization_id = logic.get_or_bust(data_dict, 'organization_id') if organization_id: if get_organization_admins(organization_id): roles = [role for role in roles if role['value'] != 'editor'] return roles else: return None
def publisher_show(context, data_dict): '''Shows publisher details. Based on group_show, but has parent group, as well as the child groups. May raise NotFound or NotAuthorized. ''' group_dict = group_show(context, data_dict) model = context['model'] id = get_or_bust(data_dict, 'id') group = model.Group.get(id) parent_groups = publisher_lib.get_parents(group) group_dict['parent_group'] = {'id': parent_groups[0].id, 'name': parent_groups[0].name} \ if parent_groups else None return group_dict
def register_survey(context, data_dict): # noinspection PyUnresolvedReferences """ Registers a new survey, then tags it with given subject codes. :param productId: Product ID of new survey :type productId: str :param subjectCodes: (optional) List of subject codes :type subjectCodes: list of str :param titleEn: (optional) English title :type titleEn: str :param titleFr: (optional) French title :type titleFr: str :return: new package :rtype: dict :raises: ValidationError """ product_id = get_or_bust(data_dict, 'productId') subject_codes = data_dict.get('subjectCodes', []) if not isinstance(subject_codes, list): try: subject_codes = json.loads(subject_codes) except (TypeError, ValueError) as e: raise ValidationError(e) title_en = data_dict.get('titleEn', 'title for survey-'+product_id) title_fr = data_dict.get('titleFr', 'titre pour survey-'+product_id) lc = ckanapi.LocalCKAN(context=context) new_package = lc.action.package_create( **{ u'owner_org': u'statcan', u'private': False, u'name': u'survey-{0}'.format(product_id), u'type': u'survey', u'title': { u'en': title_en, u'fr': title_fr }, u'product_id_new': product_id, u'subject_codes': subject_codes } ) return new_package
def report_refresh(context=None, data_dict=None): """ Causes the cached data of the report to be refreshed :param id: The name of the report :type id: string :param options: Dictionary of options to pass to the report :type options: dict """ logic.check_access('report_refresh', context, data_dict) id = logic.get_or_bust(data_dict, 'id') options = data_dict.get('options') report = ReportRegistry.instance().get_report(id) report.refresh_cache(options)
def comment_delete(context, data_dict): model = context['model'] logic.check_access("comment_delete", context, data_dict) # Comment should either be set state=='deleted' if no children, # otherwise content should be set to withdrawn text id = logic.get_or_bust(data_dict, 'id') comment = comment_model.Comment.get(id) if not comment: abort(404) comment.state = 'deleted' model.Session.add(comment) model.Session.commit() return {'success': True}
def comment_delete(context, data_dict): model = context['model'] user = context['user'] userobj = model.User.get(user) # If sysadmin. if new_authz.is_sysadmin(user): return {'success': True} cid = logic.get_or_bust(data_dict, 'id') comment = comment_model.Comment.get(cid) if not comment: return {'success': False, 'msg': _('Comment does not exist')} if comment.user_id is not userobj.id: return {'success': False, 'msg': _('User is not the author of the comment')} return {'success': True}
def comment_update(context, data_dict): user = context['user'] model = context['model'] userobj = model.User.get(user) if not userobj: log.debug("User is not logged in") return {'success': False, 'msg': _('You must be logged in to add a comment')} cid = logic.get_or_bust(data_dict, 'id') comment = comment_model.Comment.get(cid) if not comment: return {'success': False, 'msg': _('Comment does not exist')} if comment.user_id != userobj.id: return {'success': False, 'msg': _('User is not the author of the comment')} return {'success': True}