Exemplo n.º 1
0
async def test_alchemy_features():
    async with async_session() as session:
        upd_stm = update(Order).where(Order.id == 2).values(weight=15)
        await session.execute(upd_stm)

        await session.commit()
    print('pizda')
Exemplo n.º 2
0
    async def post(self):
        response = await self.request.json()

        given_fields_correct = validate_fields(self.required_fields, response)

        if not given_fields_correct:
            body = {'description': 'Bad request'}
            return web.json_response(data=body, status=400)

        async with async_session() as session:
            courier_id = response['courier_id']
            order_id = response['order_id']
            complete_time = response['complete_time']

            select_stm = select(Order).where(
                Order.id == int(order_id)
                and Order.performing_courier == int(courier_id))
            result = await session.execute(select_stm)
            order_found = result.first()

            if not order_found:
                body = {'description': 'Bad request'}
                web.json_response(data=body, status=400)

            # if order is found that means data are correct
            upd_stm = update(Order).where(
                Order.id == int(order_id)
                and Order.performing_courier == int(courier_id)).values(
                    complete_time=complete_time)

            await session.execute(upd_stm)
            await session.commit()

        body = {'description': 'OK', 'content': {'order_id': order_id}}
        return web.json_response(data=body, status=400)
Exemplo n.º 3
0
async def rebuild_db_tables():
    print('rebuild db start')
    async with async_session() as session:
        d1 = delete(Courier)
        d2 = delete(Order)
        await session.execute(d1)
        await session.execute(d2)

        await session.commit()
    print('rebuild db start')
Exemplo n.º 4
0
async def test_sess():
    async with async_session() as session:
        courier_id = 2
        courier_select = select(
            Courier.id, Courier.type, Courier.regions, Courier.working_hours,
            Courier.current_taken_weight).where(Courier.id == int(courier_id))

        courier = await session.execute(courier_select)
        courier = courier.first()

        print(courier.type)
Exemplo n.º 5
0
async def test_sql_feature2():
    async with async_session() as session:
        sel_stm = select(Order).where(Order.id == 2)
        res = await session.execute(sel_stm)
        r = res.first()
        print('ARR', r)
        if r:
            print('HAH')

        await session.commit()
    print('pizda')
Exemplo n.º 6
0
    async def patch(self):
        fields_to_modify = await self.request.json()
        courier_id = int(self.request.match_info['courier_id'])

        valid_fields = ['courier_type', 'regions', 'working_hours']

        async with async_session() as session:
            given_invalid_field = False

            for field in fields_to_modify:
                if field not in valid_fields:
                    given_invalid_field = True

            if given_invalid_field:
                return web.json_response(status=400)

            courier_exists = await does_courier_exists(courier_id, session)
            if not courier_exists:
                return web.json_response(status=404)

            courier_update_stm = update(Courier).where(
                    Courier.id == int(courier_id)).values(**fields_to_modify)

            await session.execute(courier_update_stm)

            courier_select_stm = select(Courier.id, Courier.type, Courier.regions,
                                        Courier.working_hours,
                                        Courier.current_taken_weight).where(
                    Courier.id == int(courier_id))

            result = await session.execute(courier_select_stm)
            courier = result.first()

            await self.update_orders_due_to_courier_modification(courier, session)

            modified_courier = {
                "courier_id": courier.id,
                "courier_type": courier.type,
                "regions": courier.regions,
                "working_hours": courier.working_hours
            }

            session.commit()
            return web.json_response(data=modified_courier, status=200)
Exemplo n.º 7
0
    async def post(self):
        response = await self.request.json()
        couriers_list = response['data']
        couriers_list: List[dict]

        invalid_data_in_post = False

        wrong_ids = []
        success_ids = []

        async with async_session() as session:

            for courier in couriers_list:
                given_fields_correct = validate_fields(self.required_fields,
                                                       courier)

                if not given_fields_correct:
                    invalid_data_in_post = True
                    wrong_ids.append({"id": courier["courier_id"]})

                else:
                    courier_id = courier["courier_id"]
                    courier_type = courier["courier_type"]
                    regions = courier["regions"]
                    working_hours = courier["working_hours"]

                    instance = Courier(id=courier_id,
                                       type=courier_type,
                                       regions=regions,
                                       working_hours=working_hours,
                                       current_taken_weight=0)

                    success_ids.append({"id": courier["courier_id"]})
                    session.add(instance)

            if invalid_data_in_post:
                await session.rollback()
                body = {'validation_error': {'couriers': wrong_ids}}
                return web.json_response(data=body, status=400)
            else:
                await session.commit()
                body = {'couriers': success_ids}
                return web.json_response(data=body, status=201)
Exemplo n.º 8
0
    async def post(self):
        response = await self.request.json()
        orders_list = response['data']
        orders_list: List[dict]

        invalid_data_in_post = False

        wrong_ids = []
        success_ids = []

        async with async_session() as session:

            for order in orders_list:
                given_fields_correct = validate_fields(self.required_fields,
                                                       order)

                if not given_fields_correct:
                    invalid_data_in_post = True
                    wrong_ids.append({"id": order["order_id"]})

                else:
                    order_id = order["order_id"]
                    weight = order["weight"]
                    region = order["region"]
                    delivery_hours = order["delivery_hours"]

                    instance = Order(id=order_id,
                                     weight=weight,
                                     region=region,
                                     delivery_hours=delivery_hours)

                    success_ids.append({"id": order["order_id"]})
                    session.add(instance)

            if invalid_data_in_post:
                await session.rollback()
                body = {'validation_error': {'orders': wrong_ids}}
                return web.json_response(data=body, status=400)
            else:
                await session.commit()
                body = {'orders': success_ids}
                return web.json_response(data=body, status=201)