diff --git a/copanier/views/products.py b/copanier/views/products.py index 11d246e..18574d2 100644 --- a/copanier/views/products.py +++ b/copanier/views/products.py @@ -29,6 +29,32 @@ async def list_products(request, response, id): response.html(template_name, template_params) +@app.route("/produits/{delivery_id}/producteurs/créer", methods=["GET", "POST"]) +async def create_producer(request, response, delivery_id): + delivery = Delivery.load(delivery_id) + producer = None + if request.method == "POST": + form = request.form + name = form.get("name") + producer_id = slugify(name) + + producer = Producer(name=name, id=producer_id) + producer.referent = form.get("referent") + producer.referent_tel = form.get("referent_tel") + producer.referent_name = form.get("referent_name") + producer.description = form.get("description") + producer.contact = form.get("contact") + + delivery.producers[producer_id] = producer + delivery.persist() + response.message(f"« {producer.name} » à bien été créé !") + response.redirect = f"/produits/{delivery.id}/producteurs/{producer.id}" + + response.html( + "products/edit_producer.html", {"delivery": delivery, "producer": producer} + ) + + @app.route("/produits/{delivery_id}/producteurs/{producer_id}", methods=["GET", "POST"]) async def edit_producer(request, response, delivery_id, producer_id): delivery = Delivery.load(delivery_id) @@ -83,32 +109,6 @@ async def delete_producer(request, response, delivery_id, producer_id): ) -@app.route("/produits/{delivery_id}/producteurs/créer", methods=["GET", "POST"]) -async def create_producer(request, response, delivery_id): - delivery = Delivery.load(delivery_id) - producer = None - if request.method == "POST": - form = request.form - name = form.get("name") - producer_id = slugify(name) - - producer = Producer(name=name, id=producer_id) - producer.referent = form.get("referent") - producer.referent_tel = form.get("referent_tel") - producer.referent_name = form.get("referent_name") - producer.description = form.get("description") - producer.contact = form.get("contact") - - delivery.producers[producer_id] = producer - delivery.persist() - response.message(f"« {producer.name} » à bien été créé !") - response.redirect = f"/produits/{delivery.id}/producteurs/{producer.id}" - - response.html( - "products/edit_producer.html", {"delivery": delivery, "producer": producer} - ) - - @app.route( "/produits/{delivery_id}/producteurs/{producer_id}/produits/{product_ref}", methods=["GET", "POST"],