From 0075cee1ee3abb93fbbc0a9d81eed8c63ff2b0cf Mon Sep 17 00:00:00 2001 From: Son Nguyen Kim Date: Tue, 21 Sep 2021 11:27:37 +0200 Subject: [PATCH] default to UUID for api key code if the previous one is already used --- app/models.py | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/app/models.py b/app/models.py index 644d6f95..8983e72d 100644 --- a/app/models.py +++ b/app/models.py @@ -1769,18 +1769,12 @@ class ApiKey(db.Model, ModelMixin): user = db.relationship(User) @classmethod - def create(cls, user_id, name): - # generate unique code - found = False - while not found: - code = random_string(60) + def create(cls, user_id, name=None, **kwargs): + code = random_string(60) + if cls.get_by(code=code): + code = str(uuid.uuid4()) - if not cls.get_by(code=code): - found = True - - a = cls(user_id=user_id, code=code, name=name) - db.session.add(a) - return a + return super().create(user_id=user_id, name=name, code=code, **kwargs) class CustomDomain(db.Model, ModelMixin):