2020-08-17 12:37:00 +03:00
|
|
|
from tiny0 import db
|
2020-07-20 16:00:36 +00:00
|
|
|
from tiny0.models import URL
|
2020-07-18 13:10:03 +00:00
|
|
|
from secrets import choice
|
|
|
|
|
|
2020-07-20 16:00:36 +00:00
|
|
|
# The characters used to generate the token
|
|
|
|
|
token_characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz_-"
|
2020-07-18 13:10:03 +00:00
|
|
|
|
2020-07-20 16:00:36 +00:00
|
|
|
def gen_valid_token():
|
|
|
|
|
while True:
|
|
|
|
|
# Generate a token
|
2020-07-25 18:28:42 +00:00
|
|
|
token = "".join(choice(token_characters) for i in range(6))
|
2020-07-20 16:00:36 +00:00
|
|
|
|
|
|
|
|
# If the token does not exists in the database
|
2020-08-17 12:37:00 +03:00
|
|
|
if not db.session.query(db.session.query(URL).filter_by(token=token).exists()).scalar():
|
2020-07-20 16:00:36 +00:00
|
|
|
# Break the loop
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
# Return the token
|
|
|
|
|
return token
|
2020-07-18 13:10:03 +00:00
|
|
|
|