Get back-and-forth of form validation working

This commit is contained in:
Luke Murphy 2020-07-02 13:04:10 +02:00
parent 5dffc7db72
commit 44b0658a31
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
3 changed files with 91 additions and 11 deletions

View File

@ -14,7 +14,7 @@ from flask import Flask, render_template, request
from flask_wtf import FlaskForm
from ruamel.yaml import YAML
from wtforms import PasswordField, StringField
from wtforms.validators import DataRequired
from wtforms.validators import URL, DataRequired, Length
app = Flask(__name__)
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
@ -71,17 +71,81 @@ class GiteaInstallForm(FlaskForm):
"""Gitea installation form."""
app_name = StringField("Application name", default="Git with a cup of tea")
domain = StringField("Domain name", validators=[DataRequired()])
domain = StringField(
# TODO(decentral1se): missing domain name validator
"Domain name",
validators=[DataRequired("Please enter a domain name")],
)
db_host = StringField("Database host", default="mariadb:3306")
db_name = StringField("Database name", default="gitea")
db_passwd = PasswordField("DB password", default=get_secret(n=32))
db_root_passwd = PasswordField("Root DB password", default=get_secret(n=32))
db_passwd = PasswordField(
"Database password",
validators=[
DataRequired(),
Length(
min=32,
message=(
"Your chosen password length is too short, "
"must be at least 32 characters long"
),
),
],
)
db_root_passwd = PasswordField(
"Database root password",
validators=[
DataRequired(),
Length(
min=32,
message=(
"Your chosen password length is too short, "
"it must be at least 32 characters long"
),
),
],
)
db_type = StringField("Database type", default="mysql")
db_user = StringField("Database user", default="mysql")
internal_token = PasswordField("Internal token", default=get_secret(n=105))
jwt_secret = PasswordField("JWT secret", default=get_secret(n=43))
secret_key = PasswordField("Secret key", default=get_secret(n=64))
internal_token = PasswordField(
"Internal secret token",
validators=[
DataRequired(),
Length(
min=105,
message=(
"Your chosen token length is too short, "
"it must be at least 105 characters long"
),
),
],
)
jwt_secret = PasswordField(
"JWT secret",
validators=[
DataRequired(),
Length(
min=43,
message=(
"Your chosen password length is too short, "
"it must be at least 32 characters long"
),
),
],
)
secret_key = PasswordField(
"Secret key",
validators=[
DataRequired(),
Length(
min=64,
message=(
"Your chosen password length is too short, "
"it must be at least 64 characters long"
),
),
],
)
ssh_port = StringField("SSH port", default="2222")
@ -106,7 +170,7 @@ def deploy(app_name):
form = GiteaInstallForm(request.form)
if not form.validate():
return render_template("install.html", app_name=app_name, form=form)
return render_template("second/install.html", app_name=app_name, form=form)
environment = get_loaded_env(app_name, request.form)

View File

@ -1,3 +1,5 @@
{% from "second/macros.html" import with_errors %}
<!DOCTYPE html>
<html lang="en">
<head>
@ -9,10 +11,8 @@
<p>Install {{ app_name }}</p>
<form method="POST" action="/deploy/{{ app_name }}">
{% for field in form %}
<div>
{{ field.label() }}
{{ field() }}
</div>
{{ with_errors(field, style='font-weight: bold') }}
{% endfor %}
<input type="submit" value="Deploy" />
</form>

View File

@ -0,0 +1,16 @@
{#
From: https://wtforms.readthedocs.io/en/2.3.x/specific_problems/#rendering-errors
Usage: with_errors(form.field, style='font-weight: bold')
#}
{% macro with_errors(field) %}
<div class="form_field">
{% if field.errors %}
{% set css_class = 'has_error ' + kwargs.pop('class', '') %}
{{ field(class=css_class, **kwargs) }}
<ul class="errors">{% for error in field.errors %}<li>{{ error|e }}</li>{% endfor %}</ul>
{% else %}
{{ field(**kwargs) }}
{% endif %}
</div>
{% endmacro %}