Ejemplo n.º 1
0
def showcase_package_association_create(context, data_dict):
    '''Create an association between a showcase and a package.

    :param showcase_id: id or name of the showcase to associate
    :type showcase_id: string

    :param package_id: id or name of the package to associate
    :type package_id: string
    '''

    toolkit.check_access('ckanext_showcase_package_association_create',
                         context, data_dict)

    # validate the incoming data_dict
    validated_data_dict, errors = validate(
        data_dict, showcase_package_association_create_schema(), context)

    if errors:
        raise toolkit.ValidationError(errors)

    package_id, showcase_id = toolkit.get_or_bust(validated_data_dict,
                                                  ['package_id',
                                                   'showcase_id'])

    if ShowcasePackageAssociation.exists(package_id=package_id,
                                         showcase_id=showcase_id):
        raise toolkit.ValidationError("ShowcasePackageAssociation with package_id '{0}' and showcase_id '{1}' already exists.".format(package_id, showcase_id),
                                      error_summary=u"The dataset, {0}, is already in the showcase".format(convert_package_name_or_id_to_title_or_name(package_id, context)))

    # create the association
    return ShowcasePackageAssociation.create(package_id=package_id,
                                             showcase_id=showcase_id)
Ejemplo n.º 2
0
def showcase_package_association_create(context, data_dict):
    '''Create an association between a showcase and a package.

    :param showcase_id: id or name of the showcase to associate
    :type showcase_id: string

    :param package_id: id or name of the package to associate
    :type package_id: string
    '''

    toolkit.check_access('ckanext_showcase_package_association_create',
                         context, data_dict)

    # get organization id
    pkg_dict = toolkit.get_action('package_show')(context, {
        'id': data_dict['package_id']
    })
    if pkg_dict.get('organization'):
        data_dict['organization_id'] = pkg_dict['organization']['id']

    # validate the incoming data_dict
    validated_data_dict, errors = validate(
        data_dict, showcase_package_association_create_schema(), context)

    if errors:
        raise toolkit.ValidationError(errors)

    package_id, showcase_id, organization_id = toolkit.get_or_bust(
        validated_data_dict, ['package_id', 'showcase_id', 'organization_id'])

    if ShowcasePackageAssociation.exists(package_id=package_id,
                                         showcase_id=showcase_id):
        raise toolkit.ValidationError(
            "ShowcasePackageAssociation with package_id '{0}' and showcase_id '{1}' already exists."
            .format(package_id, showcase_id),
            error_summary=u"The dataset, {0}, is already in the showcase".
            format(
                convert_package_name_or_id_to_title_or_name(
                    package_id, context)))

    # create the association
    return ShowcasePackageAssociation.create(package_id=package_id,
                                             showcase_id=showcase_id,
                                             organization_id=organization_id)
Ejemplo n.º 3
0
def showcase_package_association_create(context, data_dict):
    '''Create an association between a showcase and a package.

    :param showcase_id: id or name of the showcase to associate
    :type showcase_id: string

    :param package_id: id or name of the package to associate
    :type package_id: string
    '''

    toolkit.check_access('ckanext_showcase_package_association_create',
                         context, data_dict)

    # validate the incoming data_dict
    validated_data_dict, errors = validate(
        data_dict, showcase_package_association_create_schema(), context)

    if errors:
        raise toolkit.ValidationError(errors)

    package_id, showcase_id = toolkit.get_or_bust(
        validated_data_dict, ['package_id', 'showcase_id'])

    if ShowcasePackageAssociation.exists(package_id=package_id,
                                         showcase_id=showcase_id):
        raise toolkit.ValidationError(
            "ShowcasePackageAssociation with package_id '{0}' and showcase_id '{1}' already exists."
            .format(package_id, showcase_id),
            error_summary=u"The dataset, {0}, is already in the showcase".
            format(
                convert_package_name_or_id_to_title_or_name(
                    package_id, context)))

    # Etsin: raise Validationerror if the showcase is already associated with a package
    if len(
            ShowcasePackageAssociation.get_package_ids_for_showcase(
                showcase_id=showcase_id)) >= 1:
        raise toolkit.ValidationError(
            "Only one package association can be created for a showcase.")

    # create the association
    return ShowcasePackageAssociation.create(package_id=package_id,
                                             showcase_id=showcase_id)