Coverage for portality/view/query.py: 75%

36 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-07-19 18:38 +0100

1import json, urllib.request, urllib.error, urllib.parse 

2 

3from flask import Blueprint, request, abort, make_response 

4from flask_login import current_user 

5 

6from portality import util 

7from portality.bll.doaj import DOAJ 

8from portality.bll import exceptions 

9 

10blueprint = Blueprint('query', __name__) 

11 

12# pass queries direct to index. POST only for receipt of complex query objects 

13@blueprint.route('/<path:path>', methods=['GET','POST']) 

14@util.jsonp 

15def query(path=None): 

16 """ 

17 Query endpoint for general queries via the web interface. Calls on the DOAJ.queryService for action 

18 

19 :param path: 

20 :return: 

21 """ 

22 pathparts = request.path.strip('/').split('/') 

23 if len(pathparts) < 2: 

24 abort(400) 

25 domain = pathparts[0] 

26 index_type = pathparts[1] 

27 

28 q = None 

29 # if this is a POST, read the contents out of the body 

30 if request.method == "POST": 

31 q = request.json 

32 # if there is a source param, load the json from it 

33 elif 'source' in request.values: 

34 try: 

35 q = json.loads(urllib.parse.unquote(request.values['source'])) 

36 except ValueError: 

37 abort(400) 

38 

39 try: 

40 account = None 

41 if current_user is not None and not current_user.is_anonymous: 

42 account = current_user._get_current_object() 

43 queryService = DOAJ.queryService() 

44 res = queryService.search(domain, index_type, q, account, request.values) 

45 except exceptions.AuthoriseException as e: 

46 abort(403) 

47 except exceptions.NoSuchObjectException as e: 

48 abort(404) 

49 

50 resp = make_response(json.dumps(res)) 

51 resp.mimetype = "application/json" 

52 return resp