Skip to content

Helpers

accessible_gravatar(email_hash, size=100, default=None, userobj=None)

Port of ckan helper gravatar Adds title text to the image so it passes accessibility checks.

Parameters:

Name Type Description Default
email_hash
required
default

(optional, default: None)

None
size

(optional, default: 100)

100
userobj

(optional, default: None)

None
Source code in ckanext/nhm/lib/helpers.py
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
def accessible_gravatar(email_hash, size=100, default=None, userobj=None):
    """
    Port of ckan helper gravatar Adds title text to the image so it passes accessibility
    checks.

    :param email_hash:
    :param default: (optional, default: None)
    :param size: (optional, default: 100)
    :param userobj: (optional, default: None)
    """
    gravatar_literal = toolkit.h.gravatar(email_hash, size, default)
    if userobj is not None:
        grav_xml = etree.fromstring(gravatar_literal)
        grav_xml.attrib['alt'] = userobj.name
        gravatar_literal = literal(etree.tostring(grav_xml, encoding='unicode'))

    return gravatar_literal

add_url_filter(field, value, extras=None)

The CKAN built in functions remove_url_param / add_url_param cannot handle multiple filters which are concatenated with |, not separate query params This replaces add_url_param for filters.

Parameters:

Name Type Description Default
field
required
extras
None
value
required
Source code in ckanext/nhm/lib/helpers.py
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
def add_url_filter(field, value, extras=None):
    """
    The CKAN built in functions remove_url_param / add_url_param cannot handle multiple
    filters which are concatenated with |, not separate query params This replaces
    add_url_param for filters.

    :param field:
    :param extras:
    :param value:
    """

    params = {k: v for k, v in toolkit.request.params.items() if k != 'page'}
    url_filter = f'{field}:{value}'
    filters = [
        f for f in params.get('filters', '').split('|') + [url_filter] if f != ''
    ]
    filters = '|'.join(filters)
    params['filters'] = filters
    return core_helpers._url_with_params(toolkit.request.base_url, params.items())

Link to API documentation.

Source code in ckanext/nhm/lib/helpers.py
423
424
425
426
427
428
429
430
def api_doc_link():
    """
    Link to API documentation.
    """
    attr = {'class': 'external', 'target': '_blank'}
    return toolkit.h.link_to(
        toolkit._('API guide'), 'http://docs.ckan.org/en/latest/api/index.html', **attr
    )

build_nav_main(*menu_items)

Build a set of menu items. Overrides core CKAN method to add "nav-item" class to li elements and allow endpoint keyword args.

Parameters:

Name Type Description Default
menu_items

tuples of (endpoint_details, title) e.g. ('home.index', ('Home')) or (('search.view', {'slug': 'everything'}), ('Search'))

()

Returns:

Type Description

literal -

Source code in ckanext/nhm/lib/helpers.py
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
def build_nav_main(*menu_items):
    """
    Build a set of menu items. Overrides core CKAN method to add "nav-item" class to li
    elements and allow endpoint keyword args.

    :param menu_items: tuples of (endpoint_details, title) e.g. ('home.index', _('Home')) or (('search.view', {'slug': 'everything'}), _('Search'))
    :returns: literal - <li class="nav-item"><a href="...">title</a></li>
    """
    current_endpoint = '.'.join(toolkit.get_endpoint())
    output = ''
    for item in menu_items:
        endpoint_details = item[0]
        if isinstance(endpoint_details, tuple):
            endpoint_name, endpoint_kwargs = endpoint_details
        else:
            endpoint_name = endpoint_details
            endpoint_kwargs = {}
        title = item[1]
        if len(item) == 3 and not toolkit.check_access(item[2]):
            continue
        try:
            endpoint = toolkit.url_for(endpoint_name, **endpoint_kwargs)
        except BuildError:
            raise Exception(f'Endpoint {endpoint_name} cannot be found.')
        active = endpoint_name == current_endpoint
        link_text = f'<span>{title}</span>'
        link = core_helpers.link_to(link_text, endpoint).unescape()
        if active:
            output += f'<li class="active nav-item">{link}</li>'
        else:
            output += f'<li class="nav-item">{link}</li>'
    return literal(output)

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

Creates the specimen nav items allowing the user to navigate to different views of the specimen record data. A list of nav items 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 list of nav items

Source code in ckanext/nhm/lib/helpers.py
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
def build_specimen_nav_items(package_name, resource_id, record_id, version=None):
    """
    Creates the specimen nav items allowing the user to navigate to different views of
    the specimen record data. A list of nav items 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 list of nav items
    """
    link_definitions = [
        ('record.view', toolkit._('Normal view')),
        ('record.dwc', toolkit._('Darwin Core view')),
    ]
    links = []
    for route_name, link_text in link_definitions:
        nav_item = toolkit.h.build_nav_icon(
            route_name,
            link_text,
            package_name=package_name,
            resource_id=resource_id,
            record_id=record_id,
            version=version,
        )
        links.append(_add_nav_item_class(nav_item, classes=[], role='presentation'))

    return links

camel_case_to_string(camel_case_string)

Parameters:

Name Type Description Default
camel_case_string
required
Source code in ckanext/nhm/lib/helpers.py
672
673
674
675
676
677
678
679
def camel_case_to_string(camel_case_string):
    """

    :param camel_case_string:

    """
    s = ' '.join(re.findall(r'[A-Z]?[a-z]+|[A-Z]+(?=[A-Z]|$)', camel_case_string))
    return s[0].upper() + s[1:]

collection_stats()

Get collection stats, including collection codes and collection totals.

Source code in ckanext/nhm/lib/helpers.py
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
@cache_region('collection_stats', 'collection_stats')
def collection_stats():
    """
    Get collection stats, including collection codes and collection totals.
    """
    stats = {}
    collections = [
        ('artefacts', get_artefact_resource_id()),
        ('indexlots', get_indexlot_resource_id()),
        ('specimens', get_specimen_resource_id()),
    ]

    collections_total = 0
    for name, resource_id in collections:
        # we use the same params for both the vds_resource_check and the vds_basic_count
        # and the limit param is just ignored by vds_resource_check
        params = {'resource_id': resource_id, 'limit': 0}
        # check if the resource is a valid datastore resource first otherwise the call
        # to vds_basic_count could error out
        if toolkit.get_action('vds_resource_check')({}, params):
            stats[name] = toolkit.get_action('vds_basic_count')({}, params)
        else:
            stats[name] = 0
        collections_total += stats[name]
    stats['total'] = collections_total

    collection_code_counts = []
    for collection_code in ('PAL', 'MIN', 'BMNH(E)', 'ZOO', 'BOT'):
        params = {
            'resource_ids': [get_specimen_resource_id()],
            'limit': 0,
            'query': {
                'filters': {
                    'and': [
                        {
                            'string_equals': {
                                'fields': ['collectionCode'],
                                'value': collection_code,
                            }
                        }
                    ]
                }
            },
        }
        total = toolkit.get_action('vds_multi_count')({}, params)['total']
        collection_code_counts.append((collection_code, total))

    collection_code_counts.sort(key=operator.itemgetter(1), reverse=True)
    stats['collectionCodes'] = OrderedDict(collection_code_counts)

    return stats

dataset_author_truncate(author_str)

For author strings with lots of authors need to shorten for display insert et al as abbreviation tag.

Parameters:

Name Type Description Default
author_str

dataset author

required

Returns:

Type Description

shortened author str, full text in abbr tag

Source code in ckanext/nhm/lib/helpers.py
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
def dataset_author_truncate(author_str):
    """
    For author strings with lots of authors need to shorten for display insert et al as
    abbreviation tag.

    :param author_str: dataset author
    :returns: shortened author str, full text in abbr tag
    """

    def _truncate(author_str, separator=None):
        """

        :param author_str:
        :param separator:  (optional, default: None)

        """

        # If we have a separator, split string on it
        if separator:
            shortened = ';'.join(author_str.split(separator)[0:4])
        else:
            # Otherwise use the jinja truncate function (may split author name)
            shortened = do_truncate(author_str, length=AUTHOR_MAX_LENGTH, end='')

        return literal(
            '{0} <abbr title="{1}" style="cursor: pointer;">et al.</abbr>'.format(
                shortened, author_str
            )
        )

    if author_str and len(author_str) > AUTHOR_MAX_LENGTH:
        if ';' in author_str:
            author_str = _truncate(author_str, ';')
        elif ',' in author_str:
            author_str = _truncate(author_str, ',')
        else:
            author_str = _truncate(author_str)

    return author_str

dataset_categories()

Return list of dataset category terms.

Returns:

Type Description

list

Source code in ckanext/nhm/lib/helpers.py
189
190
191
192
193
194
195
196
197
198
199
200
def dataset_categories():
    """
    Return list of dataset category terms.

    :returns: list
    """
    try:
        return toolkit.get_action('tag_list')(
            data_dict={'vocabulary_id': DATASET_TYPE_VOCABULARY}
        )
    except toolkit.ObjectNotFound:
        return []

delimit_number(num)

Separate long number into thousands 1000000 => 1,000,000.

Parameters:

Name Type Description Default
num
required
Source code in ckanext/nhm/lib/helpers.py
414
415
416
417
418
419
420
def delimit_number(num):
    """
    Separate long number into thousands 1000000 => 1,000,000.

    :param num:
    """
    return '{:,}'.format(num)

downloadable(resource)

Is a resource downloadable.

Parameters:

Name Type Description Default
resource
required

Returns:

Type Description

bool

Source code in ckanext/nhm/lib/helpers.py
852
853
854
855
856
857
858
859
860
861
862
def downloadable(resource):
    """
    Is a resource downloadable.

    :param resource:
    :returns: bool
    """
    # datastore resources and uploads with a format
    return resource.get('datastore_active', False) or (
        resource.get('url_type') == 'upload' and bool(resource.get('format'))
    )

Is a field a link (starts with http and is a valid URL).

Parameters:

Name Type Description Default
value
required

Returns:

Type Description

boolean

Source code in ckanext/nhm/lib/helpers.py
827
828
829
830
831
832
833
834
835
836
837
838
def field_is_link(value):
    """
    Is a field a link (starts with http and is a valid URL).

    :param value:
    :returns: boolean
    """
    try:
        return value.startswith('http') and re_url_validation.match(value)
    except Exception:
        pass
    return False

field_name_label(field_name)

Convert a field name into a label - replacing _s and upper casing first character.

Parameters:

Name Type Description Default
field_name
required

Returns:

Type Description

str label

Source code in ckanext/nhm/lib/helpers.py
814
815
816
817
818
819
820
821
822
823
824
def field_name_label(field_name):
    """
    Convert a field name into a label - replacing _s and upper casing first character.

    :param field_name:
    :returns: str label

    """
    label = field_name.replace('_', ' ')
    label = label[0].upper() + label[1:]
    return label

filter_and_format_resource_items(resource)

Given a resource, return the items from it that are whitelisted for display and format them.

Parameters:

Name Type Description Default
resource

the resource dict

required

Returns:

Type Description

a list of made up of 2-tuples containing formatted keys and values from the resource

Source code in ckanext/nhm/lib/helpers.py
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
def filter_and_format_resource_items(resource):
    """
    Given a resource, return the items from it that are whitelisted for display and
    format them.

    :param resource: the resource dict
    :returns: a list of made up of 2-tuples containing formatted keys and values from
        the resource
    """
    blacklist = {
        '_image_field',
        '_title_field',
        '_subtitle_field',
        'datastore_active',
        'has_views',
        'on_same_domain',
        'resource_group_id',
        'revision_id',
        'url_type',
        'disable_parsing',
    }
    items = []
    for key, value in resource.items():
        if key not in blacklist:
            items.append((key, value))
    return toolkit.h.format_resource_items(items)

form_select_datastore_field_options(resource, allow_empty=True)

Parameters:

Name Type Description Default
resource
required
allow_empty

(optional, default: True)

True
Source code in ckanext/nhm/lib/helpers.py
1272
1273
1274
1275
1276
1277
1278
1279
1280
def form_select_datastore_field_options(resource, allow_empty=True):
    """

    :param resource:
    :param allow_empty:  (optional, default: True)

    """
    fields = toolkit.h.resource_view_get_fields(resource)
    return list_to_form_options(fields, allow_empty)

form_select_update_frequency_options()

Get update frequencies as a form list.

Source code in ckanext/nhm/lib/helpers.py
171
172
173
174
175
def form_select_update_frequency_options():
    """
    Get update frequencies as a form list.
    """
    return list_to_form_options(UPDATE_FREQUENCIES)

get_allowed_view_types(resource, package)

Overwrite ckan.lib.helpers.get_allowed_view_types.

We want to edit some of the options - remove Image and change Tiled Map to Map

Parameters:

Name Type Description Default
resource
required
package
required
Source code in ckanext/nhm/lib/helpers.py
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
def get_allowed_view_types(resource, package):
    """
    Overwrite ckan.lib.helpers.get_allowed_view_types.

    We want to edit some of the options - remove Image and change Tiled Map to Map

    :param resource:
    :param package:
    """

    view_types = core_helpers.get_allowed_view_types(resource, package)
    blacklisted_types = ['image']

    filtered_types = []

    for view_type in view_types:
        # Exclude blacklisted types (at the moment just Image)
        if view_type[0] in blacklisted_types:
            continue

        # Rename Tiled map => map
        if view_type[1] == 'Tiled map':
            view_type = (view_type[0], 'Map', view_type[2])

        filtered_types.append(view_type)

    return filtered_types

get_beetle_iiif_resource_id()

Get the ID for the beetle IIIF resource.

Returns:

Type Description

the resource id

Source code in ckanext/nhm/lib/helpers.py
333
334
335
336
337
338
339
340
def get_beetle_iiif_resource_id():
    """
    Get the ID for the beetle IIIF resource.

    :returns: the resource id
    """
    value = toolkit.config.get('ckanext.nhm.beetle_iiif_resource_id')
    return str(value) if value is not None else None

get_contact_form_department_options()

Contact form category.

Source code in ckanext/nhm/lib/helpers.py
841
842
843
844
845
846
847
848
849
def get_contact_form_department_options():
    """
    Contact form category.
    """
    return list_to_form_options(
        COLLECTION_CONTACTS.keys(),
        allow_empty=True,
        allow_empty_text='Select an option',
    )

get_contributor_count()

Get the total number of authors listed on packages, calculated using Solr facets.

Source code in ckanext/nhm/lib/helpers.py
67
68
69
70
71
72
73
74
75
@cache_region('collection_stats', 'contributor_count')
def get_contributor_count():
    """
    Get the total number of authors listed on packages, calculated using Solr facets.
    """
    query = toolkit.get_action('package_search')(
        {}, {'facet.field': ['author'], 'facet.limit': -1}
    )
    return len(query.get('facets', {}).get('author', {}).keys())

get_creator_id_facet_label(facet)

Return display name for the creator_id facet.

Parameters:

Name Type Description Default
facet

A dictionary representing a single value for the facet

required

Returns:

Type Description

A string to use for display name

Source code in ckanext/nhm/lib/helpers.py
799
800
801
802
803
804
805
806
807
808
809
810
811
def get_creator_id_facet_label(facet):
    """
    Return display name for the creator_id facet.

    :param facet: A dictionary representing a single value for the facet
    :returns: A string to use for display name
    """
    try:
        user = model.User.get(facet['name'])
        display_name = user.display_name
    except (toolkit.ObjectNotFound, AttributeError) as e:
        display_name = facet['display_name']
    return display_name

get_department(collection_code)

Return a department name for collection code.

Parameters:

Name Type Description Default
collection_code

BOT, PAL etc.,

required

Returns:

Type Description

Full department name - Entomology

Source code in ckanext/nhm/lib/helpers.py
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
def get_department(collection_code):
    """
    Return a department name for collection code.

    :param collection_code: BOT, PAL etc.,
    :returns: Full department name - Entomology
    """
    departments = {
        'bmnh(e)': 'Entomology',
        'bot': 'Botany',
        'min': 'Mineralogy',
        'pal': 'Palaeontology',
        'zoo': 'Zoology',
    }

    return departments[collection_code.lower()]

get_external_sites(record)

Helper called on collection record pages (i.e. records in the specimens, indexlots or artefacts resources) which is expected to return a list of Site objects. From these sites, links can be generated which are relevant to the record.

Parameters:

Name Type Description Default
record dict

a record dict

required

Returns:

Type Description
List[Site]

a list of Site objects

Source code in ckanext/nhm/lib/helpers.py
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
def get_external_sites(record: dict) -> List[Site]:
    """
    Helper called on collection record pages (i.e. records in the specimens, indexlots
    or artefacts resources) which is expected to return a list of Site objects. From
    these sites, links can be generated which are relevant to the record.

    :param record: a record dict
    :returns: a list of Site objects
    """
    return external_links.get_sites(record)

get_facet_label_function(facet_name, multi=False)

For a given facet, return the function used to fetch the facet's items labels.

Parameters:

Name Type Description Default
facet_name

Facet name

required
multi

If True, the function returned should take a list of facets and a filter value to find the matching facet on the name field (optional, default: False)

False

Returns:

Type Description

A function or None

Source code in ckanext/nhm/lib/helpers.py
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
def get_facet_label_function(facet_name, multi=False):
    """
    For a given facet, return the function used to fetch the facet's items labels.

    :param facet_name: Facet name
    :param multi: If True, the function returned should take a list of facets and a
                  filter value to find the matching facet on the name field (optional,
                  default: False)
    :returns: A function or None
    """
    facet_function = None
    if facet_name == 'creator_user_id':
        facet_function = get_creator_id_facet_label

    if facet_function and multi:

        def filter_facets(facet, filter_value):
            """

            :param facet:
            :param filter_value:

            """
            for f in facet:
                if f['name'] == filter_value:
                    return facet_function(f)
            return filter

        return filter_facets
    else:
        return facet_function

get_image_licence_options()

Return list of image licences Currently this is the same list as dataset licences.

Source code in ckanext/nhm/lib/helpers.py
903
904
905
906
907
908
909
910
911
def get_image_licence_options():
    """
    Return list of image licences Currently this is the same list as dataset licences.
    """

    licenses = [('', '')] + model.Package.get_license_options()

    # Format licences as form options list of dicts
    return [{'value': value, 'text': text} for text, value in licenses]

get_indexlot_resource_id()

Get the ID for the index lots dataset.

Returns:

Type Description

ID for indexlot resource

Source code in ckanext/nhm/lib/helpers.py
319
320
321
322
323
324
325
326
def get_indexlot_resource_id():
    """
    Get the ID for the index lots dataset.

    :returns: ID for indexlot resource
    """
    value = toolkit.config.get('ckanext.nhm.indexlot_resource_id')
    return str(value) if value is not None else None

get_latest_update_for_package(pkg_dict, date_format=None)

Returns the most recent update datetime (formatted as a string) for the package and its resources. If there is no update time found then 'unknown' is returned. If there is datetime found then it is rendered using the standard ckan helper.

Parameters:

Name Type Description Default
pkg_dict

the package dict

required
date_format

date format for the return datetime

None

Returns:

Type Description

'unknown' or a string containing the rendered datetime

Source code in ckanext/nhm/lib/helpers.py
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
def get_latest_update_for_package(pkg_dict, date_format=None):
    """
    Returns the most recent update datetime (formatted as a string) for the package and
    its resources. If there is no update time found then 'unknown' is returned. If there
    is datetime found then it is rendered using the standard ckan helper.

    :param pkg_dict: the package dict
    :param date_format: date format for the return datetime
    :returns: 'unknown' or a string containing the rendered datetime
    """
    latest_date, _ = _get_latest_update(
        itertools.chain([pkg_dict], pkg_dict.get('resources', []))
    )
    if latest_date is not None:
        return toolkit.h.render_datetime(latest_date, date_format=date_format)
    else:
        return toolkit._('unknown')

get_latest_update_for_package_resources(pkg_dict, date_format=None)

Returns the most recent update datetime (formatted as a string) across all resources in this package. If there is no update time found then 'unknown' is returned. If there is datetime found then it is rendered using the standard ckan helper.

Parameters:

Name Type Description Default
pkg_dict

the package dict

required
date_format

date format for the return datetime

None

Returns:

Type Description

'unknown' or a string containing the rendered datetime and the resource name

Source code in ckanext/nhm/lib/helpers.py
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
def get_latest_update_for_package_resources(pkg_dict, date_format=None):
    """
    Returns the most recent update datetime (formatted as a string) across all resources
    in this package. If there is no update time found then 'unknown' is returned. If
    there is datetime found then it is rendered using the standard ckan helper.

    :param pkg_dict: the package dict
    :param date_format: date format for the return datetime
    :returns: 'unknown' or a string containing the rendered datetime and the resource
        name
    """
    latest_date, latest_resource = _get_latest_update(pkg_dict.get('resources', []))
    if latest_date is not None:
        name = latest_resource['name']
        return f'{toolkit.h.render_datetime(latest_date, date_format=date_format)} ({name})'
    # there is no available update so we return 'unknown'
    return toolkit._('unknown')

get_map_styles()

New map config overriding the marker point img.

Source code in ckanext/nhm/lib/helpers.py
487
488
489
490
491
492
493
494
495
496
497
def get_map_styles():
    """
    New map config overriding the marker point img.
    """
    return {
        'point': {
            'iconUrl': '/images/leaflet/marker-icon.png',
            'iconSize': [20, 34],
            'iconAnchor': [12, 30],
        }
    }

get_nhm_organisation_id()

Get the organisation ID for the NHM.

Returns:

Type Description

ID for the NHM organisation

Source code in ckanext/nhm/lib/helpers.py
282
283
284
285
286
287
288
289
def get_nhm_organisation_id():
    """
    Get the organisation ID for the NHM.

    :returns: ID for the NHM organisation
    """
    value = toolkit.config.get('ldap.organization.id')
    return str(value) if value is not None else None

get_object_url(resource_id, guid, version=None, include_version=True)

Retrieves the object url for the given guid in the given resource with the given version. If the version is None then the latest version of the resource is used.

The version passed (if one is passed) is not used verbatim, a call to the versioned search extension is put in to retrieve the rounded version of the resource so that the object url we create is always correct.

Parameters:

Name Type Description Default
resource_id

the resource id

required
guid

the guid of the object

required
version

the version (default: None which means use the latest version)

None
include_version

whether to include the version in the object URL or not. If this is False the version parameter is ignored (default: True)

True

Returns:

Type Description

the object url

Source code in ckanext/nhm/lib/helpers.py
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
def get_object_url(resource_id, guid, version=None, include_version=True):
    """
    Retrieves the object url for the given guid in the given resource with the given
    version. If the version is None then the latest version of the resource is used.

    The version passed (if one is passed) is not used verbatim, a call to the versioned
    search extension is put in to retrieve the rounded version of the resource so that
    the object url we create is always correct.

    :param resource_id: the resource id
    :param guid: the guid of the object
    :param version: the version (default: None which means use the latest version)
    :param include_version: whether to include the version in the object URL or not. If
        this is False the version parameter is ignored (default: True)
    :returns: the object url
    """
    if include_version:
        rounded_version = toolkit.get_action('vds_version_round')(
            {},
            {
                'resource_id': resource_id,
                'version': version,
            },
        )
    else:
        rounded_version = None
    return toolkit.url_for(
        'object.view', uuid=guid, qualified=True, version=rounded_version
    )

get_package(package_id)

Get data for the given package.

Parameters:

Name Type Description Default
package_id

the ID of the package

required
Source code in ckanext/nhm/lib/helpers.py
140
141
142
143
144
145
146
def get_package(package_id):
    """
    Get data for the given package.

    :param package_id: the ID of the package
    """
    return _get_action('package_show', {'id': package_id})

get_query_params()

Helper function to build a dict of query params To be used in urls for persistent filters.

Returns:

Type Description

dict

Source code in ckanext/nhm/lib/helpers.py
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
def get_query_params():
    """
    Helper function to build a dict of query params To be used in urls for persistent
    filters.

    :returns: dict
    """
    params = dict()

    for key in ['q', 'filters']:
        value = toolkit.request.params.get(key)
        if value:
            params[key] = value

    return params

get_record(resource_id, record_id)

Get data for the given record.

Parameters:

Name Type Description Default
resource_id

the ID of the resource holding the record

required
record_id

the ID of the record

required
Source code in ckanext/nhm/lib/helpers.py
158
159
160
161
162
163
164
165
166
167
168
def get_record(resource_id, record_id):
    """
    Get data for the given record.

    :param resource_id: the ID of the resource holding the record
    :param record_id: the ID of the record
    """
    record = _get_action(
        'vds_data_get', {'resource_id': resource_id, 'record_id': record_id}
    )
    return record.get('data', None)

get_record_count()

Get the current total number of records in the collections dataset.

Source code in ckanext/nhm/lib/helpers.py
83
84
85
86
87
88
89
90
91
92
93
94
95
@cache_region('collection_stats', 'record_count')
def get_record_count():
    """
    Get the current total number of records in the collections dataset.
    """
    record_count = 0
    try:
        dataset_statistics = _get_action('dataset_statistics', {})
        record_count = dataset_statistics.get('total', 0)
    except Exception as _e:
        # if there was a problem getting the stats return 0 and log an exception
        log.exception('Could not gather dataset statistics')
    return record_count

get_record_iiif_manifest_url(resource_id, record_id)

Given a resource ID and a record ID, a fully qualified URL to the IIIF manifest for the record's images.

Parameters:

Name Type Description Default
resource_id str

the resource ID

required
record_id int

the record ID

required

Returns:

Type Description
str

the fully qualified URL

Source code in ckanext/nhm/lib/helpers.py
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
def get_record_iiif_manifest_url(resource_id: str, record_id: int) -> str:
    """
    Given a resource ID and a record ID, a fully qualified URL to the IIIF manifest for
    the record's images.

    :param resource_id: the resource ID
    :param record_id: the record ID
    :returns: the fully qualified URL
    """
    manifest_id = toolkit.get_action('build_iiif_identifier')(
        {},
        {'builder_id': 'record', 'resource_id': resource_id, 'record_id': record_id},
    )
    return toolkit.url_for('iiif.resource', identifier=manifest_id, _external=True)

get_record_stats()

Returns a list of dictionaries containing statistics about the number of records available each week starting from 01/08/2017 and ending now.

Returns:

Type Description
List[dict]

a list of dicts

Source code in ckanext/nhm/lib/helpers.py
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
@cache_region('collection_stats', 'record_stats')
def get_record_stats() -> List[dict]:
    """
    Returns a list of dictionaries containing statistics about the number of records
    available each week starting from 01/08/2017 and ending now.

    :returns: a list of dicts
    """
    # 01/08/2017 as ms epoch
    start_version = 1501545600000
    # now as ms epoch
    end_version = int(time.time() * 1000)
    # 1 week in ms
    step = 604800000
    count_action = toolkit.get_action('vds_multi_count')

    return [
        {
            'date': datetime.fromtimestamp(version / 1000),
            'count': count_action({}, {'version': version})['total'],
        }
        for version in range(start_version, end_version, step)
    ]

get_resource(resource_id)

Get data for the given resource.

Parameters:

Name Type Description Default
resource_id

the ID of the resource

required
Source code in ckanext/nhm/lib/helpers.py
149
150
151
152
153
154
155
def get_resource(resource_id):
    """
    Get data for the given resource.

    :param resource_id: the ID of the resource
    """
    return _get_action('resource_show', {'id': resource_id})

get_resource_facets(resource)

Return a list of facets for a particular resource.

Parameters:

Name Type Description Default
resource
required
Source code in ckanext/nhm/lib/helpers.py
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
def get_resource_facets(resource):
    """
    Return a list of facets for a particular resource.

    :param resource:
    """
    # Number of facets to display
    num_facets = 10
    resource_view = resource_view_get_view(resource)
    # if facets aren't defined in the resource view, then just return
    if not resource_view.field_facets:
        return
    context = {'user': toolkit.c.user}
    # Build query parameters for the faceted search
    # We'll use the same query parameters used in the current request
    # And then add extras to perform a solr faceted query, returning
    # facets but zero results (limit=0)
    query_params = get_query_params()

    # Convert filters to a dictionary as this won't happen automatically
    # as we're retrieving raw get parameters from get_query_params
    filters = defaultdict(list)
    if query_params.get('filters'):
        for f in query_params.get('filters').split('|'):
            filter_field, filter_value = f.split(':', 1)
            filters[filter_field].append(filter_value)

    search_params = dict(
        resource_id=resource.get('id'),
        # use limit 0 as we're not interested in getting any results, just the facets
        limit=0,
        facets=resource_view.field_facets,
        q=query_params.get('q', None),
        filters=filters,
    )

    # if the show more button is clicked, a parameter is added to the query which
    # informs us we need
    # to show more facets, so use 50 for the facet limit on the given field
    for field_name in resource_view.field_facets:
        if toolkit.h.get_param_int('_{}_limit'.format(field_name)) == 0:
            search_params.setdefault('facet_limits', {})[field_name] = 50

    search = toolkit.get_action('vds_basic_query')(context, search_params)
    facets = []

    # dictionary of facet name => formatter function with camel_case_to_string defined
    # as the
    # default formatting function and then any overrides for specific edge cases
    facet_label_formatters = defaultdict(
        lambda: camel_case_to_string,
        **{
            # specific lambda for GBIF to ensure it's capitalised correctly
            'gbifIssue': lambda _: 'GBIF Issue'
        },
    )

    # Dictionary of field name => formatter function
    # Pass facet value to a formatter to get a better facet item label, if the facet
    # doesn't have a
    # formatter defined then the value is just used as is
    facet_field_label_formatters = defaultdict(
        lambda: (lambda v: v.capitalize()), **{'collectionCode': get_department}
    )

    # Loop through original facets to ensure order is preserved
    for field_name in resource_view.field_facets:
        # parse the facets into a list of dictionary values
        facets.append(
            {
                'name': field_name,
                'label': facet_label_formatters[field_name](field_name),
                'active': field_name in filters,
                'has_more': search['facets'][field_name]['details'][
                    'sum_other_doc_count'
                ]
                > 0,
                'facet_values': [
                    {
                        'name': value,
                        'label': facet_field_label_formatters[field_name](value),
                        'count': count,
                        'active': field_name in filters
                        and value in filters[field_name],
                        # loop over the top values, sorted by count desc so that the top
                        # value is first
                    }
                    for value, count in sorted(
                        search['facets'][field_name]['values'].items(),
                        key=itemgetter(1),
                        reverse=True,
                    )
                ],
            }
        )

    return facets

get_resource_fields(resource, version=None, use_request_version=False)

Retrieves the fields for the given resource. This is done using the datastore_search action. By default, the field names from the latest version of the resource are returned. However, this can be altered by either passing a version (must be an integer) or by having a version filter in the request and then passing use_request_version=True. The version is extracted from the version filter as defined by the versioned-datastore plugin. If we can start passing the version as a parameter in its own right rather than as part of the filters then we can change this code.

If the resource isn't a datastore resource then an empty list is returned.

Because the versioned_datastore plugin guarantees that the fields returned in its datastore_search responses will be in the order they were when they were ingested or sorted alphabetically if no ingestion ordering is available, no field sorting occurs in this function.

Parameters:

Name Type Description Default
resource

the resource dict

required
version

the version to request (default: None)

None
use_request_version

whether to look in the request parameters to find a version in the filters (default: False)

False

Returns:

Type Description

a list of field names

Source code in ckanext/nhm/lib/helpers.py
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
def get_resource_fields(resource, version=None, use_request_version=False):
    """
    Retrieves the fields for the given resource. This is done using the datastore_search
    action. By default, the field names from the latest version of the resource are
    returned. However, this can be altered by either passing a version (must be an
    integer) or by having a version filter in the request and then passing
    use_request_version=True. The version is extracted from the __version__ filter as
    defined by the versioned-datastore plugin. If we can start passing the version as a
    parameter in its own right rather than as part of the filters then we can change
    this code.

    If the resource isn't a datastore resource then an empty list is returned.

    Because the versioned_datastore plugin guarantees that the fields returned in its
    datastore_search responses will be in the order they were when they were ingested or
    sorted alphabetically if no ingestion ordering is available, no field sorting occurs
    in this function.

    :param resource: the resource dict
    :param version: the version to request (default: None)
    :param use_request_version: whether to look in the request parameters to find a
        version in the filters (default: False)
    :returns: a list of field names
    """
    if not resource.get('datastore_active'):
        return []

    data = {'resource_id': resource['id'], 'limit': 0}

    if version is not None:
        data['version'] = version
    elif use_request_version:
        filters = parse_request_filters()
        if '__version__' in filters:
            data['version'] = int(filters['__version__'][0])

    result = toolkit.get_action('vds_basic_query')({}, data)
    return [field['id'] for field in result.get('fields', [])]

get_resource_filter_options(resource, resource_view)

Return the available filter options for the given resource.

Parameters:

Name Type Description Default
resource

Dictionary representing a resource

required
resource_view
required

Returns:

Type Description

A dictionary associating each option's name to a dict defining: - label: The label to display to users; - checked: True if the option is currently applied.

Source code in ckanext/nhm/lib/helpers.py
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
def get_resource_filter_options(resource, resource_view):
    """
    Return the available filter options for the given resource.

    :param resource: Dictionary representing a resource
    :param resource_view:
    :returns: A dictionary associating each option's name to a dict defining:
                - label: The label to display to users;
                - checked: True if the option is currently applied.
    """
    options = resource_view_get_filter_options(resource)
    filter_list = toolkit.request.params.get('filters', '').split('|')
    filters = {}

    # If this is a gallery view, hide the has image filter
    # Only records with images will be displayed anyway
    # if resource_view['view_type'] == 'gallery':
    #     options.pop('_has_image', None)

    for filter_def in filter_list:
        try:
            (key, value) = filter_def.split(':', 1)
        except ValueError:
            continue

        if key not in filters:
            filters[key] = [value]
        else:
            filters[key].append(value)
    result = {}
    for option in options:
        if option.hide:
            continue
        result[option.name] = option.as_dict()
        result[option.name]['checked'] = (
            option.name in filters and 'true' in filters[option.name]
        )
    return result

get_resource_filter_pills(package, resource, resource_view=None)

Get filter pills.

We don't want the field group pills - these are handled separately in get_resource_field_groups

Parameters:

Name Type Description Default
resource
required
package
required
resource_view

(optional, default: None)

None
Source code in ckanext/nhm/lib/helpers.py
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
def get_resource_filter_pills(package, resource, resource_view=None):
    """
    Get filter pills.

    We don't want the field group pills - these are handled separately in
    get_resource_field_groups

    :param resource:
    :param package:
    :param resource_view:  (optional, default: None)
    """

    if not isinstance(package, dict):
        package = package.as_dict()

    filter_dict = parse_request_filters()
    extras = {'id': package['id'], 'resource_id': resource['id']}

    # there are some special filter field names provided by versioned-datastore which
    # should have
    # their values formatted differently to normal filter values
    special = {
        # display a human readable timestamp
        '__version__': lambda value: [
            time.strftime('%Y/%m/%d, %H:%M:%S', time.localtime(int(v) / 1000))
            for v in value
        ],
        # display the type of the GeoJSON filter
        '__geo__': lambda value: [json.loads(v)['type'] for v in filter_value],
    }

    pills = []

    for filter_field, filter_value in filter_dict.items():
        # if the field name stars with an underscore, don't include it in the pills (
        # unless it's
        # special!)
        if filter_field.startswith('_') and filter_field not in special:
            continue

        # remove filter from url function
        href = remove_url_filter(filter_field, filter_value, extras=extras)

        pills.append(
            {
                'label': camel_case_to_string(filter_field),
                'field': filter_field,
                # if the filter isn't a special one, just use the value
                'value': ' '.join(
                    special.get(filter_field, lambda value: value)(filter_value)
                ),
                'href': href,
            }
        )

    return pills

get_resource_gbif_errors(resource)

Return GBIF errors applicable for this resource i.e. if this the specimen resource, return the gbif errors dict.

Parameters:

Name Type Description Default
resource

return:

required
Source code in ckanext/nhm/lib/helpers.py
722
723
724
725
726
727
728
729
730
731
732
733
734
def get_resource_gbif_errors(resource):
    """
    Return GBIF errors applicable for this resource i.e. if this the specimen resource,
    return the gbif errors dict.

    :param resource: return:
    """

    # If this is a
    if resource.get('id') == get_specimen_resource_id():
        return GBIF_ERRORS
    else:
        return {}

get_sample_voucher_guid(associated_occurrence_value)

Given an associatedOccurrence value from a Sample record, returns the associated specimen GUID.

Parameters:

Name Type Description Default
associated_occurrence_value str

the associatedOccurrence value

required

Returns:

Type Description
str

the voucher specimen GUID

Source code in ckanext/nhm/lib/helpers.py
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
def get_sample_voucher_guid(associated_occurrence_value: str) -> str:
    """
    Given an associatedOccurrence value from a Sample record, returns the associated
    specimen GUID.

    :param associated_occurrence_value: the associatedOccurrence value
    :returns: the voucher specimen GUID
    """
    _, guid = associated_occurrence_value.split(':', 1)
    return guid.strip()

get_site_statistics()

Get statistics for the site.

Source code in ckanext/nhm/lib/helpers.py
56
57
58
59
60
61
62
63
64
def get_site_statistics():
    """
    Get statistics for the site.
    """
    stats = dict()
    stats['dataset_count'] = get_dataset_count()
    stats['contributor_count'] = get_contributor_count()
    stats['record_count'] = get_record_count()
    return stats

get_specimen_jsonld(uuid, version=None)

Returns the rdf representation of the given specimen uuid. The returned data is a string of json-ld data. If something goes wrong, an empty string is returned.

Parameters:

Name Type Description Default
uuid

the uuid of the specimen record

required
version

optional version for the record data

None

Returns:

Type Description

string of dumped json-ld data

Source code in ckanext/nhm/lib/helpers.py
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
def get_specimen_jsonld(uuid, version=None):
    """
    Returns the rdf representation of the given specimen uuid. The returned data is a
    string of json-ld data. If something goes wrong, an empty string is returned.

    :param uuid: the uuid of the specimen record
    :param version: optional version for the record data
    :returns: string of dumped json-ld data
    """
    data_dict = {
        'uuid': uuid,
        'format': 'json-ld',
        'version': version,
    }
    try:
        return toolkit.get_action('object_rdf')({}, data_dict)
    except toolkit.ValidationError:
        return ''

get_specimen_resource_id()

Get the ID for the specimens dataset.

Returns:

Type Description

ID for the specimen resource

Source code in ckanext/nhm/lib/helpers.py
309
310
311
312
313
314
315
316
def get_specimen_resource_id():
    """
    Get the ID for the specimens dataset.

    :returns: ID for the specimen resource
    """
    value = toolkit.config.get('ckanext.nhm.specimen_resource_id')
    return str(value) if value is not None else None

get_status_indicator()

Check if we need to display a status indicator, and if so what type.

Returns:

Type Description

'red', 'amber', or None (if no alerts)

Source code in ckanext/nhm/lib/helpers.py
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
def get_status_indicator():
    """
    Check if we need to display a status indicator, and if so what type.

    :returns: 'red', 'amber', or None (if no alerts)
    """
    # is there a status message?
    status_message = toolkit.config.get('ckanext.status.message', None)
    if status_message:
        return 'red'

    try:
        status_reports = toolkit.get_action('status_list')({}, {}).get('reports', [])
    except KeyError:
        # if the action doesn't exist
        status_reports = []

    # are there any 'bad' items?
    red_status = [r for r in status_reports if r['state'] == 'bad']
    if len(red_status) > 0:
        return 'red'

    # are there any reports with small issues?
    amber_status = [r for r in status_reports if r['state'] == 'ok']
    if len(amber_status) > 0:
        return 'amber'

group_fields_have_data(record_dict, fields)

Are any of the fields in the group populated Return true if they are; false if not.

Parameters:

Name Type Description Default
record_dict

record data

required
fields

fields to test

required

Returns:

Type Description

bool

Source code in ckanext/nhm/lib/helpers.py
889
890
891
892
893
894
895
896
897
898
899
900
def group_fields_have_data(record_dict, fields):
    """
    Are any of the fields in the group populated Return true if they are; false if not.

    :param record_dict: record data
    :param fields: fields to test
    :returns: bool
    """

    for field in fields:
        if record_dict.get(field, None):
            return True

indexlot_count()

Get the total number of index lots.

Source code in ckanext/nhm/lib/helpers.py
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
@cache_region('collection_stats', 'collection_stats')
def indexlot_count():
    """
    Get the total number of index lots.
    """
    resource_id = get_indexlot_resource_id()

    if not resource_id:
        log.error('Please configure index lot resource ID')

    context = {'user': toolkit.c.user}

    search_params = dict(
        resource_id=resource_id,
        limit=1,
    )
    search = toolkit.get_action('datastore_search')(context, search_params)
    return delimit_number(search.get('total', 0))

is_collection_resource_id(resource_id)

Given a resource ID, returns True if the resource ID is one of the designated collection IDs.

Parameters:

Name Type Description Default
resource_id str

the resource ID

required

Returns:

Type Description
bool

True if the resource ID is one of the collection resource IDs, False if not

Source code in ckanext/nhm/lib/helpers.py
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
def is_collection_resource_id(resource_id: str) -> bool:
    """
    Given a resource ID, returns True if the resource ID is one of the designated
    collection IDs.

    :param resource_id: the resource ID
    :returns: True if the resource ID is one of the collection resource IDs, False if
        not
    """
    resource_ids = {
        get_artefact_resource_id(),
        get_indexlot_resource_id(),
        get_specimen_resource_id(),
    }
    return resource_id in resource_ids

is_sysadmin()

Is user a sysadmin user.

Source code in ckanext/nhm/lib/helpers.py
865
866
867
868
869
870
def is_sysadmin():
    """
    Is user a sysadmin user.
    """
    if toolkit.c.userobj.sysadmin:
        return True

parse_request_filters()

Get the filters from the request object.

Source code in ckanext/nhm/lib/helpers.py
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
def parse_request_filters():
    """
    Get the filters from the request object.
    """
    filter_dict = {}

    try:
        filter_params = toolkit.request.params.get('filters').split('|')
    except AttributeError:
        return {}

    # Remove empty values form the filter_params
    filter_params = filter(None, filter_params)

    for filter_param in filter_params:
        field, value = filter_param.split(':', 1)
        filter_dict.setdefault(field, []).append(value)

    return filter_dict

persistent_follow_button(obj_type, obj_id)

Replaces ckan.lib.follow_button which returns an empty string for anonymous users.

For anon users this function outputs a follow button which links through to the login page.

Source code in ckanext/nhm/lib/helpers.py
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
def persistent_follow_button(obj_type, obj_id):
    """
    Replaces ckan.lib.follow_button which returns an empty string for anonymous users.

    For anon users this function outputs a follow button which links through to the
    login page.
    """
    obj_type = obj_type.lower()
    assert obj_type in toolkit.h._follow_objects

    if toolkit.c.user:
        context = {'user': toolkit.c.user}
        action = f'am_following_{obj_type}'
        following = toolkit.get_action(action)(context, {'id': obj_id})
        return toolkit.h.snippet(
            'snippets/follow_button.html',
            following=following,
            obj_id=obj_id,
            obj_type=obj_type,
        )

    return toolkit.h.snippet(
        'snippets/anon_follow_button.html', obj_id=obj_id, obj_type=obj_type
    )

record_display_field(field_name, value)

Decide whether to display a field Evaluates whether a field has value.

Parameters:

Name Type Description Default
field_name
required
value
required

Returns:

Type Description

bool - true to display field; false not to

Source code in ckanext/nhm/lib/helpers.py
873
874
875
876
877
878
879
880
881
882
883
884
885
886
def record_display_field(field_name, value):
    """
    Decide whether to display a field Evaluates whether a field has value.

    :param field_name:
    :param value:
    :returns: bool - true to display field; false not to
    """

    # If this is a string, strip it before evaluating
    if isinstance(value, str):
        value = value.strip()

    return bool(value)

remove_url_filter(field, value, extras=None)

The CKAN built in functions remove_url_param / add_url_param cannot handle multiple filters which are concatenated with |, not separate query params This replaces remove_url_param for filters.

Parameters:

Name Type Description Default
field

the field to remove the filter for

required
value

the value of the field to remove the filter for

required
extras

extra parameters to include in the created URL

None

Returns:

Type Description

a URL

Source code in ckanext/nhm/lib/helpers.py
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
def remove_url_filter(field, value, extras=None):
    """
    The CKAN built in functions remove_url_param / add_url_param cannot handle multiple
    filters which are concatenated with |, not separate query params This replaces
    remove_url_param for filters.

    :param field: the field to remove the filter for
    :param value: the value of the field to remove the filter for
    :param extras: extra parameters to include in the created URL
    :returns: a URL
    """

    params = dict(toolkit.request.params)
    try:
        del params['filters']
    except KeyError:
        pass
    else:
        filters = parse_request_filters()
        if field in filters:
            # Convert all filters to unicode for ease of comparison
            filters[field] = [f.lower() for f in filters[field]]
            # Remove the filter value form the current filters
            value_list = value if isinstance(value, list) else [value]
            for value_item in value_list:
                try:
                    # Try and remove the item from the filters
                    filters[field].remove(value_item.lower())
                except ValueError:
                    continue

            # If the filters values for the field are empty, remove the whole field
            if not filters[field]:
                del filters[field]

        # Combine the filters again
        filter_parts = []
        for filter_field, filter_values in filters.items():
            for filter_value in filter_values:
                filter_parts.append(f'{filter_field}:{filter_value}')
        # If we have filter parts, add them back to the params dict
        if filter_parts:
            filters = '|'.join(filter_parts)
            return toolkit.h.remove_url_param('filters', replace=filters, extras=extras)
    return toolkit.h.remove_url_param('filters', extras=extras)

render_epoch(epoch_timestamp, in_milliseconds=True, date_format='%Y-%m-%d %H:%M:%S (UTC)')

Renders an epoch timestamp in the given date format. The timestamp is rendered in UTC.

Parameters:

Name Type Description Default
epoch_timestamp

the timestamp, represented as the number of seconds (or milliseconds if in_milliseconds is True) since the UNIX epoch

required
in_milliseconds

whether the timestamp is in milliseconds or seconds. By default this is True and therefore the timestamp is expected to be in milliseconds

True
date_format

the output format. This will be passed straight to datetime's strftime function and therefore uses its keywords etc. Defaults to: %Y-%m-%d %H:%M:%S (UTC)

'%Y-%m-%d %H:%M:%S (UTC)'

Returns:

Type Description

a string rendering of the timestamp using the

Source code in ckanext/nhm/lib/helpers.py
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
def render_epoch(
    epoch_timestamp, in_milliseconds=True, date_format='%Y-%m-%d %H:%M:%S (UTC)'
):
    """
    Renders an epoch timestamp in the given date format. The timestamp is rendered in
    UTC.

    :param epoch_timestamp: the timestamp, represented as the number of seconds (or
        milliseconds if in_milliseconds is True) since the UNIX epoch
    :param in_milliseconds: whether the timestamp is in milliseconds or seconds. By
        default this is True and therefore the timestamp is expected to be in
        milliseconds
    :param date_format: the output format. This will be passed straight to datetime's
        strftime function and therefore uses its keywords etc. Defaults to: %Y-%m-%d
        %H:%M:%S (UTC)
    :returns: a string rendering of the timestamp using the
    """
    if in_milliseconds:
        epoch_timestamp = epoch_timestamp / 1000
    return datetime.utcfromtimestamp(epoch_timestamp).strftime(date_format)

resource_is_dwc(resource)

Is the resource format DwC?

Parameters:

Name Type Description Default
resource

return:

required
Source code in ckanext/nhm/lib/helpers.py
663
664
665
666
667
668
669
def resource_is_dwc(resource):
    """
    Is the resource format DwC?

    :param resource: return:
    """
    return bool(resource.get('format').lower() == 'dwc')

resource_view_get_field_groups(resource)

Return dictionary of field groups.

Parameters:

Name Type Description Default
resource

resource dict

required

Returns:

Type Description

OrderedDict of fields

Source code in ckanext/nhm/lib/helpers.py
517
518
519
520
521
522
523
524
525
526
def resource_view_get_field_groups(resource):
    """
    Return dictionary of field groups.

    :param resource: resource dict
    :returns: OrderedDict of fields
    """
    view_cls = resource_view_get_view(resource)

    return view_cls.get_field_groups(resource)

resource_view_get_filterable_fields(resource)

Retrieves the fields that can be filtered on.

Returns:

Type Description

a list of sorted fields

Source code in ckanext/nhm/lib/helpers.py
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
def resource_view_get_filterable_fields(resource):
    """
    Retrieves the fields that can be filtered on.

    :returns: a list of sorted fields
    """
    # if this isn't a datastore resource, return an empty list
    if not resource.get('datastore_active'):
        return []

    # otherwise, query the datastore for the fields
    data = {
        'resource_id': resource['id'],
        'limit': 0,
    }
    fields = toolkit.get_action('vds_basic_query')({}, data).get('fields', [])

    # sort and filter the fields ensuring we only return string type fields and don't
    # return the id
    # field
    return sorted(f['id'] for f in fields if f['type'] == 'string' and f['id'] != '_id')

resource_view_state(resource_view_json, resource_json)

Alter the recline view resource, adding in state info.

Parameters:

Name Type Description Default
resource_view_json
required
resource_json
required
Source code in ckanext/nhm/lib/helpers.py
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
def resource_view_state(resource_view_json, resource_json):
    """
    Alter the recline view resource, adding in state info.

    :param resource_view_json:
    :param resource_json:
    """
    resource_view = json.loads(resource_view_json)
    resource = json.loads(resource_json)

    fields = get_resource_fields(resource, use_request_version=True)

    # Initiate the resource view
    view = resource_view_get_view(resource)
    # And get the state
    resource_view['state'] = view.get_slickgrid_state()

    # there is an annoying feature/bug in slickgrid that if fitColumns=True and grid is wider than
    # available viewport, slickgrid columns cannot be resized until fitColumns is deactivated. So to
    # fix, we're going to work out how many columns are in the dataset to decide whether or not to
    # turn on fitColumns. Messy, but better than trying to hack around with slickgrid
    viewport_max_width = 920
    col_width = 100
    fit_columns = (len(fields) * col_width) < viewport_max_width
    # TODO: This can be merged into get_slickgrid_state
    resource_view['state']['fitColumns'] = fit_columns

    # ID and DQI always first
    columns_order = ['_id']
    if 'gbifIssue' in fields:
        columns_order.append('gbifIssue')
    # add other useful DwC fields
    if 'currentScientificName' in fields:
        # prefer current scientific name
        columns_order.append('currentScientificName')
    elif 'scientificName' in fields:
        columns_order.append('scientificName')
    for f in [
        'typeStatus',
        'type',
        'phylum',
        'class',
        'order',
        'family',
        'genus',
        'specificEpithet',
        'infraspecificEpithet',
        'locality',
        'country',
        'recordedBy',
        'catalogNumber',
        'associatedMedia',
        'preservative',
        'collectionCode',
        'year',
        'month',
        'day',
    ]:
        if f in fields:
            columns_order.append(f)
    # Add the rest of the columns to the columns order
    columns_order += [f for f in fields if f not in columns_order]
    resource_view['state']['columnsOrder'] = list(columns_order)

    # this is a bit of a hack but not the worst thing that's ever happened. This code is here to
    # solve a specific problem whereby if a user is viewing an old version of the data and in newer
    # versions of the data new columns have been added, the user will see these new columns in the
    # slick grid header row (no data will be shown for them because the column doesn't exist in the
    # old records). This happens because when slick is setting up it requests the data and the
    # column headers in separate datastore_search requests, this is inefficient but also problematic
    # because the column headers request doesn't include any of the filters or query parameters that
    # the data request does. This means that we lose the version information and will return the
    # headers in the latest version even if the user is actually looking at older data. By
    # retrieving the current fields for the resource here and then using this list to generate a
    # list of hidden columns to pass to slick we can make sure these columns don't appear. If we
    # stop using slick or slick is fixed we can stop doing this.
    latest_fields = get_resource_fields(resource, use_request_version=False)
    resource_view['state']['hiddenColumns'] = [
        f for f in latest_fields if f not in fields
    ]

    if view.grid_column_widths:
        for column, width in view.grid_column_widths.items():
            resource_view['state']['columnsWidth'].append(
                {'column': column, 'width': width}
            )

    try:
        return json.dumps(resource_view)
    except TypeError:
        return {}

route_exists(route)

Simple helper for checking if a flask route exists.

Parameters:

Name Type Description Default
route

endpoint name, as passed to url_for

required

Returns:

Type Description

bool

Source code in ckanext/nhm/lib/helpers.py
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
def route_exists(route):
    """
    Simple helper for checking if a flask route exists.

    :param route: endpoint name, as passed to url_for
    :returns: bool
    """
    try:
        url = toolkit.url_for(route)
        return True
    except BuildError:
        return False

social_share_text(pkg_dict=None, res_dict=None, rec_dict=None)

Generate social share text for a package.

Parameters:

Name Type Description Default
pkg_dict
None

Returns:

Type Description
Source code in ckanext/nhm/lib/helpers.py
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
def social_share_text(pkg_dict=None, res_dict=None, rec_dict=None):
    """
    Generate social share text for a package.

    :param pkg_dict:
    :returns:
    """
    text = []
    if rec_dict:
        title_field = res_dict.get('_title_field', None)
        if title_field and rec_dict.get(title_field, None):
            text.append(rec_dict[title_field])
        else:
            text.append('Record {}'.format(rec_dict['_id']))
    elif res_dict:
        text.append(res_dict['name'])
    elif pkg_dict:
        text.append(pkg_dict['title'] or pkg_dict['name'])

    text.append('on the @NHM_London Data Portal')

    try:
        text.append(f'DOI: {"/".join(["https://doi.org", pkg_dict["doi"]])}')
    except KeyError:
        pass

    return quote(' '.join(map(str, text)).encode('utf8'))

update_frequency_get_label(value)

Get the label for this update frequency.

Parameters:

Name Type Description Default
value

return:

required
Source code in ckanext/nhm/lib/helpers.py
178
179
180
181
182
183
184
185
186
def update_frequency_get_label(value):
    """
    Get the label for this update frequency.

    :param value: return:
    """
    for v, label in UPDATE_FREQUENCIES:
        if v == value:
            return label

url_for_collection_view(view_type=None, filters={})

Return URL to link through to specimen dataset view, with optional search params.

Parameters:

Name Type Description Default
view_type

grid to link to - grid or map (optional, default: None)

None
kwargs

search filter params

required
filters

(optional, default: {})

{}

Returns:

Type Description

url

Source code in ckanext/nhm/lib/helpers.py
203
204
205
206
207
208
209
210
211
212
213
def url_for_collection_view(view_type=None, filters={}):
    """
    Return URL to link through to specimen dataset view, with optional search params.

    :param view_type: grid to link to - grid or map (optional, default: None)
    :param kwargs: search filter params
    :param filters:  (optional, default: {})
    :returns: url
    """
    resource_id = get_specimen_resource_id()
    return url_for_resource_view(resource_id, view_type, filters)

url_for_indexlot_view()

Return URL to link through to index lot resource view.

Returns:

Type Description

url

Source code in ckanext/nhm/lib/helpers.py
216
217
218
219
220
221
222
223
def url_for_indexlot_view():
    """
    Return URL to link through to index lot resource view.

    :returns: url
    """
    resource_id = get_indexlot_resource_id()
    return url_for_resource_view(resource_id)

url_for_resource_view(resource_id, view_type=None, filters={})

Get URL to link to resource view. If no view type is specified, the first view will be used.

Parameters:

Name Type Description Default
resource_id
required
filters

(optional, default: {})

{}
view_type

(optional, default: None)

None
Source code in ckanext/nhm/lib/helpers.py
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
def url_for_resource_view(resource_id, view_type=None, filters={}):
    """
    Get URL to link to resource view. If no view type is specified, the first view will
    be used.

    :param resource_id:
    :param filters: (optional, default: {})
    :param view_type: (optional, default: None)
    """

    try:
        views = toolkit.get_action('resource_view_list')({}, {'id': resource_id})
    except toolkit.ObjectNotFound:
        return None
    else:
        if not views:
            return None

        if not view_type:
            view = views[0]
        else:
            for view in views:
                if view['view_type'] == view_type:
                    break

        filters = '|'.join([f'{k}:{v}' for k, v in filters.items()])

        return toolkit.url_for(
            'resource.read',
            id=view['package_id'],
            resource_id=view['resource_id'],
            view_id=view['id'],
            filters=filters,
        )