Skip to content

Commit

Permalink
serializers: add record link factory
Browse files Browse the repository at this point in the history
  • Loading branch information
Glignos committed Aug 2, 2019
1 parent d87791f commit 3812759
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ def test_simple_flow(client):

# retrieve record
res = client.get('https://localhost:5000/records/1')

# Check if the files link is present
assert res.json['links']['files'].endswith('{0}/files') == \
'https://localhost:5000/records/1/files'
assert res.status_code == 200
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,13 @@ def _(x):
search_index='records',
search_type=None,
record_serializers={
'application/json': ('{{ cookiecutter.package_name }}.records.serializers'
':json_v1_response'),
'application/json': ('{{ cookiecutter.package_name }}.records.serializers:json_v1_response'),
},
search_serializers={
'application/json': ('{{ cookiecutter.package_name }}.records.serializers'
':json_v1_search'),
'application/json': ('{{ cookiecutter.package_name }}.records.serializers:json_v1_search'),
},
record_loaders={
'application/json': ('{{ cookiecutter.package_name }}.records.loaders'
':json_v1'),
'application/json': ('{{ cookiecutter.package_name }}.records.loaders:json_v1'),
},
list_route='/records/',
item_route='/records/<pid(recid,'
Expand All @@ -48,7 +45,9 @@ def _(x):
read_permission_factory_imp=check_elasticsearch,
update_permission_factory_imp=allow_all,
delete_permission_factory_imp=allow_all,
list_permission_factory_imp=allow_all
list_permission_factory_imp=allow_all,
links_factory_imp='{{ cookiecutter.package_name }}.'
'records.links.record_links:record_links_factory',
),
}
"""REST API for {{cookiecutter.project_shortname}}."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def init_config(self, app):
elif k == 'PIDSTORE_RECID_FIELD':
app.config['PIDSTORE_RECID_FIELD'] = getattr(config, k)
elif k == 'FILES_REST_PERMISSION_FACTORY':
app.config['FILES_REST_PERMISSION_FACTORY'] = getattr(config, k)
app.config['FILES_REST_PERMISSION_FACTORY'] = \
getattr(config, k)
else:
for n in ['RECORDS_REST_ENDPOINTS', 'RECORDS_UI_ENDPOINTS',
'RECORDS_REST_FACETS', 'RECORDS_REST_SORT_OPTIONS',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2019 CERN.
#
# My site is free software; you can redistribute it and/or modify it under
# the terms of the MIT License; see LICENSE file for more details.

"""Module for defining link factories"""

from __future__ import absolute_import, print_function
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2019 CERN.
#
# My site is free software; you can redistribute it and/or modify it under
# the terms of the MIT License; see LICENSE file for more details.

from __future__ import absolute_import, print_function

from flask import url_for, g

from invenio_records_rest.proxies import current_records_rest


def record_links_factory(pid, record=None, **kwargs):
"""Factory for record links generation.
:param pid: A Persistent Identifier instance.
:returns: Dictionary containing a list of useful links for the record.
"""
# Load pid in the global context for the url construction
g.pid = pid.pid_value
record_endpoint = '.{0}_item'.format(
current_records_rest.default_endpoint_prefixes[pid.pid_type])
links = dict(
self=url_for(record_endpoint, pid_value=pid.pid_value, _external=True),
files=url_for(
'invenio_records_files.bucket_api',
pid_value=pid.pid_value,
_external=True
)
)
return links

0 comments on commit 3812759

Please sign in to comment.