-
Notifications
You must be signed in to change notification settings - Fork 16
/
apply.py
95 lines (71 loc) · 3.32 KB
/
apply.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import json
import uuid
from flask import Blueprint, render_template, abort, redirect, url_for, request, make_response
from flask_login import current_user
from portality import models
from portality.decorators import write_required
from portality.forms.application_forms import ApplicationFormFactory
from portality.view.view_helper import exparam_editing_user
blueprint = Blueprint('apply', __name__)
@blueprint.route("/thank-you", methods=["GET"])
def application_thanks():
return render_template("layouts/static_page.html", page_frag="/apply/thank-you.html")
@blueprint.route("/draft", methods=["GET"])
def draft_saved():
return render_template("layouts/static_page.html", page_frag="/apply/draft_saved.html")
@blueprint.route("/", methods=["GET", "POST"])
@blueprint.route("/<draft_id>", methods=["GET", "POST"])
@write_required()
def public_application(draft_id=None):
if not current_user.is_authenticated:
return redirect(url_for("account.login", redirected="apply"))
draft_application = None
if draft_id is not None:
# validate that we've been given a UUID4
try:
uuid.UUID(draft_id, version=4)
except:
abort(400)
draft_application = models.DraftApplication.pull(draft_id)
#if draft_application is None:
# abort(404)
if draft_application is not None and draft_application.owner != current_user.id:
abort(404)
fc = ApplicationFormFactory.context("public", extra_param=exparam_editing_user())
if request.method == "GET":
if draft_application is not None:
fc.processor(source=draft_application)
draft_data = None
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
draft_data = {"id" : models.DraftApplication.makeid()}
return fc.render_template(obj=draft_application, draft_data=draft_data)
elif request.method == "POST":
draft = request.form.get("draft")
async_def = request.form.get("async")
if draft_id is None:
draft_id = request.form.get("id")
if draft_id is not None:
# validate that we've been given a UUID
try:
uuid.UUID(draft_id, version=4)
except:
abort(400)
if draft_id is not None and draft_application is None:
draft_application = models.DraftApplication.pull(draft_id)
#if draft_application is None:
# abort(404)
if draft_application is not None and draft_application.owner != current_user.id:
abort(404)
processor = fc.processor(formdata=request.form)
if draft == "true":
the_draft = processor.draft(current_user._get_current_object(), id=draft_id)
if async_def is not None:
return make_response(json.dumps({"id": the_draft.id}), 200)
else:
return redirect(url_for('apply.draft_saved'))
else:
if processor.validate():
processor.finalise(current_user._get_current_object(), id=draft_id)
return redirect(url_for('apply.application_thanks', _anchor='thanks'))
else:
return fc.render_template()