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

67 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-05-05 00:09 +0100

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 return render_template(templates.STATIC_PAGE, page_frag="/apply/thank-you.html") 

25 

26 

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

28def draft_saved(): 

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

30 

31 

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

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

34@write_required() 

35def public_application(draft_id=None): 

36 

37 if not current_user.is_authenticated: 

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

39 

40 draft_application = None 

41 if draft_id is not None: 

42 # validate that we've been given a UUID4 

43 try: 

44 uuid.UUID(draft_id, version=4) 

45 except: 

46 abort(400) 

47 

48 draft_application = models.DraftApplication.pull(draft_id) 

49 #if draft_application is None: 

50 # abort(404) 

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

52 abort(404) 

53 

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

55 if request.method == "GET": 

56 if draft_application is not None: 

57 fc.processor(source=draft_application) 

58 

59 draft_data = None 

60 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 

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

62 

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

64 

65 elif request.method == "POST": 

66 

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

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

69 if draft_id is None: 

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

71 

72 if draft_id is not None: 

73 # validate that we've been given a UUID 

74 try: 

75 uuid.UUID(draft_id, version=4) 

76 except: 

77 abort(400) 

78 

79 if draft_id is not None and draft_application is None: 

80 draft_application = models.DraftApplication.pull(draft_id) 

81 #if draft_application is None: 

82 # abort(404) 

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

84 abort(404) 

85 

86 

87 

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

89 

90 if draft == "true": 

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

92 if async_def is not None: 

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

94 else: 

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

96 else: 

97 if processor.validate(): 

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

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

100 else: 

101 return fc.render_template()