def add(): title = 'Cadastrar Pedido' form = OrderForm() form.cliente.data = session.get('user_id', '') if request.form: form = OrderForm(request.form) if form.validate_on_submit(): if form.order_id.data: order = Order() ret = order.get(form.order_id.data) order.observacao = form.observacao.data order.clientes_id = form.cliente.data ret = order.update() flash(ret, 'info') return redirect(url_for('order.index')) else: flash('É necessário ao menos um produto para cadastrar um pedido.', 'danger') return render_template('order_form.html', form=form, title=title, mode='add', pageOrigin='add-page'), 200
def delete(order_id): order = Order.get(id=order_id) if not order: abort(404) order.delete() db.commit() return '', 204
def show(order_id): # This will serialize our data schema = OrderSchema() # This gets a order by ID order = Order.get(id=order_id) # If we can't find a order, send a 404 response if not order: abort(404) # otherwise, send back the order data as JSON return schema.dumps(order)
def edit(id): title = 'Editar Pedido' order = Order() ret = order.get(id) if not order.id: flash(ret, 'info') return redirect(url_for('order.index')) if session.get('user_grupo', '') == 'user': if order.clientes_id != session.get('user_id', ''): flash('Você não tem permissão para acessar este recurso.', 'info') return redirect(url_for('order.index')) order_p = OrderProduct() products = order_p.getByOrderId(order.id) images = [] for product in products: images.append(b64encode(product[7]).decode("utf-8")) if not request.form: form = OrderForm() form.cliente.data = str(order.clientes_id) form.observacao.data = order.observacao form.order_id.data = order.id orderproduct = OrderProduct() pedidos_produtos = orderproduct.getByOrderId(order.id) else: form = OrderForm(request.form) if form.validate_on_submit(): order.observacao = form.observacao.data order.clientes_id = form.cliente.data ret = order.update() flash(ret, 'info') return redirect(url_for('order.edit', id=order.id)) return render_template('order_form.html', form=form, title=title, mode='edit', orderId=order.id, clientName=order.cliente_name, pageOrigin='edit-page', products=products, images=images), 200
def update(order_id): schema = OrderSchema() order = Order.get(id=order_id) if not order: abort(404) try: data = schema.load(request.get_json()) order.set(**data) db.commit() except ValidationError as err: return jsonify({ 'message': 'Validation failed', 'errors': err.messages }), 422 return schema.dumps(order)
def delete(id): order = Order() ret = order.get(id) if not order.id: flash(ret, 'info') return redirect(url_for('order.index')) if session.get('user_grupo', '') == 'user': if order.clientes_id != session.get('user_id', ''): flash('Você não tem permissão para acessar este recurso.', 'info') return redirect(url_for('order.index')) if request.method == 'POST': order_p = OrderProduct() order_p.deleteByPedido(id) ret = order.delete() flash(ret, 'info') return redirect(url_for('order.index')) title = 'Deseja realmente deletar o pedido ' + str(order.id) + '?' return render_template('order_delete.html', orderId=id, title=title), 200
def report(id): order = Order() ret = order.get(id) if not order.id: flash(ret, 'info') return redirect(url_for('order.index')) order_p = OrderProduct() products = order_p.getByOrderId(order.id) images = [] for product in products: images.append(b64encode(product[7]).decode("utf-8")) ren = render_template('order_report.html', products=products, images=images, order=order) pdf = pdfkit.from_string(ren, False) response = make_response(pdf) response.headers['Content-Type'] = 'application/pdf' response.headers[ 'Content-Disposition'] = 'attachement; filename=relatorio-pedido.pdf' return response