ciclos

commit 0d5a61c4f929b44ee1630622e94a468cc49b9a33

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

Add Status to Order and allow user to save order as draft

 src/alembic/versions/b1a2b379ee3d_add_status_to_order.py | 24 ++++++++++
 src/blueprints/cycles.py | 2 
 src/blueprints/orders.py | 13 +++++
 src/templates/cycle.html | 9 +++
 src/types/order.py | 17 +++++++


diff --git a/src/alembic/versions/b1a2b379ee3d_add_status_to_order.py b/src/alembic/versions/b1a2b379ee3d_add_status_to_order.py
new file mode 100644
index 0000000000000000000000000000000000000000..b4c3e533052371a95004af97415374e16f9cba80
--- /dev/null
+++ b/src/alembic/versions/b1a2b379ee3d_add_status_to_order.py
@@ -0,0 +1,24 @@
+"""add status to order
+
+Revision ID: b1a2b379ee3d
+Revises: 6454dfce4c2d
+Create Date: 2019-12-17 01:33:05.734684
+
+"""
+from alembic import op
+from sqlalchemy import Column
+import sqlalchemy as sa
+
+
+# revision identifiers, used by Alembic.
+revision = 'b1a2b379ee3d'
+down_revision = '6454dfce4c2d'
+branch_labels = None
+depends_on = None
+
+
+def upgrade():
+    op.add_column('orders', Column('status', sa.String(255), server_default = 'draft'))
+
+def downgrade():
+    op.drop_column('orders', 'status')




diff --git a/src/blueprints/cycles.py b/src/blueprints/cycles.py
index e7c158ee4502e7a05cb04abae31411f4fdce9cc9..f53f1c30ed28e9ae5e0b27680054399394bb0024 100644
--- a/src/blueprints/cycles.py
+++ b/src/blueprints/cycles.py
@@ -1,10 +1,12 @@
 from flask import Blueprint, jsonify, url_for, request, render_template, redirect
 from flask_login import current_user
+
 import pdb
 cycles = Blueprint('cycles', __name__, url_prefix = '/cycles')
 
 from ..types.cycle import Cycle, CycleStatus
 from ..types.product import Product
+from ..types.order import OrderStatus
 
 @cycles.route('/', methods = ['GET', 'POST'])
 def index():




diff --git a/src/blueprints/orders.py b/src/blueprints/orders.py
index c753fc04f9fc494ab058041c0d156f9d57edc4b6..c4004faa9754f45b56f48d1b0e1238e1b3bf70a1 100644
--- a/src/blueprints/orders.py
+++ b/src/blueprints/orders.py
@@ -3,6 +3,7 @@ from flask_login import current_user
 
 from ..types.cycle import Cycle, CycleStatus
 from ..types.order import Order
+import pdb
 
 orders = Blueprint('orders', __name__, url_prefix = '/orders')
 
@@ -25,5 +26,17 @@     if not order:
         order = Order.create_for(current_user, current_cycle)
     
     order.add_products(request.form.getlist('product_ids'))
+    
+    if 'confirm' in request.form:
+        order.confirm()
 
     return redirect(url_for('cycles.show', id = current_cycle.id))
+
+@orders.route('/<id>/reopen', methods = ['GET'])
+def reopen(id):
+    order = Order.query.get(id)
+
+    order.reopen()
+
+    current_cycle = Cycle.query.filter_by(status = CycleStatus.published).first()
+    return redirect(url_for('cycles.show', id = current_cycle.id))




diff --git a/src/templates/cycle.html b/src/templates/cycle.html
index bc9a853bec2b8d21d4245fb79a557217001bc150..aaf644830a6f5067f25e6911fd2c6f1edf155e4a 100644
--- a/src/templates/cycle.html
+++ b/src/templates/cycle.html
@@ -3,6 +3,10 @@   

{{ cycle.name }}

<em>{{ cycle.description }}</em> </section> +{% if current_order.status.value == 'confirmed' %} +<strong> Seu pedido está {{ current_order.status.value }} <a href="{{ url_for('orders.reopen', id = current_order.id) }}">Deseja reabrir?</a></strong> +{% endif %} + <section> {% if not current_order %} <strong>Você não tem um pedido nesse ciclo. <a href="{{ url_for('orders.create') }}">Abrir um</a></strong> @@ -52,7 +56,10 @@
</section> <div class="form-action"> - <button type="submit" rel="button">Confirmar produtos</button> + <button type="submit" {{ "disabled" if current_order.status.value == 'confirmed' }} name="confirm" rel="button">Confirmar produtos</button> + {% if current_order.status.value == 'draft' %} + <button type="submit" name="draft" rel="button">Salvar pedido como rascunho</button> + {% endif %} </div> </form> </section> diff --git a/src/types/order.py b/src/types/order.py index 6470f0a2e78cde41a74b3c79a9e337c0849fabd0..560e5931796547a1055d8b4fa481faf5a4a233b4 100644 --- a/src/types/order.py +++ b/src/types/order.py @@ -3,6 +3,13 @@ from datetime import datetime as dt from .order_product import OrderProduct from .product import Product from sqlalchemy.sql import func +import sqlalchemy as sa +import sqlalchemy_utils as sau +from enum import Enum + +class OrderStatus(Enum): + draft = 'draft' + confirmed = 'confirmed' class Order(db.Model): __tablename__ = 'orders' @@ -10,6 +17,8 @@ id = db.Column(db.Integer, primary_key = True) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable = False) cycle_id = db.Column(db.Integer, db.ForeignKey('cycles.id'), nullable = False) + status = db.Column(sau.ChoiceType(OrderStatus, impl = sa.String()), server_default = 'draft') + created_at = db.Column(db.DateTime, default = dt.utcnow) updated_at = db.Column(db.DateTime, default = dt.utcnow) @@ -36,6 +45,14 @@ # Create the new entries for product_id in product_ids: OrderProduct(self.id, product_id, 1).create() + + def reopen(self): + self.status = OrderStatus.draft + db.session.commit() + + def confirm(self): + self.status = OrderStatus.confirmed + db.session.commit() def selected_product_ids(self): return list(map(lambda entry: entry.product_id, self.order_products))