ref: products
src/blueprints/products.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 |
from flask import Blueprint, flash, request, render_template, redirect, url_for from ..types.product import Product from ..types.group import Group from ..types.product_category import ProductCategory import json pro = Blueprint('products', __name__, url_prefix = '/products') @pro.route('/', methods = ['GET', 'POST']) def index(): if request.method == 'GET': products = Product.query.all() return render_template('products.html', products = products, title = "Produtos - Feira Virtual Bem da Terra") else: return create(request.form) @pro.route('/new', methods = ['GET']) def new(): categories = ProductCategory.query.all() groups = Group.query.all() return render_template('new_product.html', groups = groups, categories = categories, title = "Novo Produto - Feira Virtual Bem da Terra") def create(params): title = params.get('title') group_id = params.get('group_id') category_id = params.get('category_id') product = Product.query.filter_by(title = title, category_id = category_id, group_id = group_id).first() if product: flash('A product with this name for this group and category already exists') return redirect(url_for('products.new')) product = Product( title = title, description = params.get('description'), group_id = group_id, category_id = category_id ) product.create() return redirect(url_for('products.index')) |