feat: add email validation to form

Introduce a new custom validator within the EmailForm class to prevent duplicate email usage both in the local database and on the Planka platform. This validator checks if the submitted email is already present in the local `requests` table and then queries the Planka user management to ensure the email is not associated with an existing Planka user. If a duplicate is found in either case, it raises a ValidationError, effectively blocking the reuse of email addresses and facilitating a more secure and reliable user registration process.

Resolves issue with duplicate email registrations, enhancing data integrity and user experience.
This commit is contained in:
Kumi 2024-04-26 11:56:11 +02:00
parent debb69e112
commit aa4065e219
Signed by: kumi
GPG key ID: ECBCC9082395383F

39
app.py
View file

@ -169,6 +169,45 @@ class EmailForm(FlaskForm):
email = StringField("Email", validators=[DataRequired(), Email()])
submit = SubmitField("Submit")
def validate_email(self, field):
conn = sqlite3.connect("db.sqlite3")
cursor = conn.cursor()
cursor.execute(
"""
SELECT COUNT(*)
FROM requests
WHERE email = ?
""",
(field.data,),
)
count = cursor.fetchone()[0]
conn.close()
if count > 0:
raise ValidationError("This email address has already been used.")
planka = Planka(
url=config["Planka"]["url"],
username=config["Planka"]["username"],
password=config["Planka"]["password"],
)
users = User(planka)
try:
user = users.get(email=field.data)
if user:
raise ValidationError(
f"This email address is already associated with a user. Please log in instead."
)
except InvalidToken:
pass
@app.route("/", methods=["GET", "POST"])
def start_request():