Skip to content

External links

GBIFSite dataclass

Bases: Site

Site that provides links to species and occurrence pages associated with the given record.

Source code in ckanext/nhm/lib/external_links.py
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
class GBIFSite(Site):
    """
    Site that provides links to species and occurrence pages associated with the given
    record.
    """

    def get_links(self, record: dict) -> List[Link]:
        links = []

        try:
            gbif_record = _get_gbif_record(
                record.get('occurrenceID'), record.get('institutionCode', 'NHMUK')
            )
            if gbif_record:
                links_parts = [
                    ('https://gbif.org/occurrence/{}', 'catalogNumber', 'key'),
                    (
                        'https://gbif.org/species/{}',
                        'scientificName',
                        'acceptedTaxonKey',
                    ),
                ]
                for url_template, name_key, url_key in links_parts:
                    if name_key in gbif_record and url_key in gbif_record:
                        links.append(
                            Link(
                                gbif_record[name_key],
                                url_template.format(gbif_record[url_key]),
                            )
                        )
        except requests.RequestException:
            pass

        return links

Phenome10kSite dataclass

Bases: Site

Site which uses the GBIF API and Phenome10k API to find associated 3D data on Phenome10k.

Source code in ckanext/nhm/lib/external_links.py
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
class Phenome10kSite(Site):
    """
    Site which uses the GBIF API and Phenome10k API to find associated 3D data on
    Phenome10k.
    """

    def get_links(self, record: dict) -> List[Link]:
        links = []
        try:
            gbif_record = _get_gbif_record(
                record.get('occurrenceID'), record.get('institutionCode', 'NHMUK')
            )
            if gbif_record and 'key' in gbif_record:
                p10k_record = _get_phenome10k_record(gbif_record['key'])
                if p10k_record:
                    links.append(
                        Link(p10k_record['scientific_name'], p10k_record['url'])
                    )
        except (requests.RequestException, KeyError):
            pass
        return links

RankedTemplateSite dataclass

Bases: Site

A site based on a templated URL filled in with the taxonomy ranks available in the record.

Source code in ckanext/nhm/lib/external_links.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
@dataclass
class RankedTemplateSite(Site):
    """
    A site based on a templated URL filled in with the taxonomy ranks available in the
    record.
    """

    url_template: str

    def get_links(self, record: dict) -> List[Link]:
        ranks = extract_ranks(record)
        used = set()
        # return the links - removing any duplicates
        return [
            Link(rank, self.url_template.format(rank))
            for rank in ranks.values()
            if not (rank in used or used.add(rank))
        ]

Site dataclass

Bases: ABC

An external site we can link to from a specimen record page.

Source code in ckanext/nhm/lib/external_links.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
@dataclass
class Site(abc.ABC):
    """
    An external site we can link to from a specimen record page.
    """

    name: str
    icon_url: str

    @abc.abstractmethod
    def get_links(self, record: dict) -> List[Link]:
        """
        Returns a list of Links from the passed record.

        :param record: the record dict
        """
        ...

Returns a list of Links from the passed record.

Parameters:

Name Type Description Default
record dict

the record dict

required
Source code in ckanext/nhm/lib/external_links.py
28
29
30
31
32
33
34
35
@abc.abstractmethod
def get_links(self, record: dict) -> List[Link]:
    """
    Returns a list of Links from the passed record.

    :param record: the record dict
    """
    ...

get_sites(record)

Given a record, returns a list of sites that may be able to provide relevant links.

Parameters:

Name Type Description Default
record dict

a record dict

required
Source code in ckanext/nhm/lib/external_links.py
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
def get_sites(record: dict) -> List[Site]:
    """
    Given a record, returns a list of sites that may be able to provide relevant links.

    :param record: a record dict
    """
    searches = {
        'BMNH(E)': [BHL, CoL, GBIF, P10K, LADL],
        'BOT': [BHL, CoL, GBIF, P10K, LADL],
        'MIN': [Mindat, LADL],
        'PAL': [PBDB, GBIF, P10K, LADL],
        'ZOO': [BHL, CoL, GBIF, P10K, LADL],
        # if there is no collection code, just check the BHL and CoL. This catches index
        # lot entries
        None: [BHL, CoL, LADL],
    }
    # if no collection code is available, default to None
    return searches.get(record.get('collectionCode', None), [])