validate email is unique

This commit is contained in:
Cayo Puigdefabregas 2022-04-13 19:11:23 +02:00
parent 04cdb8181f
commit 4e19b9233b

View file

@ -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