def product_repertory_by_pid_list(self, pids): products = [Product(pid=pid) for pid in pids] return Repertory.select().where(Repertory.product << products)
def add_one_order(self, order_data, user): order_detail_data = order_data["order"] order_obj = SaleOrder( soid=str(uuid.uuid4()), addr_level1=order_data["addr_level1"], addr_level2=order_data["addr_level2"], addr_level3=order_data["addr_level3"], shipping_cost=order_data["shipping_cost"], receiver=order_data["receiver"], phone=order_data["phone"], receipt_sort=order_data["receipt_sort"], receipt_content=order_data["receipt_content"], receipt_title=order_data["receipt_title"], pay_sort=order_data["pay_sort"], ) if user: order_obj.user = User(**user) pay_sort_ll = PaySortLL() order_state_ll = OrderStateLL() if order_obj.pay_sort == pay_sort_ll.value(COD): order_obj.order_state = order_state_ll.value(HANDLING) elif order_obj.pay_sort == pay_sort_ll.value(ALIPAY): order_obj.order_state = order_state_ll.value(NOPAY) product_ll = ProductLL() product_details = product_ll.get_product_obj_by_pid_list(order_detail_data.keys()) order_detail_obj = [] product_count = 0 product_sum_price = 0.0 for pd in product_details: sod = SaleOrderDetail( sod_id=str(uuid.uuid4()), sale_order=order_obj, product=pd, purchase_count=order_detail_data[pd.pid], price=pd.price, ) product_count += sod.purchase_count product_sum_price += sod.purchase_count * sod.price order_detail_obj.append(sod) order_obj.product_count = product_count order_obj.product_sum_price = product_sum_price trace = SaleOrderTrace(sot_id=str(uuid.uuid4()), sale_order=order_obj, state=order_obj.order_state) reper_ll = RepertoryLL() product_reper_list = reper_ll.product_repertory_by_pid_list(order_detail_data.keys()) with DB.transaction() as trans: try: for order_detail in order_detail_obj: product = order_detail.product product_reper_list = filter(lambda x: x.product.pid == product.pid, product_reper_list) if not product_reper_list: product_reper = Repertory(product=product) # raise NoProductInRepertory(pid) else: product_reper = product_reper_list[0] # if product_reper.store_count < order_detail.purchase_count: # raise NoEnoughStore(product.pid) product_reper.sale_count += order_detail.purchase_count product_reper.store_count -= order_detail.purchase_count product_reper.save(force_insert=True) order_obj.save(force_insert=True) for d in order_detail_obj: d.save(force_insert=True) trace.save(force_insert=True) trans.commit() except NoProductInRepertory as e: self.err[e.product_id] = str(e) trans.rollback() except NoEnoughStore as e: self.err[e.product_id] = str(e) trans.rollback() except Exception, e: self.err["all"] = str(e) trans.rollback()