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
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-20 13:43 +0000
1import json
2import uuid
4from flask import Blueprint, render_template, abort, redirect, url_for, request, make_response, session
5from flask_login import current_user
7from portality import models
8from portality.decorators import write_required
10from portality.forms.application_forms import ApplicationFormFactory
11from portality.view.view_helper import exparam_editing_user
12from portality.ui import templates
14blueprint = Blueprint('apply', __name__)
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)
22@blueprint.route("/thank-you", methods=["GET"])
23def application_thanks():
24 lang = session.get("lang", "en")
26 if lang not in ("en", "fr"):
27 lang = "en"
29 page_frag = "/apply/fr-thank-you.html" if lang == "fr" else "/apply/thank-you.html"
31 return render_template(templates.STATIC_PAGE, page_frag=page_frag)
34@blueprint.route("/draft", methods=["GET"])
35def draft_saved():
36 return render_template(templates.STATIC_PAGE, page_frag="/apply/draft_saved.html")
39@blueprint.route("/", methods=["GET", "POST"])
40@blueprint.route("/<draft_id>", methods=["GET", "POST"])
41@write_required()
42def public_application(draft_id=None):
44 if not current_user.is_authenticated:
45 return redirect(url_for("account.login", redirected="apply"))
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)
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)
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)
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()}
70 return fc.render_template(obj=draft_application, draft_data=draft_data)
72 elif request.method == "POST":
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")
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)
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)
95 processor = fc.processor(formdata=request.form)
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()