ref: cycles
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 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, flash, request, render_template, redirect, url_for from ..types.product import Product from ..types.group import Group from ..types.product_category import ProductCategory import pdb 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, price = params.get('price') ) product.create() return redirect(url_for('products.index')) @pro.route("/<id>", methods = ['GET']) def show(id): product = Product.query.filter_by(id = id).first() return jsonify(product.to_json()) @pro.route("/<id>/balance", methods = ['GET', 'POST']) def balance(id): product = Product.query.filter_by(id = id).first() if request.method == "GET": return render_template('edit_product_balance.html', product = product, title = "Editar Balanco - Feira Virtual Bem da Terra") else: product.balance.update( value = float(request.form.get('value')) ) return redirect(url_for('products.show', id = product.id)) |