ciclos

ref: responsive

src/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
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():
    if request.method == 'GET':
        current_cycle = Cycle.query.filter_by(status = 'published').first()
        return render_template('cycles.html', current_cycle = current_cycle, 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]
    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()

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

def create(params):
    start_at = params.get('start_at')
    end_at = params.get('end_at')

    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"

    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'),
        price_margin = params.get('price_margin')
    )

    cycle.create()

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