Coverage for portality/view/apply.py: 26%

62 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-07-22 15:59 +0100

1import json 

2import uuid 

3 

4from flask import Blueprint, render_template, abort, redirect, url_for, request, make_response 

5from flask_login import current_user 

6 

7from portality import models 

8from portality.decorators import write_required 

9 

10from portality.forms.application_forms import ApplicationFormFactory 

11 

12blueprint = Blueprint('apply', __name__) 

13 

14 

15@blueprint.route("/thank-you", methods=["GET"]) 

16def application_thanks(): 

17 return render_template("layouts/static_page.html", page_frag="/apply/thank-you.html") 

18 

19 

20@blueprint.route("/draft", methods=["GET"]) 

21def draft_saved(): 

22 return render_template("layouts/static_page.html", page_frag="/apply/draft_saved.html") 

23 

24 

25@blueprint.route("/", methods=["GET", "POST"]) 

26@blueprint.route("/<draft_id>", methods=["GET", "POST"]) 

27@write_required() 

28def public_application(draft_id=None): 

29 

30 if not current_user.is_authenticated: 

31 return redirect(url_for("account.login", redirected="apply")) 

32 

33 draft_application = None 

34 if draft_id is not None: 

35 # validate that we've been given a UUID4 

36 try: 

37 uuid.UUID(draft_id, version=4) 

38 except: 

39 abort(400) 

40 

41 draft_application = models.DraftApplication.pull(draft_id) 

42 #if draft_application is None: 

43 # abort(404) 

44 if draft_application is not None and draft_application.owner != current_user.id: 

45 abort(404) 

46 

47 if request.method == "GET": 

48 fc = ApplicationFormFactory.context("public") 

49 if draft_application is not None: 

50 fc.processor(source=draft_application) 

51 

52 draft_data = None 

53 if draft_application is None: # we always set a draft id, which means that whenver the browser reloads this page from cache, the id is stable and no duplicates are created 

54 draft_data = {"id" : models.DraftApplication.makeid()} 

55 

56 return fc.render_template(obj=draft_application, draft_data=draft_data) 

57 

58 elif request.method == "POST": 

59 

60 fc = ApplicationFormFactory.context("public") 

61 

62 draft = request.form.get("draft") 

63 async_def = request.form.get("async") 

64 if draft_id is None: 

65 draft_id = request.form.get("id") 

66 

67 if draft_id is not None: 

68 # validate that we've been given a UUID 

69 try: 

70 uuid.UUID(draft_id, version=4) 

71 except: 

72 abort(400) 

73 

74 if draft_id is not None and draft_application is None: 

75 draft_application = models.DraftApplication.pull(draft_id) 

76 #if draft_application is None: 

77 # abort(404) 

78 if draft_application is not None and draft_application.owner != current_user.id: 

79 abort(404) 

80 

81 

82 

83 processor = fc.processor(formdata=request.form) 

84 

85 if draft == "true": 

86 the_draft = processor.draft(current_user._get_current_object(), id=draft_id) 

87 if async_def is not None: 

88 return make_response(json.dumps({"id": the_draft.id}), 200) 

89 else: 

90 return redirect(url_for('apply.draft_saved')) 

91 else: 

92 if processor.validate(): 

93 processor.finalise(current_user._get_current_object(), id=draft_id) 

94 return redirect(url_for('apply.application_thanks', _anchor='thanks')) 

95 else: 

96 return fc.render_template()