Viewing file: order_resource.py (1.86 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
from ahab.resources.base_resource import BaseResource from ahab.models.model import Model from ahab.models.stock_model import StockModel from json import loads from flask import request
class OrderResource(BaseResource): def __init__(self): self.labels={ 'id_estado' : 'status', 'id_pedido': 'order_id', 'vendedor': 'seller_id', 'id_cliente': 'client_id', 'descuento': 'discount', 'fecha': 'date' } BaseResource.__init__(self, 'pedidos', 'id_pedido', labels=self.labels ) self.items_model = Model('detalle_pedido', 'id') self.stock_model = StockModel()
def post(self): data = loads(request.data) print "*"*10, data, "*"*10 input = {} for d in data: if d in self.labels.values(): input[self.labels.keys()[self.labels.values().index(d)]] = data[d] else: input[d] = data[d]
input.pop('products', None) if 'client_name' in data: client = Model('clientes','id').get_by(**{'nombre': data['client_name']}) print 'client {}'.format(client) input.pop('client_name', None) input.update({'id_cliente': client['id']}) output = self.model.create(input) for p in data['products']: product_data = Model('productos', 'nombre').get(p['name']) item = { 'id_pedido': output['id_pedido'], 'cantidad': p['count'], 'nombre_producto': p['name'], 'precio': product_data['precio_venta'] } self.items_model.create(item) self.stock_model.remove_stock(p['name'], p['count'])
output.update({'products': data['products']}) return output, 200
|