Clean fix of answer id validation
This commit is contained in:
parent
fb0ac080e5
commit
5c85e8cc47
@ -44,7 +44,6 @@ class SolutionForm(Form):
|
|||||||
|
|
||||||
def create_used_id_list():
|
def create_used_id_list():
|
||||||
data = list()
|
data = list()
|
||||||
|
|
||||||
if not os.path.exists(datafile):
|
if not os.path.exists(datafile):
|
||||||
open(datafile, "a").close()
|
open(datafile, "a").close()
|
||||||
|
|
||||||
@ -54,16 +53,14 @@ def create_used_id_list():
|
|||||||
data.append(line[0])
|
data.append(line[0])
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def check_id(user_id):
|
def check_id(form, field):
|
||||||
used_ids = create_used_id_list()
|
used_ids = create_used_id_list()
|
||||||
if user_id in used_ids:
|
if field.data not in used_ids:
|
||||||
return True
|
raise validators.ValidationError('Leider keine gültige ID')
|
||||||
else:
|
|
||||||
return False
|
|
||||||
|
|
||||||
class AnswerForm(Form):
|
class AnswerForm(Form):
|
||||||
used_id_list = create_used_id_list()
|
used_id_list = create_used_id_list()
|
||||||
user_id = StringField('Deine Id', [validators.InputRequired(), validators.AnyOf(values=used_id_list, message='Leider ist das keine gültige ID')])
|
user_id = StringField('Deine Id', [validators.InputRequired(), check_id])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -135,14 +132,10 @@ def admin_solution():
|
|||||||
|
|
||||||
@app.route('/answers', methods=['GET', 'POST'])
|
@app.route('/answers', methods=['GET', 'POST'])
|
||||||
def answers():
|
def answers():
|
||||||
file_data = create_used_id_list()
|
|
||||||
print(file_data)
|
|
||||||
form = AnswerForm(request.form)
|
form = AnswerForm(request.form)
|
||||||
if request.method == 'POST':
|
if request.method == 'POST' and form.validate():
|
||||||
print(request.form['user_id'])
|
ident = request.form['user_id']
|
||||||
if request.form['user_id'] in file_data:
|
return redirect(url_for('view_answers', user_id=ident))
|
||||||
ident = request.form['user_id']
|
|
||||||
return redirect(url_for('view_answers', user_id=ident))
|
|
||||||
return render_template('answers.html', form=form, title='Sortierhut', text='Hallo, Admin Fooboar')
|
return render_template('answers.html', form=form, title='Sortierhut', text='Hallo, Admin Fooboar')
|
||||||
|
|
||||||
@app.route('/view_answers<user_id>')
|
@app.route('/view_answers<user_id>')
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user