Coverage for portality/view/openurl.py: 92%
52 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-20 13:43 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-20 13:43 +0000
1import re
2from flask import Blueprint, request, redirect, url_for, render_template, abort
3from portality.models import OpenURLRequest, UnsupportedOpenURLQuery
4from portality.lib import plausible
5from portality.core import app
6from urllib.parse import unquote
7from portality.ui import templates
9blueprint = Blueprint('openurl', __name__)
12@blueprint.route("/openurl", methods=["GET", "POST"])
13def openurl():
14 # check that we've been given some arguments at all
15 if len(request.values) == 0:
16 abort(404)
18 # Decode and unquote the query string, which comes in as bytes.
19 qs = unquote(request.query_string.decode('utf-8'))
21 # Validate the query syntax version and build an object representing it
22 parser_response = parse_query(query=qs, req=request)
24 # theoretically this can return None, so catch it
25 if parser_response is None:
26 abort(404)
28 # If it's not parsed to an OpenURLRequest, it's a redirect URL to try again.
29 if type(parser_response) != OpenURLRequest:
30 return redirect(parser_response, 301)
32 # Log this request to analytics
33 plausible.send_event(app.config.get('ANALYTICS_CATEGORY_OPENURL', 'OpenURL'),
34 action=parser_response.genre,
35 label=qs)
37 # Get the OpenURLRequest object to issue a query and supply a url for the result
38 try:
39 result_url = parser_response.get_result_url()
40 except UnsupportedOpenURLQuery as e:
41 app.logger.info("Rejecting OpenURL request as a bad request: " + str(e))
42 return render_template(templates.OPENURL_400), 400
44 if result_url:
45 return redirect(result_url)
46 else:
47 abort(404)
50def parse_query(query, req):
51 """
52 Create the model which holds the query
53 :param query: The query string from the request URL (separated for the sake of analytics)
54 :param req: an incoming OpenURL request
55 :return: an object representing the query, or a redirect to the reissued query, or None if failed.
56 """
57 # Check if this is new or old syntax, translate if necessary
58 if "url_ver=Z39.88-2004" not in query:
59 app.logger.info("Legacy OpenURL 0.1 request: " + unquote(req.url))
60 return old_to_new(req)
62 app.logger.info("OpenURL 1.0 request: " + unquote(req.url))
64 # Wee function to strip of the referent namespace prefix from parameters
65 rem_ns = lambda x: re.sub('rft.', '', x)
67 # Pack the list of parameters into a dictionary, while un-escaping the string.
68 dict_params = {rem_ns(key): value for (key, value) in req.values.items()}
70 # Create an object to represent this OpenURL request.
71 try:
72 query_object = OpenURLRequest(**dict_params)
73 except:
74 query_object = None
75 app.logger.info("Failed to create OpenURLRequest object")
77 return query_object
80def old_to_new(req):
81 """
82 Translate the OpenURL 0.1 syntax to 1.0, to provide a redirect.
83 :param req: An incoming OpenURL request
84 :return: An OpenURL 1.0 query string
85 """
87 # The meta parameters in the preamble.
88 params = {'url_ver': 'Z39.88-2004', 'url_ctx_fmt': 'info:ofi/fmt:kev:mtx:ctx', 'rft_val_fmt': 'info:ofi/fmt:kev:mtx:journal'}
90 # In OpenURL 0.1, jtitle is just title. This function substitutes them.
91 sub_title = lambda x: re.sub('^title', 'jtitle', x)
93 # Add referent tags to each parameter, and change title tag using above function
94 rewritten_params = {"rft." + sub_title(key): value for (key, value) in req.values.items()}
96 # Add the rewritten parameters to the meta params
97 params.update(rewritten_params)
99 return url_for('.openurl', **params)
102@blueprint.route("/openurl/help")
103def help():
104 return render_template(templates.OPENURL_HELP)
107@blueprint.errorhandler(404)
108def bad_request(e):
109 return render_template(templates.OPENURL_404), 404