def _add_order_to_db(form): order = Order() order.status = 1 order.subcontractor_id = form.subcontractor.data order.cost_center_id = form.cost_center.data db.session().add(order) db.session().flush() return order.id
def insert_order_to_db(order_details): """ Function to insert order to DB params: order_details : dict """ # converting date to python datetime object # print("shubham") order_created_at = convert_to_datetime(order_details['created_at']) # if phone is not present set to None phone_num = None if not order_details['phone'] else order_details['phone'] # if email is not present set to None email_id = None if not order_details['email'] else order_details['email'] default_address = order_details['customer']['default_address'] formatted_address = format_address(default_address) try: # print("shubham") order = Order(id=order_details['order_number'], customer_name=order_details['customer']['first_name'] + ' ' + order_details['customer']['last_name'], currency=order_details['customer']['currency'], total_price=order_details['total_price'], sub_total_price=order_details['subtotal_price'], created_at=order_created_at, email=email_id, phone_no=phone_num, address=formatted_address) db.session.add(order) db.session.commit() except Exception as e: print(e) return
async def place_orders(request): """ curl -d '[{"book_id": 1, "shop_id": 1, "amount": 1}]' -H 'Authorization: xxxxx' returns list of created orders if order for this user with this book_id/shop_id already exists - increases amount returns list of orders """ try: json_text = await request.text() data = json.loads(json_text) for o in data: if o['amount'] <= 0: raise ValueError except (json.JSONDecodeError, ValueError): logging.error(f"bad json {json_text}") raise web.HTTPBadRequest async with request.app['db'].acquire() as conn: try: order_res = await Order.place_orders( orders_list=data, user_id=request['user'].id, conn=conn) except: logging.exception(f"error in input data") raise web.HTTPBadRequest return web.json_response( {'orders': [Order.order_to_dict(o) for o in order_res]})
async def orders(request): """ история заказов для авторизованного пользователя """ app = request.app user = request['user'] async with app['db'].acquire() as conn: user_orders = await Order.get_user_orders(user, conn=conn) return web.json_response( {'orders': [Order.order_to_dict(o) for o in user_orders]})
def _add(item: Item, quantity: str, sold_at: datetime.date) -> Result: quantity = int(quantity) if not quantity: return Result(Status.FAILED) db.session.add( Order(item=item, price=item.price * quantity, quantity=quantity, sold_at=sold_at)) db.session.commit() return Result(Status.SUCCEEDED)
def setUp(self): db.drop_all() db.create_all() Genre.add_genre('test_genre') self.genre = Genre.query.filter_by(name='test_genre').first() add_item = { 'name': 'test_item', 'genre': self.genre, 'price': 100, 'is_active': True, 'is_high_priority': True} Item.add_item(**add_item) self.item = Item.query.filter_by(name='test_item').first() self.today = datetime.today().date() db.session.add(Order( item=self.item, price=self.item.price * 3, quantity=3, sold_at=self.today) ) db.session.commit()
def get(self, period=None): orders = Order.get_orders_by_filter(period) return self.response(orders)
def get(self): data = Order.get_today_sum() return self.response(data)