Author: Pedro Lucas Porcellis <pedrolucasporcellis@gmail.com>
Display selected products
src/templates/cycle.html | 14 +++++++++----- src/types/order.py | 11 +++++++++--
diff --git a/src/templates/cycle.html b/src/templates/cycle.html index a70e9665cfab79abcd6bfd919f4c7648524c9916..4594afc511688f97183bad971e586b73e46f3355 100644 --- a/src/templates/cycle.html +++ b/src/templates/cycle.html @@ -21,11 +21,12 @@Categoria </tr> </thead> <tbody> + {% with selected_product_ids = current_order.selected_product_ids() %} {% for product in products %} - <tr> - <td> - {% if current_order %} - <input type="checkbox" name="product_ids" value="{{product.id}}"> + <tr> + <td> + {% if current_order %} + <input type="checkbox" name="product_ids" {{ 'checked' if product.id in selected_product_ids }} value="{{product.id}}"> {% endif %} </td> <td> {{ product.title }} </td> @@ -33,13 +34,16 @@{{ product.group.name }} <td> {{ product.category.name }}</td> </tr> {% endfor %} + {% endwith %} </tbody> </table> <section> <h4> O que vocĂȘ vai levar: </h4> <ul> - <li>Cebola - Grupo Germinari</li> + {% for product in current_order.products() %} + <li>{{ product.title }} - {{ product.group.name }}</li> + {% endfor %} </ul> <div style="text-align: left;"> diff --git a/src/types/order.py b/src/types/order.py index 6312222d1a1d7371655018a29223cfc4c0318ee0..e8ac1d2f31d79c7559fe1979797d07223755271b 100644 --- a/src/types/order.py +++ b/src/types/order.py @@ -1,6 +1,7 @@ from . import db from datetime import datetime as dt from .order_product import OrderProduct +from .product import Product class Order(db.Model): __tablename__ = 'orders' @@ -34,10 +35,16 @@ # Create the new entries for product_id in product_ids: OrderProduct(self.id, product_id, 1).create() - + + def selected_product_ids(self): + return list(map(lambda entry: entry.product_id, self.order_products)) + + def products(self): + return list(map(lambda entry_id: Product.query.get(entry_id), self.selected_product_ids())) + def to_json(self): return { "user_id": self.user_id, "cycle_id": self.cycle_id, - "products": list(map(lambda x: x.product_id, self.order_products)) + "products": self.selected_product_ids() }