From 2c976a9c03589f3bdd1d7aa65b45f1d1b3ebc78f Mon Sep 17 00:00:00 2001 From: Kumi Date: Fri, 26 Apr 2024 12:19:16 +0200 Subject: [PATCH] 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. --- app.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index 4a63dee..b1607b8 100644 --- a/app.py +++ b/app.py @@ -198,7 +198,9 @@ class EmailForm(FlaskForm): users = User(planka) 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: raise ValidationError(