Coverage for portality/view/doajservices.py: 37%

65 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-09-21 00:49 +0100

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

2 

3from flask import Blueprint, make_response, request, abort, render_template 

4from flask_login import current_user, login_required 

5 

6from portality.core import app 

7from portality.decorators import ssl_required, write_required, restrict_to_role 

8from portality.util import jsonp 

9from portality import lock 

10from portality.bll import DOAJ 

11 

12blueprint = Blueprint('doajservices', __name__) 

13 

14 

15@blueprint.route("/unlock/<object_type>/<object_id>", methods=["POST"]) 

16@login_required 

17@ssl_required 

18@write_required() 

19def unlock(object_type, object_id): 

20 # change from suggestion to application after redesign, that needs refactoring to make the code consistent 

21 # that if is only for quick hotfix to make sure the functionality works 

22 if object_type == "application": 

23 object_type = "suggestion" 

24 

25 # first figure out if we are allowed to even contemplate this action 

26 if object_type not in ["journal", "suggestion"]: 

27 abort(404) 

28 if object_type == "journal": 

29 if not current_user.has_role("edit_journal"): 

30 abort(401) 

31 if object_type == "suggestion": 

32 if not current_user.has_role("edit_suggestion"): 

33 abort(401) 

34 

35 # try to unlock 

36 unlocked = lock.unlock(object_type, object_id, current_user.id) 

37 

38 # if we couldn't unlock, this is a bad request 

39 if not unlocked: 

40 abort(400) 

41 

42 # otherwise, return success 

43 resp = make_response(json.dumps({"result" : "success"})) 

44 resp.mimetype = "application/json" 

45 return resp 

46 

47 

48@blueprint.route("/unlocked") 

49@login_required 

50def unlocked(): 

51 """ 

52 Redirect to this route on completion of an unlock 

53 :return: 

54 """ 

55 return render_template("unlocked.html") 

56 

57 

58@blueprint.route("/shorten", methods=["POST"]) 

59@jsonp 

60def shorten(): 

61 # Enable this if you are testing and you want to see the front end work, without working bit.ly credentials 

62 # return make_response(json.dumps({"url" : "testing url"})) 

63 try: 

64 # parse the json 

65 d = json.loads(request.data) 

66 p = d['page'] 

67 q = d['query'] 

68 

69 # re-serialise the query, and url encode it 

70 source = urllib.parse.quote(json.dumps(q)) 

71 

72 # assemble the DOAJ url 

73 doajurl = p + "?source=" + source 

74 

75 # assemble the bitly url. Note that we re-encode the doajurl to include in the 

76 # query arguments, so by this point it is double-encoded 

77 bitly = app.config.get("BITLY_SHORTENING_API_URL") 

78 bitly_oauth = app.config.get("BITLY_OAUTH_TOKEN") 

79 

80 # Set an Auth Bearer token (Bitly 4.0) 

81 headers = {'Authorization': 'Bearer ' + bitly_oauth} 

82 

83 # Add the long url as a payload 

84 payload = {'long_url': doajurl} 

85 

86 # make the request 

87 resp = requests.post(bitly, headers=headers, data=json.dumps(payload)) 

88 shorturl = resp.json().get('link') 

89 

90 if not shorturl: 

91 abort(400) 

92 

93 # make the response 

94 answer = make_response(json.dumps({"url": shorturl})) 

95 answer.mimetype = "application/json" 

96 return answer 

97 except: 

98 abort(400) 

99 

100 

101@blueprint.route("/groupstatus/<group_id>", methods=["GET"]) 

102@jsonp 

103@login_required 

104def group_status(group_id): 

105 """ 

106 ~~GroupStatus:Feature -> Todo:Service~~ 

107 :param group_id: 

108 :return: 

109 """ 

110 if not current_user.has_role("admin"): 

111 abort(404) 

112 svc = DOAJ.todoService() 

113 stats = svc.group_stats(group_id) 

114 return make_response(json.dumps(stats))