Add backend validation if the survey input is correct
This commit is contained in:
parent
1ce5b467be
commit
699b6fae52
2 changed files with 91 additions and 21 deletions
|
@ -172,18 +172,66 @@ def survey():
|
|||
|
||||
# get data from the survey and write it to the database
|
||||
|
||||
# placeholder if the survey is valid
|
||||
is_survey_valid = True
|
||||
|
||||
# Check age if it's numeric
|
||||
age = req.get('age', None)
|
||||
if not age.isnumeric():
|
||||
is_survey_valid = False
|
||||
|
||||
# check gender possibilites
|
||||
gender = req.get('gender', None)
|
||||
possible_genders = [
|
||||
"male",
|
||||
"female",
|
||||
"divers",
|
||||
"sex_na"
|
||||
]
|
||||
if gender not in possible_genders:
|
||||
is_survey_valid = False
|
||||
|
||||
# check education possibilities
|
||||
education = req.get('education', None)
|
||||
possible_education = [
|
||||
"edu_lehre",
|
||||
"edu_gymnasium",
|
||||
"edu_berufsmatura",
|
||||
"edu_bachelor",
|
||||
"edu_Master",
|
||||
"edu_na"
|
||||
]
|
||||
if education not in possible_education:
|
||||
is_survey_valid = False
|
||||
|
||||
# check skills values
|
||||
skills = req.get('skills', None)
|
||||
user_db = User.query.filter_by(id=id_req).first()
|
||||
user_db.age = age
|
||||
user_db.gender = gender
|
||||
user_db.education = education
|
||||
user_db.skills = skills
|
||||
db.session.commit()
|
||||
ret = {'message': 'Umfrage freigestellt'}
|
||||
return ret, 200
|
||||
possible_skills = [
|
||||
"skills_sehr_gut",
|
||||
"skills_gut",
|
||||
"skills_mittel",
|
||||
"skills_nicht_so_good",
|
||||
"skills_garnicht",
|
||||
"skills_na"
|
||||
]
|
||||
|
||||
if skills not in possible_skills:
|
||||
is_survey_valid = False
|
||||
|
||||
if is_survey_valid:
|
||||
user_db = User.query.filter_by(id=id_req).first()
|
||||
user_db.age = age
|
||||
user_db.gender = gender
|
||||
user_db.education = education
|
||||
user_db.skills = skills
|
||||
db.session.commit()
|
||||
print(req)
|
||||
ret = {'message': 'Vielen Dank für das Ausfüllen der Umfrage.'}
|
||||
return ret, 200
|
||||
else:
|
||||
ret = {
|
||||
'message': 'Einige der Felder stimmen nicht überein und müssen angepasst werden.'}
|
||||
return ret, 400
|
||||
|
||||
|
||||
@app.route('/time')
|
||||
|
|
|
@ -21,6 +21,10 @@ export default function Umfrage() {
|
|||
const [educationErrorMessage, setEducationErrorMessage] = useState("");
|
||||
const [skillsErrorMessage, setSkillsErrorMessage] = useState("");
|
||||
|
||||
const [backendErrorMessage, setBackendErrorMessage] = useState("");
|
||||
const [backendSuccessMessage, setBackendSuccessMessage] = useState("");
|
||||
const [isSurveyValidBackend, setIsSurveyValidBackend] = useState("");
|
||||
|
||||
/* boolean if the values are ok */
|
||||
const [isAgeOk, setIsAgeOk] = useState("");
|
||||
const [isGenderOk, setIsGenderOk] = useState("");
|
||||
|
@ -71,10 +75,23 @@ export default function Umfrage() {
|
|||
setIsSkillsOk(false);
|
||||
}
|
||||
|
||||
setIsSurveyValid(true);
|
||||
if (isSurveyValid === true) {
|
||||
authFetch("/api/protected/umfrage", {
|
||||
method: "post",
|
||||
body: JSON.stringify(opts),
|
||||
}).then((response) => {
|
||||
if (response.status === 200) {
|
||||
response.json().then((r) => {
|
||||
setBackendSuccessMessage(r.message);
|
||||
setIsSurveyValidBackend(true);
|
||||
});
|
||||
} else {
|
||||
response.json().then((r) => {
|
||||
setBackendErrorMessage(r.message);
|
||||
setIsSurveyValidBackend(false);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
@ -135,7 +152,7 @@ export default function Umfrage() {
|
|||
</option>
|
||||
<option value="male">Männlich</option>
|
||||
<option value="female">Weiblich</option>
|
||||
<option value="divers">divers</option>
|
||||
<option value="divers">Divers</option>
|
||||
<option value="sex_na">Keine Angabe</option>
|
||||
</select>
|
||||
</div>
|
||||
|
@ -154,12 +171,12 @@ export default function Umfrage() {
|
|||
<option disabled value="DEFAULT">
|
||||
-- Bitte wähle deine Ausbildung aus --
|
||||
</option>
|
||||
<option>Berufslehre</option>
|
||||
<option>Gymnasiale Maturität</option>
|
||||
<option>Berufsmatura</option>
|
||||
<option>Bachelor</option>
|
||||
<option>Master oder höher</option>
|
||||
<option>Keine Angabe</option>
|
||||
<option value="edu_lehre">Berufslehre</option>
|
||||
<option value="edu_gymnasium">Gymnasiale Maturität</option>
|
||||
<option value="edu_berufsmatura">Berufsmatura</option>
|
||||
<option value="edu_bachelor">Bachelor</option>
|
||||
<option value="edu_Master">Master oder höher</option>
|
||||
<option value="edu_na">Keine Angabe</option>
|
||||
</select>
|
||||
</div>
|
||||
{!isEducationOk && <ErrorMessage message={educationErrorMessage} />}
|
||||
|
@ -178,12 +195,12 @@ export default function Umfrage() {
|
|||
<option disabled value="DEFAULT">
|
||||
-- Bitte wähle etwas aus --
|
||||
</option>
|
||||
<option>Sehr gut</option>
|
||||
<option>Gut</option>
|
||||
<option>Mittel</option>
|
||||
<option>Eher nicht so gut</option>
|
||||
<option>Gar nicht gut</option>
|
||||
<option>Keine Angabe</option>
|
||||
<option value="skills_sehr_gut">Sehr gut</option>
|
||||
<option value="skills_gut">Gut</option>
|
||||
<option value="skills_mittel">Mittel</option>
|
||||
<option value="skills_nicht_so_good">Eher nicht so gut</option>
|
||||
<option value="skills_garnicht">Gar nicht gut</option>
|
||||
<option value="skills_na">Keine Angabe</option>
|
||||
</select>
|
||||
</div>
|
||||
{!isSkillsOk && <ErrorMessage message={skillsErrorMessage} />}
|
||||
|
@ -194,6 +211,11 @@ export default function Umfrage() {
|
|||
onClick={onSubmitClick}
|
||||
/>
|
||||
</form>
|
||||
{!isSurveyValidBackend ? (
|
||||
<ErrorMessage message={backendErrorMessage} />
|
||||
) : (
|
||||
<p>{backendSuccessMessage}</p>
|
||||
)}
|
||||
</div>
|
||||
<Footer />
|
||||
</>
|
||||
|
|
Loading…
Reference in a new issue