Ejemplo n.º 1
0
 def test_save_as_public(self):
     ds_private = DatasetDcatApOp(TRANSLATION_MEMORY_V_4,
                                  privacy_state=PRIVACY_STATE_PRIVATE,
                                  graph_name=DCATAPOP_PRIVATE_GRAPH_NAME)
     ds_private.privacy_state = PRIVACY_STATE_PUBLIC
     ds_private.get_description_from_ts()
     ds_private.save_to_ts()
Ejemplo n.º 2
0
    def test_update_dataset(self):

        ds = DatasetDcatApOp(
            "http://data.europa.eu/88u/dataset"
            "/european-structural-investment-funds-esif-2014-2020-finance-implementation-details"
        )
        ds.get_description_from_ts()
        ds.save_to_ts()
Ejemplo n.º 3
0
    def test_save_to_ts_new(self):
        dataset = DatasetDcatApOp(
            "http://data.europa.eu/88u/dataset/dgt-translation-memory")
        dataset.get_description_from_ts()
        r = dataset.save_to_ts()

        self.assertTrue(r)

        pass
Ejemplo n.º 4
0
    def test_data_change_in_ts(self):

        ds = DatasetDcatApOp(
            'http://data.europa.eu/88u/dataset/efsa-botanical-compendium')
        ds.get_description_from_ts()

        ds.schema.identifier_adms.get('0').uri = '10.5281/zenodo.1212387'
        result = ds.save_to_ts()

        self.assertTrue(result)
Ejemplo n.º 5
0
def edit_save_to_ts():
    ds1 = DatasetDcatApOp("http://data.europa.eu/88u/dataset/dgt-translation-memory-V1-2")
    if ds1.get_description_from_ts():
        ds1.privacy_state = "public"
        ds1.schema.ckanName_dcatapop['0'].value_or_uri = "NEW CKAN NAME"
        ds1.schema.keyword_dcat['fr'] = ResourceValue(u'la réussite', lang="fr")
        ds1.schema.keyword_dcat['grg'] = ResourceValue(u'επιτυχία', lang="gr")
        ds1.schema.contactPoint_dcat['0'].hasTelephone_vcard['0'].hasValue_vcard['0'].uri = "TEL:213232323"
        if ds1.save_to_ts():
            print " Save done"
        ds1after = DatasetDcatApOp("http://data.europa.eu/88u/dataset/dgt-translation-memory-V1-2")
        ds1after.get_description_from_ts()
        pass
Ejemplo n.º 6
0
    def test_save_to_ts(self):
        ds1 = DatasetDcatApOp(
            "http://data.europa.eu/88u/dataset/dgt-translation-memory-V1-2")
        if ds1.get_description_from_ts():
            ds1.privacy_state = "public"
            ds1.schema.ckanName_dcatapop['0'].value_or_uri = "NEW CKAN NAME"
            ds1.schema.ckanName_dcatapop['1'] = ResourceValue(
                "Second CKAN NAME")
            # ckan_name_new = ds1.schema.ckanName_dcatapop['1'] = ResourceValue("another ckan Name")
            ds1.schema.contactPoint_dcat['0'].hasTelephone_vcard[
                '0'].hasValue_vcard['0'].uri = "TEL:213232323"
            ds1.save_to_ts()

            ds1after = DatasetDcatApOp(
                "http://data.europa.eu/88u/dataset/dgt-translation-memory-V1-2"
            )
            ds1after.get_description_from_ts()

            ckan_name_new = ds1after.schema.ckanName_dcatapop['0'].value_or_uri
            lenc = len(ds1after.schema.ckanName_dcatapop)
            msg = "Expected name {0}, New value {1}. Expected length {2}, Get {3}"
            self.assertTrue(
                ckan_name_new == "NEW CKAN NAME" and lenc == 2,
                msg.format("NEW CKAN NAME", ckan_name_new, 2, lenc))
            # check if the generation of uris from memeber name workds in the case of DASH and DOT
            self.assertTrue("organisation-name" in ds1after.ttl_as_in_ts,
                            "generation of uri from member failed")

        ds_new = DatasetDcatApOp("http://newdcatap.com")
        ds_new.schema.ckanName_dcatapop['0'] = ResourceValue("ckan Name new")
        ds_new.save_to_ts()

        ds_new_from_ts = DatasetDcatApOp("http://newdcatap.com")
        ds_new_from_ts.get_description_from_ts()

        self.assertTrue(
            ds_new_from_ts.schema.ckanName_dcatapop['0'].value_or_uri ==
            ds_new.schema.ckanName_dcatapop['0'].value_or_uri,
            "New dataset is not saved")
Ejemplo n.º 7
0
 def test_edit_save_to_ts(self):
     self.test_get_description_from_ts()
     dataset = DatasetDcatApOp(TRANSLATION_MEMORY_V_1_2)
     if dataset.get_description_from_ts():
         dataset.privacy_state = PRIVACY_STATE_PUBLIC
         dataset.schema.ckanName_dcatapop[
             '0'].value_or_uri = "NEW CKAN NAME"
         dataset.schema.keyword_dcat[LanguagesConstants.LANGUAGE_CODE_FR] = \
             ResourceValue(u'la réussite', lang=LanguagesConstants.LANGUAGE_CODE_FR)
         dataset.schema.keyword_dcat[LanguagesConstants.LANGUAGE_CODE_EL] = \
             ResourceValue(u'επιτυχία', lang=LanguagesConstants.LANGUAGE_CODE_EL)
         dataset.schema.contactPoint_dcat['0'].hasTelephone_vcard[
             '0'].hasValue_vcard['0'].uri = "TEL:213232323"
         if dataset.save_to_ts():
             print " Save done"
         ds1after = DatasetDcatApOp(TRANSLATION_MEMORY_V_1_2)
         ds1after.get_description_from_ts()
         pass
Ejemplo n.º 8
0
def save_as_public():
    ds_private = DatasetDcatApOp("http://data.europa.eu/999/dataset/dgt-translation-memory-V4", privacy_state="private",
                                 graph_name=DCATAPOP_PRIVATE_GRAPH_NAME)
    ds_private.privacy_state = 'public'
    ds_private.get_description_from_ts()
    ds_private.save_to_ts()
Ejemplo n.º 9
0
def package_create(context, data_dict):
    '''
    This overides core package_create  to deal with DCAT-AP datasets and old CKAN model datasets.
    :param context:
    :param data_dict:
    :return:
    '''
    user = context['user']
    package_type = data_dict.get('type')
    package_plugin = lib_plugins.lookup_package_plugin(package_type)
    if 'schema' in context:
        schema = context['schema']
    else:
        schema = package_plugin.create_package_schema()

    _check_access('package_create', context, data_dict)

    if 'api_version' not in context:
        # check_data_dict() is deprecated. If the package_plugin has a
        # check_data_dict() we'll call it, if it doesn't have the method we'll
        # do nothing.
        check_data_dict = getattr(package_plugin, 'check_data_dict', None)
        if check_data_dict:
            try:
                check_data_dict(data_dict, schema)
            except TypeError:
                # Old plugins do not support passing the schema so we need
                # to ensure they still work
                package_plugin.check_data_dict(data_dict)

    ex_url = data_dict.get('url')
    publisher = data_dict.get('owner_org')

    dataset = None
    mapper = None
    if not 'DCATAP' == context.get('model', ''):
        package_show_action = 'legacy_package_show'
        if config.get('ckan.ecodp.backward_compatibility',
                      'true') in 'false, False':
            raise logic.NotFound('Function not available')

        #if not validation.is_ckanName_unique(data_dict.get('name', '')):
        #    raise ValidationError(_('That CKAN name is already in use.'))

        try:
            dataset = dataset_transition_util.create_dataset_schema_for_package_dict(
                data_dict)
        except ValidationError as e:
            import traceback
            log.error('{0}'.format(e))
            log.error(traceback.print_exc())
            raise e
        except BaseException as e:
            import traceback
            log.error('{0}'.format(e))
            log.error(traceback.print_exc())
            raise ValidationError(
                'Could {0} not transform to new model'.format(
                    data_dict.get('name')))

        context['package'] = dataset

        if not ex_url:
            raise ValidationError(_('The URL is mandatory.'))

        int_id = dataset.dataset_uri.split('/')[-1]
        mapper = DatasetIdMapping(ex_url, int_id, publisher)
        if mapper.is_mapping_exists():
            raise ValidationError(
                _('That URL already exists [{0}] for publisher [{1}].'.format(
                    ex_url, publisher)))

    else:
        package_show_action = 'package_show'
        uri, ds_name = uri_util.new_dataset_uri_from_title(
            data_dict.get('title'))
        dataset = DatasetDcatApOp(uri)
        context['package'] = dataset
        data_dict[
            'name'] = ds_name  #uri.split('/')[-1]# put the correct ckanName alligned with the uri.
        data_dict[
            'accessRights'] = 'http://publications.europa.eu/resource/authority/access-right/PUBLIC'  #add default public accessright

        dataset.create_dataset_schema_for_package_dict(data_dict, {}, context)

    start = time.time()
    dataset, errors = validation.validate_dacat_dataset(dataset, context)
    context['errors'] = errors
    log.info('validation took {0} sec'.format(time.time() - start))

    # TODO check the business rule of save
    if errors.get('fatal'):
        # dataset.privacy_state = DCATAPOP_PRIVATE_DATASET
        # dataset.add_draft_to_title()
        raise ValidationError(errors)
    elif errors.get(
            'error') and dataset.privacy_state == DCATAPOP_PUBLIC_DATASET:
        # dataset.privacy_state = DCATAPOP_PRIVATE_DATASET
        # dataset.add_draft_to_title()
        raise ValidationError(errors)
        # elif errors.get('error') and dataset.privacy_state == DCATAPOP_PRIVATE_DATASET:
        #   pass

    # if dataset.privacy_state ==DCATAPOP_PRIVATE_DATASET:
    #    dataset.add_draft_to_title()

    rev = model.repo.new_revision()
    rev.author = user
    if 'message' in context:
        rev.message = context['message']
    else:
        rev.message = _(u'REST API: Create object %s') % dataset.dataset_uri

    try:
        state = dataset.save_to_ts(rev.id)
        if state and mapper:
            mapper.save_to_db()
    except BaseException as e:
        import traceback
        log.error('{0}'.format(e))
        log.error(traceback.print_exc())
        log.error("Error while saving the package to Virtuoso.")
        model.repo.rollback()
        raise ActionError(
            'Error while saving the package {0} to Virtuoso.'.format(
                dataset.dataset_uri))

    context_org_update = context.copy()
    context_org_update['ignore_auth'] = True
    context_org_update['defer_commit'] = True
    _get_action('package_owner_org_update')(context_org_update, {
        'id':
        dataset.schema.uri,
        'organization_id':
        data_dict.get('owner_org') or data_dict.get('organization')
    })

    # for item in plugins.PluginImplementations(plugins.IPackageController):
    #     item.create(pkg)
    #
    #     item.after_create(context, data)

    ## this is added so that the rest controller can make a new location
    context["id"] = dataset.schema.uri
    log.debug('Created object %s' % dataset.schema.uri)

    # Make sure that a user provided schema is not used on package_show
    context.pop('schema', None)

    return_id_only = context.get('return_id_only', False)
    if return_id_only:
        output = dataset.dataset_uri
    elif 'legacy_package_show' == package_show_action:
        output = _get_action(package_show_action)(context, {
            'uri': dataset.dataset_uri
        })
    else:
        _get_action(package_show_action)(context, {'uri': dataset.dataset_uri})
        output = context.get('package')

    indexer = solar_package_index()

    try:
        indexer.update_dict(dataset)
        if not context.get('defer_commit'):
            model.repo.commit()
    except Exception as e:
        dataset.delete_from_ts()
        model.repo.rollback()
        raise ActionError('Error while index the package {0} to Solr.'.format(
            dataset.dataset_uri))

    return output