From 1c85f39c823f55e1ab69696c46381f3c8bc5b175 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20M=C3=A9taireau?= Date: Wed, 9 Nov 2022 23:40:56 +0100 Subject: [PATCH] Change the way product refs are being generated. Add a salt of randomness to avoid conflicts. --- copanier/views/products.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/copanier/views/products.py b/copanier/views/products.py index 9ef4dfa..5ad6280 100644 --- a/copanier/views/products.py +++ b/copanier/views/products.py @@ -1,5 +1,8 @@ from datetime import datetime +import random +import string + from slugify import slugify from .core import app from ..models import Delivery, Product, Producer @@ -153,7 +156,8 @@ async def create_product(request, response, delivery_id, producer_id): product.producer = producer_id form = request.form product.update_from_form(form) - product.ref = slugify(f"{producer_id}-{product.name}-{product.unit}") + random_string = "".join(random.choices(string.ascii_lowercase + string.digits, k=8)) + product.ref = slugify(f"{producer_id}-{product.name}-{product.unit}-{random_string}") delivery.products.append(product) delivery.persist()