add param WORDS_FILE_PATH, use random_words when generating email

This commit is contained in:
Son NK 2019-07-03 22:13:41 +02:00 committed by Son NK
parent 8c3afddaa7
commit 1703f4b5f6
4 changed files with 29 additions and 3 deletions

View file

@ -57,3 +57,5 @@ PARTNER_CODES = ["SL2019"]
# Allow user to have 1 year of premium: set the expiration_date to 1 year more
PROMO_CODE = "SIMPLEISBETTER"
WORDS_FILE_PATH = os.environ.get("WORDS_FILE_PATH")

View file

@ -9,11 +9,11 @@ from flask_login import UserMixin
from sqlalchemy_utils import ArrowType
from app import s3
from app.config import URL, MAX_NB_EMAIL_FREE_PLAN, EMAIL_DOMAIN
from app.config import EMAIL_DOMAIN, MAX_NB_EMAIL_FREE_PLAN, URL
from app.extensions import db
from app.log import LOG
from app.oauth_models import Scope
from app.utils import convert_to_id, random_string
from app.utils import convert_to_id, random_string, random_words
class ModelMixin(object):
@ -308,7 +308,7 @@ class OauthToken(db.Model, ModelMixin):
def generate_email() -> str:
"""generate an email address that does not exist before"""
random_email = random_string(40) + "@" + EMAIL_DOMAIN
random_email = random_words() + "@" + EMAIL_DOMAIN
# check that the client does not exist yet
if not GenEmail.get_by(email=random_email):

View file

@ -4,6 +4,19 @@ import urllib.parse
from unidecode import unidecode
from .config import WORDS_FILE_PATH
from .log import LOG
with open(WORDS_FILE_PATH) as f:
LOG.d("load words file: %s", WORDS_FILE_PATH)
_words = f.read().split()
def random_words():
"""Generate a random words. Used to generate user-facing string, for ex email addresses"""
nb_words = random.randint(3, 5)
return "_".join([random.choice(_words) for i in range(nb_words)])
def random_string(length=10):
"""Generate a random string of fixed length """

11
tests/test_utils.py Normal file
View file

@ -0,0 +1,11 @@
from app.utils import random_string, random_words
def test_random_words():
s = random_words()
assert len(s) > 0
def test_random_string():
s = random_string()
assert len(s) > 0