ciclos

ref: master

core/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
37
38
39
from flask import flash, Blueprint, jsonify, make_response, render_template, redirect, url_for, request
from core.types.product_category import ProductCategory
from core.database import db
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 render_template('product_categories.html', categories = categories, title = "Categorias de Produtos - ")
    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')
    )

    db.add(category)
    db.commit()

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