ciclos

ref: master

core/blueprints/cycles.py


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from flask import Blueprint, jsonify, url_for, request, render_template, redirect
from flask_login import current_user
from datetime import datetime as dt

import pdb
cycles = Blueprint('cycles', __name__, url_prefix = '/cycles')

from core.database import db

from core.types.cycle import Cycle, CycleStatus
from core.types.product import Product
from core.types.order import OrderStatus
#from core.types.configuration import Configuration

@cycles.route('/', methods = ['GET', 'POST'])
def index():
    if request.method == 'GET':
        current_cycle = Cycle.query.filter_by(status = 'published').first()

        closed_cycles = Cycle.query.filter_by(status = 'delivered').all()

        return render_template('cycles.html', current_cycle = current_cycle, closed_cycles = closed_cycles, title = "Ciclos - Feira Virtual Bem da Terra")
    else:
        return create(request.form)

@cycles.route('/new', methods = ['GET'])
def new():
    cycle_status_options = [(cycle.value, cycle.name) for cycle in CycleStatus]
    # configuration = Configuration.query.first()
    return render_template('new_cycle.html', cycles = cycle_status_options, title = "Abrir novo Ciclo - Feira Virtual Bem da Terra")

@cycles.route('/<id>', 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, current_order = current_order, products = products, title = "Carrinho - Feira Virtual")

@cycles.route('/<id>/close', methods = ['GET'])
def close(id):
    if current_user.is_admin():
        cycle = Cycle.query.get(id)
        cycle.close()
        db.commit()

        return redirect(url_for('cycles.index'))
    else:
        return "Not found", 404

def create(params):
    cycle = Cycle.query.filter_by(status = CycleStatus.published.value).first()

    # TODO: Better handling an ongoing cycle when create another one
    if cycle:
        return "there's an ongoing cycle"

    start_at = build_date_from_params(params.get('start_at_day'), params.get('start_at_month'), params.get('start_at_year'))
    end_at = build_date_from_params(params.get('end_at_day'), params.get('end_at_month'), params.get('end_at_year'))

    delivery_start_at = build_date_from_params(params.get('delivery_start_at_day'), params.get('delivery_start_at_month'), params.get('delivery_start_at_year'))
    delivery_end_at = build_date_from_params(params.get('delivery_end_at_day'), params.get('delivery_end_at_month'), params.get('delivery_end_at_year'))

    cycle = Cycle(
        name = params.get('name'),
        description = params.get('description'),
        start_at = start_at,
        end_at = end_at,
        delivery_start_at = delivery_start_at,
        delivery_end_at = delivery_end_at,
        status = params.get('status'),
        price_margin = float(params.get('price_margin'))
    )

    db.add(cycle)
    db.commit()

    return redirect(url_for('cycles.index'))

def build_date_from_params(day, month, year):
    return dt(int(year), int(month), int(day))