def orderdetail_add(order_id, order_item): if not validate_uuid(order_id,4): return uuid_notvalidate(obj, order_id) else: existing_order = Order.query.filter(Order.id == order_id).one_or_none() if existing_order is None: return uuid_notfound(obj, order_id) else: itemid=order_item['id'] if order_item['id'] else uuid.uuid4() # delete random repeat id while True: existing_item = Order_Item.query.filter(Order_Item.id == itemid).one_or_none() if existing_item is None: break else: itemid = uuid.uuid4() if "amount" not in order_item: order_item["amount"] = 1 order_item['order_id'] = order_id res = Order_Item(**order_item) res.save(False) oldpayment = existing_order.totalpayment print(order_item) newpayment=oldpayment+Decimal(order_item['item_price']*order_item["amount"]) new_order = {"totalpayment": newpayment} existing_order.update(**new_order) return jsonify(res.to_dict()), 201
def read(product_id): if not validate_uuid(product_id, 4): return uuid_notvalidate(obj, product_id) else: existing_item = Product.query.filter(Product.id == product_id).one_or_none() if existing_item is None: return uuid_notfound(obj, product_id) else: return jsonify(existing_item.to_dict())
def get_latest_repo(jid): schema = JobHistorySchema() if not validate_uuid(jid, version=1): return jsonify({"error": "Invalid UUID"}), 400 history = get_latest_history_from_job(jid) data = schema.dump(history) return jsonify(data), 200
def order_details(order_id): if not validate_uuid(order_id, 4): return uuid_notvalidate(obj, order_id) else: existing_item = Order.query.filter(Order.id == order_id).one_or_none() if existing_item is None: return uuid_notfound(obj, id) else: return jsonify(existing_item.to_dict())
def update(product_id, product): id = product.get('id') if not validate_uuid(id, 4): return uuid_notvalidate(obj, "id") existing_item = Product.query.filter(Product.id == id).one_or_none() if existing_item is None: return uuid_notfound(obj, 'id') else: existing_item.update(**product) return jsonify(existing_item.to_dict()), 201
def read_one(category_id): print(category_id) if not validate_uuid(category_id, 4): return uuid_notvalidate("Category", "id") cate = Category.query.filter(Category.id == category_id).one_or_none() if cate is not None: return jsonify(cate.to_dict()) else: message = 'Category do not found for Id:{id}'.format(id=category_id) return {"message": message}, 404
def update_status(order_id, order): id = order_id if not validate_uuid(id, 4): return uuid_notvalidate(obj, id) else: existing_item = Order.query.filter(Order.id == id).one_or_none() if existing_item is None: return uuid_notfound(obj, id) else: ret = existing_item.update(**order) return jsonify(ret.to_dict()),201
def delete(product_id): if not validate_uuid(product_id, 4): return uuid_notvalidate(obj, "id") existing_item = Product.query.filter(Product.id == product_id).one_or_none() if existing_item is None: return uuid_notfound(obj, 'id') else: existing_item.delete() msg = "{} with id({}) already delete".format(obj, product_id) return jsonify(message=msg)
def update(category_id, category): if not validate_uuid(category_id, 4): return uuid_notvalidate("Category", "id") updata_item = Category.query.filter( Category.id == category_id).one_or_none() if updata_item is not None: updata_item.update(**category) return jsonify(updata_item.to_dict()), 201 else: msg = 'Category not found for Id: {c_id}'.format(c_id=category_id) return {"message": msg}, 404
def create(product): id = product.get('id') if product.get('id') else str(uuid.uuid4()) if not validate_uuid(id, 4): return uuid_notvalidate(obj, "id") while True: existing_item = Product.query.filter(Product.id == id).one_or_none() if existing_item is None: product['id'] = id break else: id = str(uuid.uuid4()) item = Product.create(**product) return jsonify(item.to_dict()), 201
def orderdetail_update(item_id, order_item): id=order_item.get("id") order_id = order_item.get('order_id') if not validate_uuid(order_id, 4): return uuid_notvalidate(obj, order_id) elif not validate_uuid(id, 4): return uuid_notvalidate("Order_Item", id) else: existing_item = Order_Item.query.filter(Order_Item.id == id).one_or_none() existing_order = Order.query.filter(Order.id == order_id).one_or_none() if existing_item is None: return uuid_notfound("Order_Item", id) elif existing_order is None: return uuid_notfound(obj, order_id) else: if order_item['amount'] or order_item['item_price']: amount = order_item['amount'] if order_item['amount'] else existing_item['amount'] price = order_item['item_price'] if order_item['item_price']>=0 else existing_item['item_price'] existing_order["totalpayment"] += amount * price - existing_item['amount'] * existing_item['item_price'] # commit = false is make the transation and commit next sql existing_order.update(commit=False, **existing_order.to_dict()) existing_item.update(**order_item) return jsonify(existing_item.to_dict()), 201
def create(category): id = category.get('id') if category.get('id') else str(uuid.uuid4()) print(id) if not validate_uuid(id, 4): return uuid_notvalidate("Category", "id") while True: existing_cate = Category.query.filter(Category.id == id).one_or_none() if existing_cate is None: category['id'] = id break else: id = str(uuid.uuid4()) cate = Category.create(**category) return jsonify(cate.to_dict()), 201
def orderitem_delete(item_id): if not validate_uuid(item_id, 4): return uuid_notvalidate("Order_Item", item_id) else: existing_item = Order_Item.query.filter(Order_Item.id == id).one_or_none() if existing_item is None: return uuid_notfound("Order_Item", id) else: itemdict=existing_item.to_dict() existing_order = Order.query.filter(Order.id == itemdict['order_id']).one_or_none() if existing_order: existing_order["totalpayment"] -= itemdict["amount"] * itemdict['itemprice'] existing_order.update(commit=False, **existing_order.to_dict()) existing_item.delete() msg = "Order_Item with id({}) already deleted".format(item_id) return jsonify(message=msg)
def delete(category_id): if not validate_uuid(category_id, 4): return uuid_notvalidate("Category", "id") cate = Category.query.filter(Category.id == category_id).one_or_none() product = Product.query.filter( Product.category_id == category_id).one_or_none() if product is not None: msg = 'Can not delete category({}) when it has some production on it'.format( category_id) return {"message": msg}, 509 elif cate is not None: cate.delete() msg = 'Category {id} deleted'.format(id=category_id) return {"message": msg} # Otherwise, nope, didn't find that person else: msg = "Id:{id} do not found".format(id=category_id) return {"message": msg}, 404
def _get_output_data(output_list, heat, stack_id): """ 获取output数据 """ response = {'code': 200, 'msg': 'ok', 'data': []} for item in output_list['outputs']: output = heat.stacks.output_show(stack_id, item['output_key']) output_value = output['output']['output_value'] item = { 'vmId': output_value['vmId'], 'vncUrl': output_value['vncUrl'], 'networks': [] } if 'networks' in output_value: for net_name, ip_data in output_value['networks'].items(): if utils.validate_uuid(net_name): continue network = {'name': net_name, 'ip': ip_data[0]['addr']} item['networks'].append(network) response['data'].append(item) return response