fix: optimize email validation in forms

Refactored the email lookup logic in the EmailForm class to fetch all users once and then search the list in Python, rather than making a database call for each email validation. This change reduces database load and potentially speeds up form processing by avoiding multiple queries. It's particularly beneficial when dealing with a large number of users, ensuring scalability and improved performance of the application.

The new approach retrieves the list of users upfront and iterates over it to find a match, falling back to `None` if no user with the specified email exists. This method is more efficient and aligns with best practices for handling database interactions in a web application.
This commit is contained in:
Kumi 2024-04-26 12:19:16 +02:00
parent aa4065e219
commit 2c976a9c03
Signed by: kumi
GPG key ID: ECBCC9082395383F

4
app.py
View file

@ -198,7 +198,9 @@ class EmailForm(FlaskForm):
users = User(planka) users = User(planka)
try: try:
user = users.get(email=field.data) user_list = users.get()
user = next((u for u in user_list if u["email"] == field.data), None)
if user: if user:
raise ValidationError( raise ValidationError(