Make slug eight characters

This commit is contained in:
Charlotte Van Petegem 2022-06-03 19:05:33 +02:00
parent 10327941d2
commit 0661016236
No known key found for this signature in database
GPG key ID: 019E764B7184435A
2 changed files with 5 additions and 5 deletions

View file

@ -17,7 +17,7 @@ from sqlalchemy.sql import text
def upgrade():
op.add_column('order', sa.Column(
'slug',
sa.String(length=7),
sa.String(length=8),
nullable=False,
# Default: random alphanumerical string
server_default=text('SUBSTRING(MD5(RAND()) FROM 1 FOR 7)')

View file

@ -11,12 +11,12 @@ from utils import first
from .database import db
from .user import User
BASE58_ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
BASE34_ALPHABET = '123456789abcdefghijkmnopqrstuvwxyz'
def generate_slug():
secret = ''.join(secrets.choice(BASE58_ALPHABET) for i in range(7))
secret = ''.join(secrets.choice(BASE34_ALPHABET) for i in range(8))
while Order.query.filter(Order.slug == secret).first() is not None:
secret = ''.join(secrets.choice(BASE58_ALPHABET) for i in range(7))
secret = ''.join(secrets.choice(BASE34_ALPHABET) for i in range(8))
return secret
class Order(db.Model):
@ -28,7 +28,7 @@ class Order(db.Model):
starttime = db.Column(db.DateTime)
stoptime = db.Column(db.DateTime)
public = db.Column(db.Boolean, default=True)
slug = db.Column(db.String(7), default=generate_slug, unique=True)
slug = db.Column(db.String(8), default=generate_slug, unique=True)
association = db.Column(db.String(120), nullable=False, server_default="")
items = db.relationship("OrderItem", backref="order", lazy="dynamic")