ciclos

ref: responsive

src/blueprints/product_categories.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
from flask import flash, Blueprint, jsonify, make_response, render_template, redirect, url_for, request
from ..types.product_category import ProductCategory
import json

product_categories = Blueprint('product_categories', __name__, url_prefix = '/product_categories')

@product_categories.route('/', methods = ['GET', 'POST'])
def index():
    if request.method == 'GET':
        categories = ProductCategory.query.all()

        return jsonify(list(map(lambda cat: cat.to_json(), categories)))
    else:
        return create(request.form)

@product_categories.route('/new', methods = ['GET'])
def new():
    return render_template('new_product_category.html', title = "Nova Categoria de Produto - Feira Virtual Bem da Terra")

def create(params):
    name = params.get('name')

    category = ProductCategory.query.filter_by(name = name).first()

    if category:
        flash('This category is created already')
        return redirect(url_for('product_categories.new'))
    
    category = ProductCategory(
        name = name,
        description = params.get('description')
    )

    category.create()

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