Skip to content

Id as url

IdAsUrlTransform

Bases: BaseTransform

Source code in ckanext/versioned_datastore/lib/downloads/transforms/id_as_url.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class IdAsUrlTransform(BaseTransform):
    name = 'id_as_url'

    def __init__(self, field=None, **kwargs):
        """
        :param field: the name of the data field that contains the ID and that will contain the URL
        """
        super().__init__(**kwargs)
        self.field = field

    def __call__(self, data):
        """
        Reformat an ID field as a URL (probably one that links to that record). Requires
        an endpoint (config option ckanext.versioned_datastore.record_view_endpoint,
        default 'object.view') taking the ID field as the 'uuid' named argument.

        :param data: the record data to be transformed
        :returns: the transformed data (or untransformed if there was an error).
        """
        if self.field is None:
            return data
        try:
            object_id = data.get(self.field)
            if object_id is None or object_id == '':
                log.error(f'Failed to get uuid from field "{self.field}".')
                return data
            kwargs = {'uuid': object_id}
            url = toolkit.url_for(object_endpoint, **kwargs)
        except:
            log.error(f'Failed to generate URL from ID.', exc_info=True)
            return data
        data[self.field] = base_url + url
        return data

__call__(data)

Reformat an ID field as a URL (probably one that links to that record). Requires an endpoint (config option ckanext.versioned_datastore.record_view_endpoint, default 'object.view') taking the ID field as the 'uuid' named argument.

Parameters:

Name Type Description Default
data

the record data to be transformed

required

Returns:

Type Description

the transformed data (or untransformed if there was an error).

Source code in ckanext/versioned_datastore/lib/downloads/transforms/id_as_url.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def __call__(self, data):
    """
    Reformat an ID field as a URL (probably one that links to that record). Requires
    an endpoint (config option ckanext.versioned_datastore.record_view_endpoint,
    default 'object.view') taking the ID field as the 'uuid' named argument.

    :param data: the record data to be transformed
    :returns: the transformed data (or untransformed if there was an error).
    """
    if self.field is None:
        return data
    try:
        object_id = data.get(self.field)
        if object_id is None or object_id == '':
            log.error(f'Failed to get uuid from field "{self.field}".')
            return data
        kwargs = {'uuid': object_id}
        url = toolkit.url_for(object_endpoint, **kwargs)
    except:
        log.error(f'Failed to generate URL from ID.', exc_info=True)
        return data
    data[self.field] = base_url + url
    return data

__init__(field=None, **kwargs)

Parameters:

Name Type Description Default
field

the name of the data field that contains the ID and that will contain the URL

None
Source code in ckanext/versioned_datastore/lib/downloads/transforms/id_as_url.py
17
18
19
20
21
22
def __init__(self, field=None, **kwargs):
    """
    :param field: the name of the data field that contains the ID and that will contain the URL
    """
    super().__init__(**kwargs)
    self.field = field