ciclos

commit b525e6530d337680fb00275e9d1a2deb28e19362

Author: Pedro Lucas Porcellis <pedrolucasporcellis@gmail.com>

Add a cancel order

 src/blueprints/orders.py | 9 +++++++++
 src/types/order.py | 7 ++++++-


diff --git a/src/blueprints/orders.py b/src/blueprints/orders.py
index c4004faa9754f45b56f48d1b0e1238e1b3bf70a1..40f799c9a5ac0ea9f831f391c89ae67ee93d6558 100644
--- a/src/blueprints/orders.py
+++ b/src/blueprints/orders.py
@@ -40,3 +40,12 @@     order.reopen()
 
     current_cycle = Cycle.query.filter_by(status = CycleStatus.published).first()
     return redirect(url_for('cycles.show', id = current_cycle.id))
+
+@orders.route('/<id>/cancel', methods = ['GET'])
+def cancel(id):
+    order = Order.query.get(id)
+
+    order.cancel()
+
+    current_cycle = Cycle.query.filter_by(status = CycleStatus.published).first()
+    return redirect(url_for('cycles.show', id = current_cycle.id))




diff --git a/src/types/order.py b/src/types/order.py
index c5b27a93d54f8a33aaaee3594a57c80324a67c45..b30c5bdfd0404f912dc4f36da77f7194cf8b3169 100644
--- a/src/types/order.py
+++ b/src/types/order.py
@@ -22,7 +22,7 @@
     created_at = db.Column(db.DateTime, default = dt.utcnow)
     updated_at = db.Column(db.DateTime, default = dt.utcnow)
 
-    order_products = db.relationship('OrderProduct', backref="order", lazy = True)
+    order_products = db.relationship('OrderProduct', backref="order", lazy = True, cascade="save-update, merge, delete")
 
     def __init__(self, user_id, cycle_id):
         self.user_id = user_id
@@ -52,6 +52,11 @@         db.session.commit()
 
     def confirm(self):
         self.status = OrderStatus.confirmed
+        db.session.commit()
+
+    def cancel(self):
+        db.session.delete(self)
+
         db.session.commit()
 
     def selected_product_ids(self):