16 lines
415 B
Python
16 lines
415 B
Python
|
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
|
||
|
|