create GenEmail.create_custom_alias that should replace all random alias

This commit is contained in:
Son NK 2019-12-08 16:59:06 +01:00
parent b09f74c445
commit 8b9a158ac2

View file

@ -413,6 +413,22 @@ class GenEmail(db.Model, ModelMixin):
random_email = generate_email()
return GenEmail.create(user_id=user_id, email=random_email, custom=custom)
@classmethod
def create_custom_alias(cls, user_id, prefix):
if not prefix:
raise Exception("alias prefix cannot be empty")
# find the right suffix
found = False
while not found:
suffix = random_string(6)
email = f"{prefix}.{suffix}@{EMAIL_DOMAIN}"
if not cls.get_by(email=email):
found = True
return GenEmail.create(user_id=user_id, email=email, custom=True)
def __repr__(self):
return f"<GenEmail {self.id} {self.email}>"