Skip to content

Record

Controller for displaying an individual record Loads all the data and then defers render function to view objects.

dwc(package_name, resource_id, record_id, version)

View an individual record using the DwC view.

Parameters:

Name Type Description Default
package_name

the package name or ID, doesn't matter which

required
record_id

the record ID

required
resource_id

the resource ID

required
version

optional record version, defaults to None which will be interpreted as now

required

Returns:

Type Description

the rendered view template from the correct record view class

Source code in ckanext/nhm/routes/record.py
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
@blueprint.route(
    '/dataset/<package_name>/resource/<resource_id>/record/<record_id>/dwc',
    defaults={'version': None},
)
@blueprint.route(
    '/dataset/<package_name>/resource/<resource_id>/record/<record_id>/dwc/<int:version>'
)
def dwc(package_name, resource_id, record_id, version):
    """
    View an individual record using the DwC view.

    :param package_name: the package name or ID, doesn't matter which
    :param record_id: the record ID
    :param resource_id: the resource ID
    :param version: optional record version, defaults to None which will be interpreted
        as now
    :returns: the rendered view template from the correct record view class
    """
    record = Record(
        record_id,
        package_id_or_name=package_name,
        resource_id=resource_id,
        version=version,
    )
    update_render_context(record)
    toolkit.c.additional_view = True
    return DarwinCoreView().render_record(toolkit.c)

init_jinja_extensions()

This hook is called before the first request is received by the app and therefore allows us to ensure that the taxonomy extension is loaded into jinja2 before it's used.

Source code in ckanext/nhm/routes/record.py
69
70
71
72
73
74
75
76
77
@blueprint.before_app_first_request
def init_jinja_extensions():
    """
    This hook is called before the first request is received by the app and therefore
    allows us to ensure that the taxonomy extension is loaded into jinja2 before it's
    used.
    """
    # Load the taxonomy formatter
    current_app.jinja_env.add_extension(TaxonomyFormatExtension)

json_view(package_name, resource_id, record_id, version)

View the record as JSON.

Parameters:

Name Type Description Default
package_name

the package name or ID, we don't actually need this

required
resource_id

the resource ID, we do need this!

required
record_id

the record ID, stunningly enough we need this too

required
version

optional record version, defaults to None which will be interpreted as now

required

Returns:

Type Description

the record data as a dict which will be turned into JSON automatically by Flask, huzzah

Source code in ckanext/nhm/routes/record.py
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
@blueprint.route(
    '/dataset/<package_name>/resource/<resource_id>/record/<record_id>.json',
    defaults={'version': None},
)
@blueprint.route(
    '/dataset/<package_name>/resource/<resource_id>/record/<record_id>.json/<int:version>'
)
def json_view(package_name, resource_id, record_id, version):
    """
    View the record as JSON.

    :param package_name: the package name or ID, we don't actually need this
    :param resource_id: the resource ID, we do need this!
    :param record_id: the record ID, stunningly enough we need this too
    :param version: optional record version, defaults to None which will be interpreted
        as now
    :returns: the record data as a dict which will be turned into JSON automatically by
        Flask, huzzah
    """
    return Record(record_id, resource_id=resource_id, version=version).data

prepare_image(image)

Given an image object, return a dict of information about it for the view.

Parameters:

Name Type Description Default
image RecordImage

the RecordImage object

required

Returns:

Type Description
dict

a dict of info

Source code in ckanext/nhm/routes/record.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def prepare_image(image: RecordImage) -> dict:
    """
    Given an image object, return a dict of information about it for the view.

    :param image: the RecordImage object
    :returns: a dict of info
    """
    license_link = link_to(image.title, image.url, target='_blank')
    return {
        'title': image.title,
        'href': image.url,
        'download': image.download_url,
        'copyright': f'{license_link}<br /><small>{image.rights}</small>',
        'record_id': image.record.id,
        'resource_id': image.record.resource_id,
        'link': image.record.url(),
    }

update_render_context(record)

Sets the various expected context variables for the templates.

The values set here are used in some of our templates and some core CKAN ones too, it's a bit of a mess (especially for packages).

Source code in ckanext/nhm/routes/record.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def update_render_context(record: Record):
    """
    Sets the various expected context variables for the templates.

    The values set here are used in some of our templates and some core CKAN ones too,
    it's a bit of a mess (especially for packages).
    """
    toolkit.c.pkg_dict = toolkit.c.package = toolkit.c.pkg = record.package
    toolkit.c.resource = record.resource
    image_field = record.image_field
    # the templates expect for the record dict to not include the images
    toolkit.c.record_dict = {
        field: value for field, value in record.data.items() if field != image_field
    }
    toolkit.c.version = record.version
    toolkit.c.record_title = record.title
    toolkit.c.images = list(map(prepare_image, record.images))
    geojson = record.geojson
    # something expects this as json apparently, sigh
    toolkit.c.record_map = json.dumps(geojson) if geojson else None

view(package_name, resource_id, record_id, version)

View an individual record.

Parameters:

Name Type Description Default
package_name

the package name or ID, doesn't matter which

required
record_id

the record ID

required
resource_id

the resource ID

required
version

optional record version, defaults to None which will be interpreted as now

required

Returns:

Type Description

the rendered view template from the correct record view class

Source code in ckanext/nhm/routes/record.py
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
@blueprint.route(
    '/dataset/<package_name>/resource/<resource_id>/record/<record_id>',
    defaults={'version': None},
)
@blueprint.route(
    '/dataset/<package_name>/resource/<resource_id>/record/<record_id>/<int:version>'
)
def view(package_name, resource_id, record_id, version):
    """
    View an individual record.

    :param package_name: the package name or ID, doesn't matter which
    :param record_id: the record ID
    :param resource_id: the resource ID
    :param version: optional record version, defaults to None which will be interpreted
        as now
    :returns: the rendered view template from the correct record view class
    """
    record = Record(
        record_id,
        package_id_or_name=package_name,
        resource_id=resource_id,
        version=version,
    )
    update_render_context(record)
    view_cls = resource_view_get_view(record.resource)
    return view_cls.render_record(toolkit.c)