ciclos

commit fabfde6249ceb793df155c0e6c19a5e71db34d2e

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

Add a form to create cycles

 src/blueprints/cycles.py | 31 +++++++++++++++++++++++---
 src/templates/new_cycle.html | 43 ++++++++++++++++++++++++++++++++++++++
 src/types/cycle.py | 5 ++-


diff --git a/src/blueprints/cycles.py b/src/blueprints/cycles.py
index e8df9ff64c7d7ec26cf43d6dc228f15d3b6ced6a..bed6f06f9a44bdcb8de4fd3269977195354795c8 100644
--- a/src/blueprints/cycles.py
+++ b/src/blueprints/cycles.py
@@ -1,17 +1,40 @@
-from flask import Blueprint, url_for, request, render_template, redirect
+from flask import Blueprint, jsonify, url_for, request, render_template, redirect
 
 cycles = Blueprint('cycles', __name__, url_prefix = '/cycles')
+
+from ..types.cycle import Cycle, CycleStatus
 
 @cycles.route('/', methods = ['GET', 'POST'])
 def index():
     if request.method == 'GET':
         return 'yo'
     else:
-        create(request.form)
+        return create(request.form)
 
 @cycles.route('/new', methods = ['GET'])
 def new():
-    return render_template('new_cycle.html', title = "Abrir novo Ciclo - Feira Virtual Bem da Terra")
+    cycle_status_options = [(cycle.value, cycle.name) for cycle in CycleStatus]
+    return render_template('new_cycle.html', cycles = cycle_status_options, title = "Abrir novo Ciclo - Feira Virtual Bem da Terra")
 
 def create(params):
-    return "co"
+    start_at = params.get('start_at')
+    end_at = params.get('end_at')
+
+    cycle = Cycle.query.filter_by(status = CycleStatus.published.value).first()
+
+    if cycle:
+        return "there's an ongoing cycle"
+
+    cycle = Cycle(
+        name = params.get('name'),
+        description = params.get('description'),
+        start_at = start_at,
+        end_at = end_at,
+        delivery_start_at = params.get('delivery_start_at'),
+        delivery_end_at = params.get('delivery_end_at'),
+        status = params.get('status')
+    )
+
+    cycle.create()
+
+    return redirect(url_for('cycles.index'))




diff --git a/src/templates/new_cycle.html b/src/templates/new_cycle.html
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..9eb448f63bdd868853f69748baf979e5066afc42 100644
--- a/src/templates/new_cycle.html
+++ b/src/templates/new_cycle.html
@@ -0,0 +1,43 @@
+{% extends 'layout.html' %}
+
+{% block body %}
+  <form action="/cycles/" method="POST">
+    <div class="form-section">
+      <label for="">Name</label>
+      <input type="text" name="name">
+    </div>
+    <div class="form-section">
+      <label for="">Description</label>
+      <textarea rows="10" columns="10" name="description"></textarea>
+    </div>
+    <div class="form-section">
+      <label for="">Start At</label>
+      <input name="start_at" type="text">
+    </div>
+    <div class="form-section">
+      <label for="">Ends At</label>
+      <input name="end_at" type="text">
+    </div>
+    <div class="form-section">
+      <label for="">Delivery Start At</label>
+      <input name="delivery_start_at" type="text">
+    </div>
+    <div class="form-section">
+      <label for="">Delivery Ends At</label>
+      <input name="delivery_end_at" type="text">
+    </div>
+ 
+    <div class="form-section">
+      <label for="">Status</label>
+      <select name="status">
+        {% for cycle in cycles %}
+          <option value="{{cycle[0]}}">{{cycle[1]}}</option>
+        {% endfor %}
+      </select>
+    </div>
+    
+    <div class="form-action">
+      <button type="submit">Criar Ciclo</button>
+    </div>
+  </form>
+{% endblock %}




diff --git a/src/types/cycle.py b/src/types/cycle.py
index 63be69f220d5d74add0a3a3db3d82c299b5f90e0..41fa195cd630b8f7c4acee30af56c3fcf67e9e8b 100644
--- a/src/types/cycle.py
+++ b/src/types/cycle.py
@@ -2,7 +2,7 @@ from . import db
 from datetime import datetime as dt
 import sqlalchemy as sa
 import sqlalchemy_utils as sau
-import enum
+from enum import Enum
 
 class CycleStatus(Enum):
     draft = "draft"
@@ -28,13 +28,14 @@
     created_at = db.Column(db.DateTime, default = dt.utcnow)
     updated_at = db.Column(db.DateTime, default = dt.utcnow)
 
-    def __init__(self, name, description, start_at, end_at, delivery_start_at, delivery_end_at):
+    def __init__(self, name, description, start_at, end_at, delivery_start_at, delivery_end_at, status):
         self.name = name
         self.description = description
         self.start_at = start_at
         self.end_at = end_at
         self.delivery_start_at = delivery_start_at
         self.delivery_end_at = delivery_end_at
+        self.status = status
 
     def create(self):
         db.session.add(self)