diff --git a/second.py b/second.py index df72dfe..a235669 100644 --- a/second.py +++ b/second.py @@ -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) diff --git a/templates/second/install.html b/templates/second/install.html index 82a3e59..052e65f 100644 --- a/templates/second/install.html +++ b/templates/second/install.html @@ -1,3 +1,5 @@ +{% from "second/macros.html" import with_errors %} + @@ -9,10 +11,8 @@

Install {{ app_name }}

{% for field in form %} -
{{ field.label() }} - {{ field() }} -
+ {{ with_errors(field, style='font-weight: bold') }} {% endfor %}
diff --git a/templates/second/macros.html b/templates/second/macros.html new file mode 100644 index 0000000..518cb1e --- /dev/null +++ b/templates/second/macros.html @@ -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) %} +
+ {% if field.errors %} + {% set css_class = 'has_error ' + kwargs.pop('class', '') %} + {{ field(class=css_class, **kwargs) }} + + {% else %} + {{ field(**kwargs) }} + {% endif %} +
+{% endmacro %}