Skip to content

Helpers

build_gbif_nav_item(package_name, resource_id, record_id, version=None)

Creates the gbif specimen nav item allowing the user to navigate to the gbif views of the specimen record data. A single nav item is returned.

Parameters:

Name Type Description Default
package_name

the package name (or id)

required
resource_id

the resource id

required
record_id

the record id

required
version

the version of the record, or None if no version is present

None

Returns:

Type Description

a nav items

Source code in ckanext/gbif/lib/helpers.py
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
def build_gbif_nav_item(package_name, resource_id, record_id, version=None):
    """
    Creates the gbif specimen nav item allowing the user to navigate to the gbif views
    of the specimen record data. A single nav item is returned.

    :param package_name: the package name (or id)
    :param resource_id: the resource id
    :param record_id: the record id
    :param version: the version of the record, or None if no version is present
    :returns: a nav items
    """
    kwargs = {
        'package_name': package_name,
        'resource_id': resource_id,
        'record_id': record_id,
    }
    # if there's a version, add it to the kwargs
    if version is not None:
        kwargs['version'] = version
    # build the nav and return it
    return toolkit.h.build_nav_icon('gbif.view', toolkit._('GBIF view'), **kwargs)

dqi_get_severity(errors, gbif_id)

Get status for severity of errors.

Parameters:

Name Type Description Default
errors

a list of errors

required
gbif_id

the GBIF occurrence id for this record

required

Returns:

Type Description

the status to show

Source code in ckanext/gbif/lib/helpers.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
def dqi_get_severity(errors, gbif_id):
    """
    Get status for severity of errors.

    :param errors: a list of errors
    :param gbif_id: the GBIF occurrence id for this record
    :returns: the status to show
    """
    if not gbif_id:
        return 'unknown'

    if not errors:
        return 'No errors'

    for error in errors:
        if error['severity'] == DQI_MAJOR_ERRORS:
            # if we have one major error, the whole thing is major error
            return 'Major errors'

    return 'Minor errors'

dqi_parse_errors(errors)

Convert each DQI status string into a more detailed dict.

Parameters:

Name Type Description Default
errors

a list of error names

required

Returns:

Type Description

a list of dicts of information about each error

Source code in ckanext/gbif/lib/helpers.py
18
19
20
21
22
23
24
25
26
27
28
29
30
def dqi_parse_errors(errors):
    """
    Convert each DQI status string into a more detailed dict.

    :param errors: a list of error names
    :returns: a list of dicts of information about each error
    """
    if not errors:
        return []
    # do an in check to make sure that we don't break if the error is one we just haven't mapped yet
    return [
        GBIF_ERRORS[error_code] for error_code in errors if error_code in GBIF_ERRORS
    ]

gbif_get_classification(gbif_record)

Loop through all the classification parts, building an array of parts.

Parameters:

Name Type Description Default
gbif_record

return:

required
Source code in ckanext/gbif/lib/helpers.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
def gbif_get_classification(gbif_record):
    """
    Loop through all the classification parts, building an array of parts.

    :param gbif_record: return:
    """
    classification = []

    url = 'http://www.gbif.org/species'
    for classification_part in [
        'kingdom',
        'phylum',
        'class',
        'taxonorder',
        'family',
        'genus',
    ]:
        key = f'{classification_part}Key'
        key_value = gbif_record.get(key, None)
        name = gbif_record.get(classification_part, None)
        if key_value:
            classification.append(
                f'<a href="{url}/{key_value}" target="_blank" rel="nofollow">{name}</a>'
            )
        elif name:
            classification.append(name)

    return literal(' <i class="fa fa-angle-right"></i> '.join(classification))

gbif_get_geography(occurrence)

Parameters:

Name Type Description Default
occurrence
required
Source code in ckanext/gbif/lib/helpers.py
85
86
87
88
89
90
91
92
93
94
95
96
def gbif_get_geography(occurrence):
    """
    :param occurrence:
    """
    geography = []
    for geographic_part in ['continent', 'country', 'stateprovince']:
        value = occurrence.get(geographic_part, None)

        if value:
            geography.append(value.replace('_', ' '))

    return literal(' <i class="icon-angle-right"></i> '.join(geography))

gbif_render_datetime(date_str)

Render a GBIF formatted datetime.

Parameters:

Name Type Description Default
date_str

return:

required
Source code in ckanext/gbif/lib/helpers.py
 99
100
101
102
103
104
105
def gbif_render_datetime(date_str):
    """
    Render a GBIF formatted datetime.

    :param date_str: return:
    """
    return dateutil.parser.parse(date_str).strftime('%B %d, %Y')

get_gbif_record_url(pkg, res, rec)

Given details about a combination of package, resource and record, return the GBIF view URL created from them.

Parameters:

Name Type Description Default
pkg

the package dict

required
res

the resource dict

required
rec

the record dict

required

Returns:

Type Description

the link to the GBIF view for this record/resource/package combo

Source code in ckanext/gbif/lib/helpers.py
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
def get_gbif_record_url(pkg, res, rec):
    """
    Given details about a combination of package, resource and record, return the GBIF
    view URL created from them.

    :param pkg: the package dict
    :param res: the resource dict
    :param rec: the record dict
    :returns: the link to the GBIF view for this record/resource/package combo
    """
    # return the url for package/resource/record combo requested
    return toolkit.url_for(
        'gbif.view',
        package_name=pkg['name'],
        resource_id=res['id'],
        record_id=rec['_id'],
    )