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