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

71 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-20 13:43 +0000

1import json 

2import uuid 

3 

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

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 

11from portality.view.view_helper import exparam_editing_user 

12from portality.ui import templates 

13 

14blueprint = Blueprint('apply', __name__) 

15 

16@blueprint.url_value_preprocessor 

17def pull_lang(endpoint, values): 

18 # Remove 'lang' so it is not passed to the view function 

19 if values: 

20 lang = values.pop('lang', None) 

21 

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

23def application_thanks(): 

24 lang = session.get("lang", "en") 

25 

26 if lang not in ("en", "fr"): 

27 lang = "en" 

28 

29 page_frag = "/apply/fr-thank-you.html" if lang == "fr" else "/apply/thank-you.html" 

30 

31 return render_template(templates.STATIC_PAGE, page_frag=page_frag) 

32 

33 

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

35def draft_saved(): 

36 return render_template(templates.STATIC_PAGE, page_frag="/apply/draft_saved.html") 

37 

38 

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

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

41@write_required() 

42def public_application(draft_id=None): 

43 

44 if not current_user.is_authenticated: 

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

46 

47 draft_application = None 

48 if draft_id is not None: 

49 # validate that we've been given a UUID4 

50 try: 

51 uuid.UUID(draft_id, version=4) 

52 except: 

53 abort(400) 

54 

55 draft_application = models.DraftApplication.pull(draft_id) 

56 #if draft_application is None: 

57 # abort(404) 

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

59 abort(404) 

60 

61 fc = ApplicationFormFactory.context("public", extra_param=exparam_editing_user()) 

62 if request.method == "GET": 

63 if draft_application is not None: 

64 fc.processor(source=draft_application) 

65 

66 draft_data = None 

67 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 

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

69 

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

71 

72 elif request.method == "POST": 

73 

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

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

76 if draft_id is None: 

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

78 

79 if draft_id is not None: 

80 # validate that we've been given a UUID 

81 try: 

82 uuid.UUID(draft_id, version=4) 

83 except: 

84 abort(400) 

85 

86 if draft_id is not None and draft_application is None: 

87 draft_application = models.DraftApplication.pull(draft_id) 

88 #if draft_application is None: 

89 # abort(404) 

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

91 abort(404) 

92 

93 

94 

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

96 

97 if draft == "true": 

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

99 if async_def is not None: 

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

101 else: 

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

103 else: 

104 if processor.validate(): 

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

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

107 else: 

108 return fc.render_template()