Skip to content

Helpers

date_or_none(date_object_or_string)

Try and convert the given object into a datetime; if not possible, return None.

Parameters:

Name Type Description Default
date_object_or_string

a datetime or date string

required

Returns:

Type Description

datetime or None

Source code in ckanext/doi/lib/helpers.py
47
48
49
50
51
52
53
54
55
56
57
58
59
def date_or_none(date_object_or_string):
    """
    Try and convert the given object into a datetime; if not possible, return None.

    :param date_object_or_string: a datetime or date string
    :return: datetime or None
    """
    if isinstance(date_object_or_string, datetime):
        return date_object_or_string
    elif isinstance(date_object_or_string, str):
        return parser.parse(date_object_or_string)
    else:
        return None

doi_test_mode()

Determines whether we're running in test mode.

Returns:

Type Description

bool

Source code in ckanext/doi/lib/helpers.py
62
63
64
65
66
67
68
def doi_test_mode():
    """
    Determines whether we're running in test mode.

    :return: bool
    """
    return toolkit.asbool(get_setting('ckanext.doi.test_mode', default=get_debug()))

get_site_title()

Helper function to return the config site title, if it exists.

Returns:

Type Description

str site title

Source code in ckanext/doi/lib/helpers.py
26
27
28
29
30
31
32
def get_site_title():
    """
    Helper function to return the config site title, if it exists.

    :returns: str site title
    """
    return toolkit.config.get('ckanext.doi.site_title')

get_site_url()

Get the site URL.

Try and use ckanext.doi.site_url but if that's not set use ckan.site_url.

Source code in ckanext/doi/lib/helpers.py
35
36
37
38
39
40
41
42
43
44
def get_site_url():
    """
    Get the site URL.

    Try and use ckanext.doi.site_url but if that's not set use ckan.site_url.
    """
    site_url = toolkit.config.get(
        'ckanext.doi.site_url', toolkit.config.get('ckan.site_url', '')
    )
    return site_url.rstrip('/')

package_get_year(pkg_dict)

Helper function to return the package year published.

Parameters:

Name Type Description Default
pkg_dict

return:

required
Source code in ckanext/doi/lib/helpers.py
14
15
16
17
18
19
20
21
22
23
def package_get_year(pkg_dict):
    """
    Helper function to return the package year published.

    :param pkg_dict: return:
    """
    if not isinstance(pkg_dict['metadata_created'], datetime):
        pkg_dict['metadata_created'] = parser.parse(pkg_dict['metadata_created'])

    return pkg_dict['metadata_created'].year