def finish_order(order): import nova from user import serv from json import dumps order.status = 2 useraccount = serv.get_user_account(order.user_id) user = serv.get_user_login(order.user_id) if not useraccount.tenant_id: tenant = nova.api().get_tenent(useraccount) useraccount.tenant_id = tenant['id'] usertenant = UserTenant(user) usertenant.tenant_id = tenant['id'] usertenant.tenant_name = tenant['name'] usertenant.admin_user_id = tenant['user_id'] key_pair = nova.api().gen_key(useraccount, tenant["name"]) usertenant.keypair = dumps(key_pair) uk = UserKey(key_content=key_pair) g.db.add(uk) g.db.add(usertenant) orderproducts = g.db.query(OrderProduct).filter( OrderProduct.order_id == order.id).all() for orderproduct in orderproducts: userproduct = UserProduct(user, orderproduct) g.db.add(userproduct) g.db.flush() g.db.commit()
def new_purchase(jwt): data = request.get_json() user_id = data.get('user_id', None) product_id = data.get('product_id', None) if user_id is None: abort(400) if product_id is None: abort(400) try: product = Product.query.filter( Product.id == product_id).one_or_none() if product is None or product.availability_status is False: abort(404) user_product = UserProduct(user_id=user_id, product_id=product_id) user_product.insert() except: abort(422) return jsonify({ 'success': True, 'user_id': user_id, 'product_id': product_id, 'message': 'Thankyou for shopping with us!!' })