generateur_v3/backend/api/services/database.py

16 lines
415 B
Python
Raw Normal View History

2022-09-16 21:50:55 +02:00
import random
import string
from sqlmodel import select, Session
from sqlmodel import SQLModel
def generate_unique_code(model: SQLModel, s: Session, length: int = 6):
while True:
code = ''.join(random.choices(string.ascii_uppercase, k=length))
is_unique = s.exec(select(model).where(
model.id_code == code)).first() == None
if is_unique:
break
return code