ciclos

commit 3e1fe9704a317e05d19beda6032f1e1d25764133

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

Allow to create an Order for the current cycle

 src/__init__.py | 3 +
 src/blueprints/cycles.py | 6 ++-
 src/blueprints/orders.py | 18 +++++++++
 src/templates/cycle.html | 83 +++++++++++++++++++++++------------------
 src/types/order.py | 23 +++++++++++
 src/types/user.py | 7 +++


diff --git a/src/__init__.py b/src/__init__.py
index 2eb1badb78280da05cda67bc28c0767d7c48c892..b327e6235689ecdff274dfabd38cef0eb0273525 100644
--- a/src/__init__.py
+++ b/src/__init__.py
@@ -28,12 +28,15 @@     from .blueprints.groups import groups
     from .blueprints.products import pro as products
     from .blueprints.product_categories import product_categories
     from .blueprints.cycles import cycles
+    from .blueprints.orders import orders
+    app.register_blueprint(orders)
     app.register_blueprint(basic)
     app.register_blueprint(auth)
     app.register_blueprint(product_categories)
     app.register_blueprint(products)
     app.register_blueprint(groups)
     app.register_blueprint(cycles)
+    app.register_blueprint(orders)
 
     with app.app_context():
         db.create_all()




diff --git a/src/blueprints/cycles.py b/src/blueprints/cycles.py
index 77c3221522a1303f69de0ecdf119c1dcaae12843..e7c158ee4502e7a05cb04abae31411f4fdce9cc9 100644
--- a/src/blueprints/cycles.py
+++ b/src/blueprints/cycles.py
@@ -1,5 +1,6 @@
 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
@@ -22,10 +23,11 @@ @cycles.route('/', methods = ['GET'])
 def show(id):
     cycle = Cycle.query.filter_by(id = id).first()
     products = Product.query.all()
+    current_order = current_user.current_order()
     if not cycle:
         return "Not found", 404
 
-    return render_template('cycle.html', cycle = cycle, products = products, title = "Carrinho - Feira Virtual")
+    return render_template('cycle.html', cycle = cycle, current_order = current_order, products = products, title = "Carrinho - Feira Virtual")
 
 def create(params):
     start_at = params.get('start_at')




diff --git a/src/blueprints/orders.py b/src/blueprints/orders.py
new file mode 100644
index 0000000000000000000000000000000000000000..6a8a37c89e36e154fbc76c6a2664939ffe524c48
--- /dev/null
+++ b/src/blueprints/orders.py
@@ -0,0 +1,18 @@
+from flask import request, redirect, url_for, Blueprint
+from flask_login import current_user
+
+from ..types.cycle import Cycle, CycleStatus
+from ..types.order import Order
+
+orders = Blueprint('orders', __name__, url_prefix = '/orders')
+
+@orders.route('/')
+def create():
+    current_cycle = Cycle.query.filter_by(status = CycleStatus.published).first()
+    
+    if not current_cycle:
+        return '', 401
+
+    Order.create_for(current_user, current_cycle) 
+    return redirect(url_for('cycles.show', id = current_cycle.id))
+  




diff --git a/src/templates/cycle.html b/src/templates/cycle.html
index 3e1e9c47722b29456546023eac9c9051b3700158..13f498542578d167ca684b79b4b855e37e04ba29 100644
--- a/src/templates/cycle.html
+++ b/src/templates/cycle.html
@@ -1,43 +1,54 @@
 <section class="a">
   <h1>{{ cycle.name }}</h1>
-  <em>{{ cycle.description }}</em>
-  <section class="products">
-    <form action="" method="post">
-      <table>
-        <thead>
+  <em>{{ cycle.description }}</em> 
+</section>
+
+<section>
+  {% if not current_order %}
+  <strong>Você não tem um pedido nesse ciclo. <a href="{{ url_for('orders.create') }}">Abrir um</a></strong>
+  {% endif %}
+</section>
+
+<section class="products">
+  <form action="" method="post">
+    <table>
+      <thead>
+        <tr>
+          <th></th>
+          <th>Nome do Produto</th>
+          <th>Grupo</th>
+          <th>Categoria</th>
+        </tr>
+      </thead>
+      <tbody>
+        {% for product in products %}
           <tr>
-            <th></th>
-            <th>Nome do Produto</th>
-            <th>Grupo</th>
-            <th>Categoria</th>
+            <td>
+              {% if current_order %}
+                <input type="checkbox" name="product_ids" > 
+              {% endif %}
+            </td>
+            <td> {{ product.title }} </td>
+            <td> {{ product.group.name }} </td>
+            <td> {{ product.category.name }}</td>
           </tr>
-        </thead>
-        <tbody>
-          {% for product in products %}
-            <tr>
-              <td> <input type="checkbox" name="product_ids" > </td>
-              <td> {{ product.title }} </td>
-              <td> {{ product.group.name }} </td>
-              <td> {{ product.category.name }}</td>
-            </tr>
-          {% endfor %}
-        </tbody>
-      </table>
+        {% endfor %}
+      </tbody>
+    </table>
 
-      <section>
-        <h4> O que você vai levar: </h4>
-        <ul>
-          <li>Cebola - Grupo Germinari - 2</li>
-        </ul>
-        
-        <div style="text-align: left;">
-          Total: <em>R$ 19,17</em>
-        </div>
-      </section>
+    <section>
+      <h4> O que você vai levar: </h4>
+      <ul>
+        <li>Cebola - Grupo Germinari</li>
+      </ul>
+      
+      <div style="text-align: left;">
+        Total: <em>R$ 19,17</em>
+      </div>
+    </section>
 
-      <div class="form-action">
-        <button type="submit" rel="button">Confirmar produtos</button>
-      </div>
-    </form> 
-  </section>
+    <div class="form-action">
+      <button type="submit" rel="button">Confirmar produtos</button>
+    </div>
+  </form> 
 </section>




diff --git a/src/types/order.py b/src/types/order.py
new file mode 100644
index 0000000000000000000000000000000000000000..f115753b9b9c57c14413acb54d9d042d9259d718
--- /dev/null
+++ b/src/types/order.py
@@ -0,0 +1,23 @@
+from . import db
+from datetime import datetime as dt
+
+class Order(db.Model):
+    __tablename__ = 'orders'
+  
+    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)
+    created_at = db.Column(db.DateTime, default = dt.utcnow)
+    updated_at = db.Column(db.DateTime, default = dt.utcnow)
+
+    def __init__(self, user_id, cycle_id):
+        self.user_id = user_id
+        self.cycle_id = cycle_id
+
+    @classmethod
+    def create_for(class_object, user, cycle):
+        return class_object(user.id, cycle.id).create()
+
+    def create(self):
+        db.session.add(self)
+        db.session.commit()




diff --git a/src/types/user.py b/src/types/user.py
index 50e7dc72fc33e604f1603a7fb03da40cc1df49c3..4c4c77772b35b4424690d9782463103a2cf832ed 100644
--- a/src/types/user.py
+++ b/src/types/user.py
@@ -1,6 +1,8 @@
 from . import db
 from flask_login import UserMixin
 from datetime import datetime as dt
+from .order import Order
+from .cycle import Cycle, CycleStatus
 
 class User(UserMixin, db.Model):
     __tablename__ = "users"
@@ -13,6 +15,8 @@     last_name = db.Column(db.String(250))
     created_at = db.Column(db.DateTime, default = dt.utcnow)
     updated_at = db.Column(db.DateTime, default = dt.utcnow)
 
+    orders = db.relationship("Order", backref = "user", lazy = True)
+    
     def __init__(self, email, password, username, first_name, last_name):
         self.email = email
         self.password = password
@@ -23,6 +27,9 @@
     def create(self):
         db.session.add(self)
         db.session.commit()
+
+    def current_order(self):
+        return Order.query.join(User).join(Cycle).filter(User.id == self.id, Cycle.status == CycleStatus.published).first()
 
     def as_dict(self):
         return {c.name: getattr(self, c.name) for c in self.__table__.columns}