Skip to content

Commit

Permalink
ajax survey post
Browse files Browse the repository at this point in the history
  • Loading branch information
AmandaBirmingham committed May 4, 2020
1 parent e40a66e commit f6cef8e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 26 deletions.
32 changes: 22 additions & 10 deletions microsetta_private_api/example/client.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,11 @@ paths:
'302':
description: Redirecting to necessary action

'/workflow_take_primary_survey':
'/workflow_take_survey':
get:
operationId: microsetta_private_api.example.client_impl.get_workflow_fill_primary_survey
operationId: microsetta_private_api.example.client_impl.get_workflow_fill_survey
parameters:
- $ref: '#/components/parameters/survey_template_id'
responses:
'200':
description: Primary Survey
Expand All @@ -169,7 +171,14 @@ paths:
type: string

post:
operationId: microsetta_private_api.example.client_impl.post_workflow_fill_primary_survey
operationId: microsetta_private_api.example.client_impl.post_workflow_fill_survey
parameters:
- $ref: '#/components/parameters/survey_template_id'
requestBody:
content:
application/json:
schema:
type: object
responses:
'200':
description: Error report
Expand Down Expand Up @@ -311,13 +320,13 @@ components:
# schema:
# $ref: '#/components/schemas/survey_id'
# required: true
# survey_template_id:
# name: survey_template_id
# in: path
# description: Unique internal id specifying a particular survey template
# schema:
# $ref: '#/components/schemas/survey_template_id'
# required: true
survey_template_id:
name: survey_template_id
in: query
description: Unique internal id specifying a particular survey template
schema:
$ref: '#/components/schemas/survey_template_id'
required: true
source_id:
name: source_id
in: path
Expand All @@ -339,3 +348,6 @@ components:
type: string
readOnly: true # sent in GET, not in POST/PUT/PATCH
example: "dae21127-27bb-4f52-9fd3-a2aa5eb5b86f"
survey_template_id:
type: integer
example: 3
28 changes: 14 additions & 14 deletions microsetta_private_api/example/client_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
ACCT_WRITEABLE_KEYS = [ACCT_FNAME_KEY, ACCT_LNAME_KEY, ACCT_EMAIL_KEY,
ACCT_ADDR_KEY]

_NEEDS_SURVEY_PREFIX = "NeedsSurvey"

# States
NEEDS_REROUTE = "NeedsReroute"
NEEDS_LOGIN = "NeedsLogin"
Expand All @@ -58,7 +60,7 @@
NEEDS_HUMAN_SOURCE = "NeedsHumanSource"
TOO_MANY_HUMAN_SOURCES = "TooManyHumanSources"
NEEDS_SAMPLE = "NeedsSample"
NEEDS_PRIMARY_SURVEY = "NeedsPrimarySurvey"
NEEDS_PRIMARY_SURVEY = _NEEDS_SURVEY_PREFIX + "1"
ALL_DONE = "AllDone"


Expand Down Expand Up @@ -224,7 +226,8 @@ def workflow():
elif next_state == NEEDS_HUMAN_SOURCE:
return redirect("/workflow_create_human_source")
elif next_state == NEEDS_PRIMARY_SURVEY:
return redirect("/workflow_take_primary_survey")
return redirect("/workflow_take_survey?survey_template_id=" +
NEEDS_PRIMARY_SURVEY.replace(_NEEDS_SURVEY_PREFIX, ""))
elif next_state == NEEDS_SAMPLE:
return redirect("/workflow_claim_kit_samples")
elif next_state == ALL_DONE:
Expand Down Expand Up @@ -392,40 +395,37 @@ def post_workflow_claim_kit_samples(body):
return redirect(WORKFLOW_URL)


def get_workflow_fill_primary_survey():
def get_workflow_fill_survey(survey_template_id):
next_state, current_state = determine_workflow_state()
if next_state != NEEDS_PRIMARY_SURVEY:
if next_state != _NEEDS_SURVEY_PREFIX + str(survey_template_id):
return redirect(WORKFLOW_URL)

acct_id = current_state["account_id"]
source_id = current_state["human_source_id"]
primary_survey = 1
do_return, survey_output = ApiRequest.get(
'/accounts/%s/sources/%s/survey_templates/%s' %
(acct_id, source_id, primary_survey))
(acct_id, source_id, survey_template_id))
if do_return:
return survey_output

return render_template("survey.jinja2",
endpoint=SERVER_CONFIG["endpoint"],
survey_template_id=survey_template_id,
survey_schema=survey_output[
'survey_template_text'])


def post_workflow_fill_primary_survey():
def post_workflow_fill_survey(survey_template_id, body):
next_state, current_state = determine_workflow_state()
if next_state == NEEDS_PRIMARY_SURVEY:
if next_state == _NEEDS_SURVEY_PREFIX + str(survey_template_id):
acct_id = current_state["account_id"]
source_id = current_state["human_source_id"]

model = {}
for x in flask.request.form:
model[x] = flask.request.form[x]

do_return, surveys_output = ApiRequest.post(
"/accounts/%s/sources/%s/surveys" % (acct_id, source_id),
json={
"survey_template_id": 1,
"survey_text": model
"survey_template_id": survey_template_id,
"survey_text": body
}
)

Expand Down
36 changes: 34 additions & 2 deletions microsetta_private_api/templates/survey.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,44 @@
<html>
<head>
<title>Microsetta Participant Survey</title>
<script src="/static/vendor/js/jquery-3.4.1.min.js"></script>
<script type="text/javascript" src="/static/vendor/js/vue-2.5.17.min.js"></script>
<link rel="stylesheet" href="/static/vendor/bootstrap-4.4.1-dist/css/bootstrap.min.css" />
<script type="text/javascript" src="/static/vendor/vue-form-generator-2.3.4/vfg.js"></script>
<link rel="stylesheet" type="text/css" href="/static/vendor/vue-form-generator-2.3.4/vfg.css">

<link rel="stylesheet" href="/static/css/minimal_interface.css" />
<script>
var result_txt = "";
function postSurvey() {
$.ajax({
type: "POST",
url: "{{ endpoint }}/workflow_take_survey?survey_template_id={{ survey_template_id }}",
data: JSON.stringify(survey_model),
success: function (data, textStatus, jqXHR) {
result_txt = data;
},
error: function (jqXHR, textStatus, errorThrown) {
result_txt = jqXHR.responseText;
},
complete: function (data, textStatus, output_obj) {
if (result_txt !== ""){
document.open();
document.write(result_txt);
document.close();
} else {
alert(textStatus);
}
},
dataType: "html",
contentType: "application/json"
});
// always return false, preventing a traditional post of the form
return false;
}
</script>
</head>
<body>
<a href="https://microsetta.ucsd.edu" title="microsetta.ucsd.edu">
Expand All @@ -28,7 +60,7 @@
<div class="container" id="app">
<div class="panel panel-default">
<div class="panel-body">
<form method="post" id="survey_form">
<form id="survey_form" onsubmit="return postAccount(this);">
<vue-form-generator :schema="schema" :model="model" :options="formOptions"></vue-form-generator>
</form>
</div>
Expand Down Expand Up @@ -60,7 +92,7 @@
type: "submit",
validateBeforeSubmit: true,
onSubmit: function(){
document.getElementById("survey_form").submit();
return postSurvey();
}
});
</script>
Expand Down

0 comments on commit f6cef8e

Please sign in to comment.