From 39b97cb97f6e0441c060346bfa265701e08ae8f6 Mon Sep 17 00:00:00 2001 From: asauer Date: Fri, 12 Apr 2019 19:25:57 +0200 Subject: [PATCH] Fill the form with content --- gruppenbildungsspiel.py | 22 +++++++++++++++++----- templates/_formhelpers1.html | 13 ------------- templates/questions.html | 7 +++++-- 3 files changed, 22 insertions(+), 20 deletions(-) delete mode 100644 templates/_formhelpers1.html diff --git a/gruppenbildungsspiel.py b/gruppenbildungsspiel.py index 051b6a8..013994f 100644 --- a/gruppenbildungsspiel.py +++ b/gruppenbildungsspiel.py @@ -2,7 +2,8 @@ from flask import Flask, render_template, request, redirect, url_for import uuid import csv import os -from wtforms import Form, StringField, SelectField, BooleanField, validators +from wtforms import Form, StringField, SelectField, BooleanField, validators +from wtforms.fields.html5 import EmailField app = Flask(__name__) app.jinja_env.trim_blocks = True @@ -10,11 +11,23 @@ app.jinja_env.lstrip_blocks = True datafile = "data.csv" +def input_check(form, field): + if field.data == 'default': + raise validators.ValidationError('Bitte wähle eine Option aus') + 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') + computers = [('default', 'Bitte auswählen'), ('hal', 'HAL 9000 (2001: Odyssee im Weltraum)'), ('deepthought', 'Deep Thought (Per Anhalter durch die Galaxie)'), ('neuromancer', 'Neuromancer (Neuromancer)'), ('samantha', 'Samatha (Her)'), ('tars', 'TARS und CASE (Interstellar)')] + computer = SelectField(u'Fiktionaler Lieblingscomputer', choices=computers, validators=[validators.InputRequired(), input_check]) + statuses = [('default', 'Bitte auswählen'), ('200', '200 OK'), ('300', '300 Multiple Choices'), ('301', '301 Moved Permanently'), ('402', '402 Payment Required'), ('404', '404 (Sleep) Not Found'), ('408', '408 Request Timeout'), ('418', '418 I am a teapot'), ('450', '450 Blocked by Windows Parental Controls'), ('451', '451 Unavailable For Legal Reasons'), ('502', '502 Bad Gateway (Internetkurort)')] + status = SelectField(u'Welcher HTTP-Statuscode trifft am häufigsten auf dich zu?', choices=statuses, validators=[validators.InputRequired(), input_check]) + vegetables = [('default', 'Bitte auswählen'), ('karotte', '🥕'), ('broccoli','🥦'), ('aubergine', '🍆'), ('kartoffel', '🥔'), ('bretzel', '🥨'), ('tomate', '🍅'), ('chili', '🌶️')] + vegetable = SelectField(u'Lieblingsgemüse', choices=vegetables, validators=[validators.InputRequired(), input_check]) + spirit_animals = [('default', 'Bitte auswählen'), ('feuer', 'Feuerfuchs'), ('wasser', 'Wasserhahn'), ('erde', 'Erdferkel'), ('luft', 'Luftschlange')] + spirit_animal = SelectField(u'Spirit Animal', choices=spirit_animals, validators=[validators.InputRequired(), input_check]) + operating_systems = [('default', 'Bitte auswählen'), ('windows', 'Windows'), ('mac', 'Mac'), ('linux', 'Linux'), ('kaffee', 'Kaffee'), ('glados', 'GLaDOS')] + operating_system = SelectField(u'Bevorzugtes Betriebssystem', choices=operating_systems, validators=[validators.InputRequired(), input_check]) check = BooleanField('Bitte nicht ankreuzen') + email = EmailField('Email-Adresse') class User(): name = '' @@ -26,7 +39,6 @@ def index(): @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(): with open(datafile, "a", newline='') as f: new_row = list() diff --git a/templates/_formhelpers1.html b/templates/_formhelpers1.html deleted file mode 100644 index 6bbe7fa..0000000 --- a/templates/_formhelpers1.html +++ /dev/null @@ -1,13 +0,0 @@ -{% macro render_field(field) %} -{% for field in form %} - - {% if field.type == "BooleanField" %} - - {{ field }} {{ field.label }} - {% else %} - {{ field.label }} - {{ field }} - {% endif %} - -{% endfor %} -{% endmacro %} diff --git a/templates/questions.html b/templates/questions.html index 0c63469..634a6f5 100644 --- a/templates/questions.html +++ b/templates/questions.html @@ -3,10 +3,13 @@ {% from "_formhelpers.html" import render_field %}
- {{ render_field(form.username) }} - {{ render_field(form.spirit_animal) }} + {{ render_field(form.computer) }} + {{ render_field(form.status) }} {{ render_field(form.vegetable) }} + {{ render_field(form.spirit_animal) }} + {{ render_field(form.operating_system) }} {{ form.check }} {{ form.check.label }} + {{ render_field(form.email) }}