From 4e19b9233ba49c58e3d38206ae628d2192ce97fe Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 13 Apr 2022 19:11:23 +0200 Subject: [PATCH] validate email is unique --- ereuse_devicehub/forms.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/ereuse_devicehub/forms.py b/ereuse_devicehub/forms.py index 7dddf884..2ae69094 100644 --- a/ereuse_devicehub/forms.py +++ b/ereuse_devicehub/forms.py @@ -12,6 +12,7 @@ from wtforms import ( ) from ereuse_devicehub.db import db +from ereuse_devicehub.resources.agent.models import Agent from ereuse_devicehub.resources.user.models import User COUNTRY = [(x.name, x.value) for x in Country] @@ -101,6 +102,21 @@ class ProfileForm(FlaskForm): if user.country: self.country.data = user.country.name + def validate(self, extra_validators=None): + is_valid = super().validate(extra_validators) + + if not is_valid: + return False + + # import pdb; pdb.set_trace() + email = self.email.data + if email != g.user.individual.email: + if Agent.query.filter_by(email=email).first(): + self.email.errors = ['You can not use this email.'] + return False + + return True + def save(self, commit=True): agent = g.user.individual agent.name = self.name.data