def resource_create(context, data_dict):
    '''
      .. sealso https://github.com/ckan/ckan/blob/master/ckan/logic/action/create.py
    '''
    model = context['model']
    user = context['user']

    package_id = _get_or_bust(data_dict, 'package_id')
    _get_or_bust(data_dict, 'url')

    pkg_dict = _get_action('package_show')(
        dict(context, return_type='dict'),
        {'id': package_id})

    _check_access('resource_create', context, data_dict)

    for plugin in plugins.PluginImplementations(plugins.IResourceController):
        plugin.before_create(context, data_dict)

    if not 'resources' in pkg_dict:
        pkg_dict['resources'] = []

    upload = uploader.get_resource_uploader(data_dict)
    pkg_dict['resources'].append(data_dict)
    try:
        context['defer_commit'] = True
        context['use_cache'] = False
        _get_action('package_update')(context, pkg_dict)
        context.pop('defer_commit')
    except ValidationError, e:
        errors = e.error_dict['resources'][-1]
        raise ValidationError(errors)
Beispiel #2
0
def hdx_mark_qa_completed(context, data_dict):
    _check_access('hdx_mark_qa_completed', context, data_dict)
    _get_or_bust(data_dict, 'qa_completed')

    context['allow_qa_completed_field'] = True
    context[BATCH_MODE] = BATCH_MODE_KEEP_OLD

    return _get_action('package_patch')(context, data_dict)
Beispiel #3
0
def group_patch(context, data_dict):
    '''Patch a group

    :param id: the id or name of the group
    :type id: string

    The difference between the update and patch methods is that the patch will
    perform an update of the provided parameters, while leaving all other
    parameters unchanged, whereas the update methods deletes all parameters
    not explicitly provided in the data_dict
    '''
    _check_access('group_patch', context, data_dict)

    show_context = {
        'model': context['model'],
        'session': context['session'],
        'user': context['user'],
        'auth_user_obj': context['auth_user_obj'],
    }

    group_dict = _get_action('group_show')(show_context, {
        'id': _get_or_bust(data_dict, 'id')
    })

    patched = dict(group_dict)
    patched.pop('display_name', None)
    patched.update(data_dict)
    return _update.group_update(context, patched)
Beispiel #4
0
def package_marsavin_save(pkg_dict, context):
    package_id = _get_or_bust(pkg_dict, 'id')

    pkg_marsavin_dict = {
        "package_id": pkg_dict["id"],
        "associated_tasks": pkg_dict["associated_tasks"],
        "collection_period": pkg_dict["collection_period"],
        "geographical_area": pkg_dict["geographical_area"],
        "number_of_instances": pkg_dict["number_of_instances"],
        "pkg_description": pkg_dict["pkg_description"],
        "number_of_attributes": pkg_dict["number_of_attributes"],
        "has_missing_values": pkg_dict["has_missing_values"]
    }
    if pkg_dict["creation_date"]:
        pkg_marsavin_dict["creation_date"] = pkg_dict["creation_date"]
    if pkg_dict["expiry_date"]:
        pkg_marsavin_dict["expiry_date"] = pkg_dict["expiry_date"]

    entity = PackageMarsavin.by_package_id(package_id)

    if entity:
        pkg_marsavin_dict["id"] = entity.id

    package_marsavin = d.table_dict_save(pkg_marsavin_dict, PackageMarsavin,
                                         context)
    return package_marsavin
Beispiel #5
0
def package_patch(context, data_dict):
    '''Patch a dataset (package).

    :param id: the id or name of the dataset
    :type id: string

    The difference between the update and patch methods is that the patch will
    perform an update of the provided parameters, while leaving all other
    parameters unchanged, whereas the update methods deletes all parameters
    not explicitly provided in the data_dict

    You must be authorized to edit the dataset and the groups that it belongs
    to.
    '''
    _check_access('package_patch', context, data_dict)

    show_context = {
        'model': context['model'],
        'session': context['session'],
        'user': context['user'],
        'auth_user_obj': context['auth_user_obj'],
        }

    package_dict = _get_action('package_show')(
        show_context,
        {'id': _get_or_bust(data_dict, 'id')})

    patched = dict(package_dict)
    patched.update(data_dict)
    patched['id'] = package_dict['id']
    return _update.package_update(context, patched)
Beispiel #6
0
def user_marsavin_save(user_dict, context):
    user_id = _get_or_bust(user_dict, 'id')

    user_marsavin_dict = {
        "user_id": user_id,
    }
    try:
        user_marsavin_dict["allow_marketting_emails"] = user_dict[
            "allow_marketting_emails"]
    except KeyError:
        pass
    try:
        user_marsavin_dict["user-terms-agree"] = user_dict["user-terms-agree"]
    except KeyError:
        pass
    try:
        user_marsavin_dict["uploader-terms-agree"] = user_dict[
            "uploader-terms-agree"]
    except KeyError:
        pass

    entity = UserMarsavin.by_user_id(user_id)

    if entity:
        user_marsavin_dict["id"] = entity.id

    user_marsavin = d.table_dict_save(user_marsavin_dict, UserMarsavin,
                                      context)
    return user_marsavin
Beispiel #7
0
def organization_patch(context: Context,
                       data_dict: DataDict) -> ActionResult.OrganizationPatch:
    '''Patch an organization

    :param id: the id or name of the organization
    :type id: string

    The difference between the update and patch methods is that the patch will
    perform an update of the provided parameters, while leaving all other
    parameters unchanged, whereas the update methods deletes all parameters
    not explicitly provided in the data_dict
    '''
    _check_access('organization_patch', context, data_dict)

    show_context: Context = {
        'model': context['model'],
        'session': context['session'],
        'user': context['user'],
        'auth_user_obj': context['auth_user_obj'],
    }

    organization_dict = _get_action('organization_show')(
        show_context, {
            'id': _get_or_bust(data_dict, 'id')
        })

    patched = dict(organization_dict)
    patched.pop('display_name', None)
    patched.update(data_dict)

    patch_context = context.copy()
    patch_context['allow_partial_update'] = True
    return _update.organization_update(patch_context, patched)
Beispiel #8
0
def resource_patch(context: Context,
                   data_dict: DataDict) -> ActionResult.ResourcePatch:
    '''Patch a resource

    :param id: the id of the resource
    :type id: string

    The difference between the update and patch methods is that the patch will
    perform an update of the provided parameters, while leaving all other
    parameters unchanged, whereas the update methods deletes all parameters
    not explicitly provided in the data_dict
    '''
    _check_access('resource_patch', context, data_dict)

    show_context: Context = {
        'model': context['model'],
        'session': context['session'],
        'user': context['user'],
        'auth_user_obj': context['auth_user_obj'],
    }

    resource_dict = _get_action('resource_show')(
        show_context, {
            'id': _get_or_bust(data_dict, 'id')
        })

    patched = dict(resource_dict)
    patched.update(data_dict)
    return _update.resource_update(context, patched)
Beispiel #9
0
def group_patch(context, data_dict):
    '''Patch a group

    :param id: the id or name of the group
    :type id: string

    The difference between the update and patch methods is that the patch will
    perform an update of the provided parameters, while leaving all other
    parameters unchanged, whereas the update methods deletes all parameters
    not explicitly provided in the data_dict
    '''
    _check_access('group_patch', context, data_dict)

    show_context = {
        'model': context['model'],
        'session': context['session'],
        'user': context['user'],
        'auth_user_obj': context['auth_user_obj'],
        }

    group_dict = _get_action('group_show')(
        show_context,
        {'id': _get_or_bust(data_dict, 'id')})

    patched = dict(group_dict)
    patched.pop('display_name', None)
    patched.update(data_dict)
    return _update.group_update(context, patched)
Beispiel #10
0
def package_patch(context, data_dict):
    '''Patch a dataset (package).

    :param id: the id or name of the dataset
    :type id: string

    The difference between the update and patch methods is that the patch will
    perform an update of the provided parameters, while leaving all other
    parameters unchanged, whereas the update methods deletes all parameters
    not explicitly provided in the data_dict

    You must be authorized to edit the dataset and the groups that it belongs
    to.
    '''
    _check_access('package_patch', context, data_dict)

    show_context = {
        'model': context['model'],
        'session': context['session'],
        'user': context['user'],
        'auth_user_obj': context['auth_user_obj'],
    }

    package_dict = _get_action('package_show')(
        show_context, {
            'id': _get_or_bust(data_dict, 'id')
        })

    patched = dict(package_dict)
    patched.update(data_dict)
    patched['id'] = package_dict['id']
    return _update.package_update(context, patched)
def resource_create(context, data_dict):
  if not tk.asbool(config.get('ckan.cloud_storage_enable')) or data_dict.get('url'):
    return origin.resource_create(context, data_dict)
  
  model = context['model']
  user = context['user']

  package_id = _get_or_bust(data_dict, 'package_id')
  data_dict.pop('package_id')

  pkg_dict = _get_action('package_show')(context, {'id': package_id})

  _check_access('resource_create', context, data_dict)

  if not 'resources' in pkg_dict:
    pkg_dict['resources'] = []

  upload = uploader.S3Upload(data_dict)

  pkg_dict['resources'].append(data_dict)

  try:
    context['defer_commit'] = True
    context['use_cache'] = False
    _get_action('package_update')(context, pkg_dict)
    context.pop('defer_commit')
  except ValidationError, e:
    errors = e.error_dict['resources'][-1]
    raise ValidationError(errors)
Beispiel #12
0
def package_marsavin_delete(pkg_dict):
    package_id = _get_or_bust(pkg_dict, 'id')

    entity = PackageMarsavin.by_package_id(package_id)

    if entity:
        # object with package id may not be found, we don't really care since
        # it may only be found if the package exists
        entity.delete()
Beispiel #13
0
def package_marsavin_load(pkg_dict, cached_entity=None):
    package_id = _get_or_bust(pkg_dict, 'id')

    entity_dict = {
        "associated_tasks": u'',
        "collection_period": u'',
        "geographical_area": u'',
        "number_of_instances": u'',
        "pkg_description": u'',
        "number_of_attributes": u'',
        "creation_date": u'',
        "expiry_date": u'',
        "has_missing_values": u''
    }

    if isinstance(cached_entity, PackageMarsavin):
        entity = cached_entity
    else:
        entity = PackageMarsavin.by_package_id(package_id)

    if entity:
        entity_dict = {
            "associated_tasks": entity.associated_tasks,
            "collection_period": entity.collection_period,
            "geographical_area": entity.geographical_area,
            "number_of_instances": entity.number_of_instances,
            "pkg_description": entity.pkg_description,
            "number_of_attributes": entity.number_of_attributes,
            "has_missing_values": entity.has_missing_values,
            "creation_date": "",
            "expiry_date": ""
        }
        if entity.creation_date:
            if isinstance(entity.creation_date, date):
                entity_dict["creation_date"] = entity.creation_date.isoformat()
            else:
                entity_dict["creation_date"] = entity.creation_date

        if entity.expiry_date:
            if isinstance(entity.expiry_date, date):
                entity_dict["expiry_date"] = entity.expiry_date.isoformat()
            else:
                entity_dict["expiry_date"] = entity.expiry_date

    pkg_dict.update(entity_dict)
def resource_update(context, data_dict):
    '''Update a resource.
    ::seealso https://github.com/ckan/ckan/blob/master/ckan/logic/action/update.py
    '''
    model = context['model']
    user = context['user']
    id = _get_or_bust(data_dict, "id")

    resource = model.Resource.get(id)
    previous_s3_object_url =   resource.url
    context["resource"] = resource

    if not resource:
        log.error('Could not find resource ' + id)
        raise NotFound(_('Resource was not found.'))

    _check_access('resource_update', context, data_dict)
    del context["resource"]

    package_id = resource.package.id
    pkg_dict = _get_action('package_show')(dict(context, return_type='dict'),
        {'id': package_id})

    for n, p in enumerate(pkg_dict['resources']):
        if p['id'] == id:
            break
    else:
        log.error('Could not find resource ' + id)
        raise NotFound(_('Resource was not found.'))

    for plugin in plugins.PluginImplementations(plugins.IResourceController):
        plugin.before_update(context, pkg_dict['resources'][n], data_dict)

    upload = uploader.get_resource_uploader(data_dict)

    pkg_dict['resources'][n] = data_dict
    try:
        context['defer_commit'] = True
        context['use_cache'] = False
        updated_pkg_dict = _get_action('package_update')(context, pkg_dict)
        context.pop('defer_commit')
    except ValidationError, e:
        errors = e.error_dict['resources'][n]
        raise ValidationError(errors)
Beispiel #15
0
def resource_patch(context, data_dict):
    '''
    Cloned from core. It adds a 'no_compute_extra_hdx_show_properties' in contexts to
    make the update faster (less computation in the custom package_show)
    Also used to parse validation parameters (SKIP_VALIDATION) for special cases.

    Patch a resource

    :param id: the id of the resource
    :type id: string

    The difference between the update and patch methods is that the patch will
    perform an update of the provided parameters, while leaving all other
    parameters unchanged, whereas the update methods deletes all parameters
    not explicitly provided in the data_dict
    '''
    _check_access('resource_patch', context, data_dict)

    context['no_compute_extra_hdx_show_properties'] = True
    process_batch_mode(context, data_dict)
    process_skip_validation(context, data_dict)

    show_context = {
        'model':
        context['model'],
        'session':
        context['session'],
        'user':
        context['user'],
        'auth_user_obj':
        context['auth_user_obj'],
        'no_compute_extra_hdx_show_properties':
        context.get('no_compute_extra_hdx_show_properties')
    }

    resource_dict = _get_action('resource_show')(
        show_context, {
            'id': _get_or_bust(data_dict, 'id')
        })

    patched = dict(resource_dict)
    patched.update(data_dict)
    return _update.resource_update(context, patched)
Beispiel #16
0
def package_patch(context: Context,
                  data_dict: DataDict) -> ActionResult.PackagePatch:
    '''Patch a dataset (package).

    :param id: the id or name of the dataset
    :type id: string

    The difference between the update and patch methods is that the patch will
    perform an update of the provided parameters, while leaving all other
    parameters unchanged, whereas the update methods deletes all parameters
    not explicitly provided in the data_dict.

    You are able to partially update and/or create resources with
    package_patch. If you are updating existing resources be sure to provide
    the resource id. Existing resources excluded from the package_patch
    data_dict will be removed. Resources in the package data_dict without
    an id will be treated as new resources and will be added. New resources
    added with the patch method do not create the default views.

    You must be authorized to edit the dataset and the groups that it belongs
    to.
    '''
    _check_access('package_patch', context, data_dict)

    show_context: Context = {
        'model': context['model'],
        'session': context['session'],
        'user': context['user'],
        'auth_user_obj': context['auth_user_obj'],
        'ignore_auth': context.get('ignore_auth', False),
        'for_update': True
    }

    package_dict = _get_action('package_show')(
        show_context, {
            'id': _get_or_bust(data_dict, 'id')
        })

    patched = dict(package_dict)
    patched.update(data_dict)
    patched['id'] = package_dict['id']
    return _update.package_update(context, patched)
Beispiel #17
0
def package_patch(context, data_dict):
    '''
    Cloned from core. It's used to parse validation parameters (SKIP_VALIDATION) for special cases
    Also, changed so that it now calls "our" package_update instead of the core package_update.

    Patch a dataset (package).

    :param id: the id or name of the dataset
    :type id: string

    The difference between the update and patch methods is that the patch will
    perform an update of the provided parameters, while leaving all other
    parameters unchanged, whereas the update methods deletes all parameters
    not explicitly provided in the data_dict

    You must be authorized to edit the dataset and the groups that it belongs
    to.
    '''
    process_skip_validation(context, data_dict)

    # Original package patch from CKAN
    _check_access('package_patch', context, data_dict)

    show_context = {
        'model': context['model'],
        'session': context['session'],
        'user': context['user'],
        'auth_user_obj': context['auth_user_obj'],
    }

    package_dict = _get_action('package_show')(
        show_context, {
            'id': _get_or_bust(data_dict, 'id')
        })

    patched = dict(package_dict)
    patched.update(data_dict)
    patched['id'] = package_dict['id']

    # slightly modified to call "our" package_update
    return package_update(context, patched)
Beispiel #18
0
def resource_update(context, data_dict):
    if not tk.asbool(
            config.get('ckan.cloud_storage_enable')) or data_dict.get('url'):
        return origin.resource_update(context, data_dict)

    model = context['model']
    user = context['user']
    id = _get_or_bust(data_dict, "id")

    resource = model.Resource.get(id)
    context["resource"] = resource

    if not resource:
        log.error('Could not find resource ' + id)
        raise NotFound(_('Resource was not found.'))

    _check_access('resource_update', context, data_dict)
    del context["resource"]

    package_id = resource.resource_group.package.id
    pkg_dict = _get_action('package_show')(context, {'id': package_id})

    for n, p in enumerate(pkg_dict['resources']):
        if p['id'] == id:
            break
    else:
        log.error('Could not find resource ' + id)
        raise NotFound(_('Resource was not found.'))

    upload = uploader.S3Upload(data_dict)

    pkg_dict['resources'][n] = data_dict

    try:
        context['defer_commit'] = True
        context['use_cache'] = False
        pkg_dict = _get_action('package_update')(context, pkg_dict)
        context.pop('defer_commit')
    except ValidationError, e:
        errors = e.error_dict['resources'][n]
        raise ValidationError(errors)
Beispiel #19
0
def resource_update(context, data_dict):
    if not config.get('ckan.cloud_storage_enable') or data_dict.get('url'):
        return origin.resource_update(context, data_dict)

    model = context['model']
    user = context['user']
    id = _get_or_bust(data_dict, "id")

    resource = model.Resource.get(id)
    context["resource"] = resource

    if not resource:
        log.error('Could not find resource ' + id)
        raise NotFound(_('Resource was not found.'))

    _check_access('resource_update', context, data_dict)
    del context["resource"]

    package_id = resource.resource_group.package.id
    pkg_dict = _get_action('package_show')(context, {'id': package_id})

    for n, p in enumerate(pkg_dict['resources']):
        if p['id'] == id:
            break
    else:
        log.error('Could not find resource ' + id)
        raise NotFound(_('Resource was not found.'))

    upload = uploader.S3Upload(data_dict)

    pkg_dict['resources'][n] = data_dict

    try:
        context['defer_commit'] = True
        context['use_cache'] = False
        pkg_dict = _get_action('package_update')(context, pkg_dict)
        context.pop('defer_commit')
    except ValidationError, e:
        errors = e.error_dict['resources'][n]
        raise ValidationError(errors)
Beispiel #20
0
def package_patch(context, data_dict):
    '''Patch a dataset (package).

    :param id: the id or name of the dataset
    :type id: string

    The difference between the update and patch methods is that the patch will
    perform an update of the provided parameters, while leaving all other
    parameters unchanged, whereas the update methods deletes all parameters
    not explicitly provided in the data_dict.

    You are able to partially update and/or create resources with
    package_patch. If you are updating existing resources be sure to provide
    the resource id. Existing resources excluded from the package_patch
    data_dict will be removed. Resources in the package data_dict without
    an id will be treated as new resources and will be added. New resources
    added with the patch method do not create the default views.

    You must be authorized to edit the dataset and the groups that it belongs
    to.
    '''
    _check_access('package_patch', context, data_dict)

    show_context = {
        'model': context['model'],
        'session': context['session'],
        'user': context['user'],
        'auth_user_obj': context['auth_user_obj'],
    }

    package_dict = _get_action('package_show')(
        show_context,
        {'id': _get_or_bust(data_dict, 'id')})

    patched = dict(package_dict)
    patched.update(data_dict)
    patched['id'] = package_dict['id']
    return _update.package_update(context, patched)
def resource_delete(context, data_dict):
  ''' Delete a resource.
    .. seealso https://github.com/ckan/ckan/blob/master/ckan/logic/action/delete.py
  '''
  model = context['model']
  user = context['user']
  id = _get_or_bust(data_dict, "id")
  log.debug(id)
  resource = model.Resource.get(id)
  previous_s3_object_url =   resource.url
  ################################################################################################################
  if tk.asbool(config.get('ckanext.cloud_storage.enable')) and previous_s3_object_url.startswith("https://s3.amazonaws.com/") :
    log.debug('Deleting Remote Resource')
    log.debug(previous_s3_object_url)
    context["resource"] = resource

    if not resource:
      log.error('Could not find resource ' + id)
      raise NotFound(_('Resource was not found.'))

    _check_access('resource_delete', context, data_dict)
    package_id = resource.package.id
    pkg_dict = _get_action('package_show')(context, {'id': package_id})

    for n, p in enumerate(pkg_dict['resources']):
      if p['id'] == id:
          break
    else:
      log.error('Could not find resource ' + id)
      raise NotFound(_('Resource was not found.'))

    upload = uploader.get_resource_uploader(data_dict)
    upload.delete(previous_s3_object_url)
  else:
    log.debug('Plugin Not Enabled or External Link')
  ################################################################################################################
  return origin.resource_delete(context, data_dict)