Add backend validation if the survey input is correct

This commit is contained in:
cami 2021-07-05 17:51:49 +02:00
parent 1ce5b467be
commit 699b6fae52
2 changed files with 91 additions and 21 deletions

View file

@ -172,18 +172,66 @@ def survey():
# get data from the survey and write it to the database # 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) age = req.get('age', None)
if not age.isnumeric():
is_survey_valid = False
# check gender possibilites
gender = req.get('gender', None) 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) 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) skills = req.get('skills', None)
user_db = User.query.filter_by(id=id_req).first() possible_skills = [
user_db.age = age "skills_sehr_gut",
user_db.gender = gender "skills_gut",
user_db.education = education "skills_mittel",
user_db.skills = skills "skills_nicht_so_good",
db.session.commit() "skills_garnicht",
ret = {'message': 'Umfrage freigestellt'} "skills_na"
return ret, 200 ]
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') @app.route('/time')

View file

@ -21,6 +21,10 @@ export default function Umfrage() {
const [educationErrorMessage, setEducationErrorMessage] = useState(""); const [educationErrorMessage, setEducationErrorMessage] = useState("");
const [skillsErrorMessage, setSkillsErrorMessage] = useState(""); const [skillsErrorMessage, setSkillsErrorMessage] = useState("");
const [backendErrorMessage, setBackendErrorMessage] = useState("");
const [backendSuccessMessage, setBackendSuccessMessage] = useState("");
const [isSurveyValidBackend, setIsSurveyValidBackend] = useState("");
/* boolean if the values are ok */ /* boolean if the values are ok */
const [isAgeOk, setIsAgeOk] = useState(""); const [isAgeOk, setIsAgeOk] = useState("");
const [isGenderOk, setIsGenderOk] = useState(""); const [isGenderOk, setIsGenderOk] = useState("");
@ -71,10 +75,23 @@ export default function Umfrage() {
setIsSkillsOk(false); setIsSkillsOk(false);
} }
setIsSurveyValid(true);
if (isSurveyValid === true) { if (isSurveyValid === true) {
authFetch("/api/protected/umfrage", { authFetch("/api/protected/umfrage", {
method: "post", method: "post",
body: JSON.stringify(opts), 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>
<option value="male">Männlich</option> <option value="male">Männlich</option>
<option value="female">Weiblich</option> <option value="female">Weiblich</option>
<option value="divers">divers</option> <option value="divers">Divers</option>
<option value="sex_na">Keine Angabe</option> <option value="sex_na">Keine Angabe</option>
</select> </select>
</div> </div>
@ -154,12 +171,12 @@ export default function Umfrage() {
<option disabled value="DEFAULT"> <option disabled value="DEFAULT">
-- Bitte wähle deine Ausbildung aus -- -- Bitte wähle deine Ausbildung aus --
</option> </option>
<option>Berufslehre</option> <option value="edu_lehre">Berufslehre</option>
<option>Gymnasiale Maturität</option> <option value="edu_gymnasium">Gymnasiale Maturität</option>
<option>Berufsmatura</option> <option value="edu_berufsmatura">Berufsmatura</option>
<option>Bachelor</option> <option value="edu_bachelor">Bachelor</option>
<option>Master oder höher</option> <option value="edu_Master">Master oder höher</option>
<option>Keine Angabe</option> <option value="edu_na">Keine Angabe</option>
</select> </select>
</div> </div>
{!isEducationOk && <ErrorMessage message={educationErrorMessage} />} {!isEducationOk && <ErrorMessage message={educationErrorMessage} />}
@ -178,12 +195,12 @@ export default function Umfrage() {
<option disabled value="DEFAULT"> <option disabled value="DEFAULT">
-- Bitte wähle etwas aus -- -- Bitte wähle etwas aus --
</option> </option>
<option>Sehr gut</option> <option value="skills_sehr_gut">Sehr gut</option>
<option>Gut</option> <option value="skills_gut">Gut</option>
<option>Mittel</option> <option value="skills_mittel">Mittel</option>
<option>Eher nicht so gut</option> <option value="skills_nicht_so_good">Eher nicht so gut</option>
<option>Gar nicht gut</option> <option value="skills_garnicht">Gar nicht gut</option>
<option>Keine Angabe</option> <option value="skills_na">Keine Angabe</option>
</select> </select>
</div> </div>
{!isSkillsOk && <ErrorMessage message={skillsErrorMessage} />} {!isSkillsOk && <ErrorMessage message={skillsErrorMessage} />}
@ -194,6 +211,11 @@ export default function Umfrage() {
onClick={onSubmitClick} onClick={onSubmitClick}
/> />
</form> </form>
{!isSurveyValidBackend ? (
<ErrorMessage message={backendErrorMessage} />
) : (
<p>{backendSuccessMessage}</p>
)}
</div> </div>
<Footer /> <Footer />
</> </>