79 lines
2.3 KiB
Python
79 lines
2.3 KiB
Python
from flask import Flask, render_template, request, redirect, url_for
|
|
import uuid
|
|
import csv
|
|
import os
|
|
from wtforms import Form, StringField, SelectField, BooleanField, validators
|
|
|
|
app = Flask(__name__)
|
|
app.jinja_env.trim_blocks = True
|
|
app.jinja_env.lstrip_blocks = True
|
|
|
|
datafile = "data.csv"
|
|
|
|
class QuestionForm(Form):
|
|
username = StringField('Name', [validators.Length(min=4, max=25)])
|
|
spirit_animal = SelectField(u'Spirit Animal', choices=[('feuer', 'Feuerfuchs'), ('wasser', 'Wasserhahn'), ('erde', 'Erdferkel'), ('luft', 'Luftschlange')])
|
|
vegetable = SelectField(u'Gemüse')
|
|
check = BooleanField('Bitte hier ankreuzen')
|
|
|
|
class User():
|
|
name = ''
|
|
|
|
@app.route('/')
|
|
def index():
|
|
return render_template('index.html', title='Test', text='Hallo, Fooboar')
|
|
|
|
@app.route('/questions', methods=['GET', 'POST'])
|
|
def questions():
|
|
form = QuestionForm(request.form)
|
|
form.vegetable.choices = [('tomate', 'Tomate'), ('erdnuss', 'Erdnuss')]
|
|
if request.method == 'POST' and form.validate():
|
|
user = User()
|
|
user.name = form.username
|
|
spirit = request.form['spirit_animal']
|
|
print(spirit)
|
|
# user.save()
|
|
return redirect(url_for('result'))
|
|
return render_template('questions.html', form=form)
|
|
|
|
@app.route('/form')
|
|
def form():
|
|
id = generate_id()
|
|
return render_template('form.html', title='Test', text='Hallo, Fooboar', your_id=id)
|
|
|
|
@app.route('/result')
|
|
def result():
|
|
return render_template('result.html', hausname='Knuth', haustext='Mitglieder dieses Hauses mögen LaTeX-Anzüge und Eisbären.', id=123)
|
|
|
|
@app.route('/admin')
|
|
def admin():
|
|
return render_template('admin.html', title='Test', text='Hallo, Admin Fooboar')
|
|
|
|
@app.route('/answers')
|
|
def answers():
|
|
return render_template('answers.html', title='Test', text='Hallo, Admin Fooboar')
|
|
|
|
@app.route('/solution')
|
|
def solution():
|
|
return render_template('solution.html', title='Test', text='Hallo, Lösung')
|
|
|
|
def generate_id():
|
|
data = list()
|
|
|
|
if not os.path.exists(datafile):
|
|
open(datafile, "a").close()
|
|
|
|
with open(datafile, "r") as f:
|
|
reader = csv.reader(f)
|
|
for line in reader:
|
|
data.append(line[0])
|
|
|
|
# best loop ever
|
|
while True:
|
|
new_id = str(uuid.uuid1()).split("-")[0]
|
|
|
|
if new_id in data:
|
|
break
|
|
else:
|
|
return new_id
|