|
11 | 11 |
|
12 | 12 | from functools import wraps
|
13 | 13 |
|
14 |
| -from flask import g, make_response, redirect, request, session, url_for |
| 14 | +from flask import current_app, g, make_response, redirect, request, session, url_for |
15 | 15 | from flask_login import login_required
|
16 | 16 | from invenio_communities.communities.resources.serializer import (
|
17 | 17 | UICommunityJSONSerializer,
|
18 | 18 | )
|
19 | 19 | from invenio_communities.proxies import current_communities
|
20 | 20 | from invenio_pidstore.errors import PIDDoesNotExistError
|
21 | 21 | from invenio_rdm_records.proxies import current_rdm_records
|
| 22 | +from invenio_rdm_records.resources.serializers.signposting import ( |
| 23 | + FAIRSignpostingProfileLvl1Serializer, |
| 24 | +) |
22 | 25 | from invenio_records_resources.services.errors import PermissionDeniedError
|
23 | 26 | from sqlalchemy.orm.exc import NoResultFound
|
24 | 27 |
|
@@ -365,20 +368,88 @@ def view(**kwargs):
|
365 | 368 | return view
|
366 | 369 |
|
367 | 370 |
|
368 |
| -def add_signposting(f): |
369 |
| - """Add signposting link to view's response headers.""" |
| 371 | +def _get_header(rel, value, link_type=None): |
| 372 | + header = f'<{value}> ; rel="{rel}"' |
| 373 | + if link_type: |
| 374 | + header += f' ; type="{link_type}"' |
| 375 | + return header |
| 376 | + |
| 377 | + |
| 378 | +def _get_signposting_collection(pid_value): |
| 379 | + ui_url = record_url_for(pid_value=pid_value) |
| 380 | + return _get_header("collection", ui_url, "text/html") |
| 381 | + |
| 382 | + |
| 383 | +def _get_signposting_describes(pid_value): |
| 384 | + ui_url = record_url_for(pid_value=pid_value) |
| 385 | + return _get_header("describes", ui_url, "text/html") |
| 386 | + |
| 387 | + |
| 388 | +def _get_signposting_linkset(pid_value): |
| 389 | + api_url = record_url_for(_app="api", pid_value=pid_value) |
| 390 | + return _get_header("linkset", api_url, "application/linkset+json") |
| 391 | + |
| 392 | + |
| 393 | +def add_signposting_landing_page(f): |
| 394 | + """Add signposting links to the landing page view's response headers.""" |
370 | 395 |
|
371 | 396 | @wraps(f)
|
372 | 397 | def view(*args, **kwargs):
|
373 | 398 | response = make_response(f(*args, **kwargs))
|
374 | 399 |
|
375 | 400 | # Relies on other decorators having operated before it
|
376 |
| - pid_value = kwargs["pid_value"] |
377 |
| - signposting_link = record_url_for(_app="api", pid_value=pid_value) |
| 401 | + record = kwargs["record"] |
378 | 402 |
|
379 |
| - response.headers["Link"] = ( |
380 |
| - f'<{signposting_link}> ; rel="linkset" ; type="application/linkset+json"' # fmt: skip |
| 403 | + signposting_headers = FAIRSignpostingProfileLvl1Serializer().serialize_object( |
| 404 | + record.to_dict() |
381 | 405 | )
|
| 406 | + |
| 407 | + response.headers["Link"] = signposting_headers |
| 408 | + |
| 409 | + return response |
| 410 | + |
| 411 | + return view |
| 412 | + |
| 413 | + |
| 414 | +def add_signposting_content_resources(f): |
| 415 | + """Add signposting links to the content resources view's response headers.""" |
| 416 | + |
| 417 | + @wraps(f) |
| 418 | + def view(*args, **kwargs): |
| 419 | + response = make_response(f(*args, **kwargs)) |
| 420 | + |
| 421 | + # Relies on other decorators having operated before it |
| 422 | + pid_value = kwargs["pid_value"] |
| 423 | + |
| 424 | + signposting_headers = [ |
| 425 | + _get_signposting_collection(pid_value), |
| 426 | + _get_signposting_linkset(pid_value), |
| 427 | + ] |
| 428 | + |
| 429 | + response.headers["Link"] = " , ".join(signposting_headers) |
| 430 | + |
| 431 | + return response |
| 432 | + |
| 433 | + return view |
| 434 | + |
| 435 | + |
| 436 | +def add_signposting_metadata_resources(f): |
| 437 | + """Add signposting links to the metadata resources view's response headers.""" |
| 438 | + |
| 439 | + @wraps(f) |
| 440 | + def view(*args, **kwargs): |
| 441 | + response = make_response(f(*args, **kwargs)) |
| 442 | + |
| 443 | + # Relies on other decorators having operated before it |
| 444 | + pid_value = kwargs["pid_value"] |
| 445 | + |
| 446 | + signposting_headers = [ |
| 447 | + _get_signposting_describes(pid_value), |
| 448 | + _get_signposting_linkset(pid_value), |
| 449 | + ] |
| 450 | + |
| 451 | + response.headers["Link"] = " , ".join(signposting_headers) |
| 452 | + |
382 | 453 | return response
|
383 | 454 |
|
384 | 455 | return view
|
|
0 commit comments