예제 #1
0
    def load_dataset(self, dataset_uri):
        '''
        Load dataset from Triple store or the cache
        :return: type: DatasetDcatApOp
        '''

        dataset = None  # type: DatasetDcatApOp
        dataset_string = redis_cache.get_from_cache(dataset_uri,
                                                    pool=redis_cache.MISC_POOL)
        if dataset_string:
            dataset = pickle.loads(dataset_string)
            log.info(
                '[Dataset] [LOAD from cache] [URI:<{0}>]'.format(dataset_uri))

        if not dataset or not dataset.schema:
            dataset = DatasetDcatApOp(dataset_uri)
            loaded_from_public = dataset.get_description_from_ts()
            if not loaded_from_public:
                dataset.set_state_as_private()
                loaded_from_private = dataset.get_description_from_ts()
                log.info("[Dataset] [Load from private] [URI:<{0}>]".format(
                    dataset_uri))

        return dataset
예제 #2
0
def legacy_package_show(context, data_dict):
    '''Return the metadata of a dataset (package) and its resources.
    This overrides core package_show to deal with DCAT-AP data

    :param str uri: the uri  of the dataset

    :rtype: dictionary

    '''
    import ckanext.ecportal.model.mapping.old_model_mapper as mapper

    if config.get('ckan.ecodp.backward_compatibility',
                  'true') in 'false, False':
        raise logic.NotFound('Function not available')

    uri_prefix = '{0}/{1}'.format(config.get('ckan.ecodp.uri_prefix'),
                                  'dataset')
    dataset_uri_ckan2odp = data_dict.get("objectUri")
    if dataset_uri_ckan2odp:
        name_or_id = dataset_uri_ckan2odp
    elif data_dict.get("id"):
        name_or_id = '{0}/{1}'.format(uri_prefix, data_dict.get("id"))
    else:
        name_or_id = data_dict.get(
            "uri"
        )  # or 'http://data.europa.eu/999/dataset/dgt-translation-memory-V1-2'

    if not name_or_id:
        raise DataError('No id provided')
    active_cache = config.get('ckan.cache.active', 'false')
    dataset = None  # type: DatasetDcatApOp
    if active_cache == 'true':
        # get the ds from cache
        dataset_string = redis_cache.get_from_cache(
            name_or_id, pool=redis_cache.DATASET_POOL)
        if dataset_string:
            dataset = pickle.loads(dataset_string)
            log.info('Load dataset from cache: {0}'.format(name_or_id))
            # dataset = DatasetDcatApOp(name_or_id,dataset_json)

    if not dataset or not dataset.schema:
        dataset = DatasetDcatApOp(name_or_id)
        # todo optimize the code
        if not dataset.get_description_from_ts() and (
                context.get('auth_user_obj', None)
                or context.get('ignore_auth', False) == True):
            dataset.set_state_as_private()
            #private dataset should not be cached
            active_cache = 'false'

        if not dataset.get_description_from_ts():
            raise logic.NotFound(_('ecodp.dcat.dataset.not_found'))

        log.info('Load dataset from ts: {0}'.format(name_or_id))
        if active_cache == 'true':
            redis_cache.set_value_no_ttl_in_cache(name_or_id,
                                                  pickle.dumps(dataset))

    if not dataset.schema:
        raise logic.NotFound('ecodp.dcat.dataset.not_found')

    context['package'] = dataset
    permission = _check_access('package_show', context, data_dict)

    if not permission:
        raise logic.NotAuthorized()

    package_dict = mapper.package_show_schema(dataset)

    return package_dict
예제 #3
0
def package_show(context, data_dict):
    '''Return the metadata of a dataset (package) and its resources.
    This overrides core package_show to deal with DCAT-AP data

    :param str uri: the uri  of the dataset

    :rtype: dictionary

    '''
    start = time.time()
    uri_prefix = '{0}/{1}'.format(config.get('ckan.ecodp.uri_prefix'),
                                  'dataset')
    dataset_uri_ckan2odp = data_dict.get("objectUri")
    if dataset_uri_ckan2odp:
        name_or_id = dataset_uri_ckan2odp
    elif data_dict.get("id"):
        name_or_id = '{0}/{1}'.format(uri_prefix, data_dict.get("id"))
    else:
        name_or_id = data_dict.get(
            "uri"
        )  # or 'http://data.europa.eu/999/dataset/dgt-translation-memory-V1-2'

    if not name_or_id:
        raise DataError('No id provided')
    active_cache = config.get('ckan.cache.active', 'false')
    dataset = None  # type: DatasetDcatApOp
    if active_cache == 'true':
        # get the ds from cache
        dataset_string = redis_cache.get_from_cache(
            name_or_id, pool=redis_cache.DATASET_POOL)
        if dataset_string:
            dataset = pickle.loads(dataset_string)
            log.info('Load dataset from cache: {0}'.format(name_or_id))
            # dataset = DatasetDcatApOp(name_or_id,dataset_json)

    if not dataset or not dataset.schema:

        dataset = DatasetDcatApOp(name_or_id)
        graph_name = dataset.find_the_graph_in_ts()
        loaded = False
        if graph_name not in [
                DCATAPOP_PRIVATE_GRAPH_NAME, DCATAPOP_PUBLIC_GRAPH_NAME
        ]:
            raise logic.NotFound('Package show: dataset {0} {1}'.format(
                name_or_id, _('ecodp.dcat.dataset.not_found')))
        if graph_name == DCATAPOP_PUBLIC_GRAPH_NAME:
            dataset.set_state_as_public()
            loaded = dataset.get_description_from_ts()
        elif graph_name == DCATAPOP_PRIVATE_GRAPH_NAME and (
                context.get('auth_user_obj', None)
                or context.get('ignore_auth', False) == True):
            dataset.set_state_as_private()
            active_cache = 'false'
            loaded = dataset.get_description_from_ts()
        if loaded:
            log.info('Load dataset from ts: {0}'.format(name_or_id))
        else:
            log.info('Load dataset from ts failed: {0}'.format(name_or_id))
            raise logic.NotFound('Package show: dataset {0} {1}'.format(
                name_or_id, _('ecodp.dcat.dataset.not_found')))
        if active_cache == 'true' and loaded:
            redis_cache.set_value_no_ttl_in_cache(name_or_id,
                                                  pickle.dumps(dataset))

    if not dataset.schema and not loaded:
        raise logic.NotFound('Package show: dataset {0} {1}'.format(
            name_or_id, _('ecodp.dcat.dataset.not_found')))

    context['package'] = dataset
    permission = _check_access('package_show', context, data_dict)
    if not permission:
        raise logic.NotAuthorized()

    if context.get('internal'):
        log.info('Package show internal took {0} sec'.format(time.time() -
                                                             start))
        context['package'] = dataset
        return dataset

    output_format = data_dict.get('output_format', u'standard')
    if output_format not in [u'standard', u'rdf', u'json']:
        output_format = u'standard'
    package_dict = {}
    if not output_format == u'json':
        package_dict['rdf'] = dataset.get_dataset_as_rdfxml()

    if not output_format == u'rdf':
        package_dict['dataset'] = dataset.schema.schema_dictaze()

    if not output_format == u'rdf':
        package_dict[
            'catalog_record'] = {} if not dataset.schema_catalog_record else dataset.schema_catalog_record.schema_dictaze(
            )

    if output_format == u'standard':
        package_dict['capacity'] = dataset.privacy_state

    if context.get('for_view'):
        try:
            locale = tk.request.environ['CKAN_LANG']
        except Exception:
            locale = config.get('ckan.locale_default', 'en')

        package_dict = ui_util.transform_dcat_schema_to_ui_schema(
            dataset, locale)
        # package_dict.update(ui_dict)
        for item in plugins.PluginImplementations(plugins.IPackageController):
            log.debug('Loaded plugin: {0}'.format(item.__class__.__name__))
            package_dict = item.before_view(package_dict)

        for key, resource_dict in package_dict.get('distribution_dcat',
                                                   {}).items():
            resource_dict['id'] = resource_dict['uri'].split('/')[-1]
            for item in plugins.PluginImplementations(
                    plugins.IResourceController):
                log.debug('Loaded plugin: {0}'.format(item.__class__.__name__))
                resource_dict = item.before_show(resource_dict)

    # for item in plugins.PluginImplementations(plugins.IPackageController):
    #     item.after_show(context, package_dict)
    log.info('Package show took {0} sec'.format(time.time() - start))
    return package_dict