From 8b9a158ac2a6294706ae2d696e9303c942e0318e Mon Sep 17 00:00:00 2001 From: Son NK Date: Sun, 8 Dec 2019 16:59:06 +0100 Subject: [PATCH] create GenEmail.create_custom_alias that should replace all random alias --- app/models.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/app/models.py b/app/models.py index 6cc06053..2fd092f7 100644 --- a/app/models.py +++ b/app/models.py @@ -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""