Sync update of localpart, domain_name and email

master
Alexander Graf 3 years ago
parent 0c38128c4e
commit 8bc4445572

@ -355,9 +355,21 @@ class Email(object):
self.localpart, self.domain_name = value.rsplit('@', 1) self.localpart, self.domain_name = value.rsplit('@', 1)
self._email = value self._email = value
# hack for email declared attr - when _email is not updated yet @staticmethod
def __str__(self): def _update_localpart(target, value, *_):
return str(f'{self.localpart}@{self.domain_name}') if target.domain_name:
target._email = f'{value}@{target.domain_name}'
@staticmethod
def _update_domain_name(target, value, *_):
if target.localpart:
target._email = f'{target.localpart}@{value}'
@classmethod
def __declare_last__(cls):
# gets called after mappings are completed
sqlalchemy.event.listen(User.localpart, 'set', cls._update_localpart, propagate=True)
sqlalchemy.event.listen(User.domain_name, 'set', cls._update_domain_name, propagate=True)
def sendmail(self, subject, body): def sendmail(self, subject, body):
""" send an email to the address """ """ send an email to the address """

@ -1030,8 +1030,8 @@ class BaseSchema(ma.SQLAlchemyAutoSchema, Storage):
self.opts.sqla_session.add(item) self.opts.sqla_session.add(item)
return item return item
# stop early if item has no password attribute # stop early when not updating or item has no password attribute
if not hasattr(item, 'password'): if not self.context.get('update') or not hasattr(item, 'password'):
return item return item
# did we hash a new plaintext password? # did we hash a new plaintext password?

Loading…
Cancel
Save