Esempio n. 1
0
async def update_couriers(request):
    json = await request.json()
    id = request.match_info.get('id')
    incorrect = False
    courier = Courier(id, None, None, None, 0)
    for key, val in json.items():
        if key == "courier_type" and type(val) is str and val in ('foot',
                                                                  'bike',
                                                                  'car'):
            courier.type = val
        elif key == "regions" and type(val) is list and all(
                type(x) is int and x > 0 for x in val):
            courier.regions = val
        elif key == "working_hours" and type(val) is list and all(
                type(x) is str for x in val) and len(val) <= 3:
            courier.working_hours = val
        else:
            incorrect = True
    if incorrect:
        return web.Response(status=400)
    courier = courier.update(request.app['db'])
    orders = courier.get_incompatible_orders(request.app['db'])
    for order in orders:
        order.remove_courier(request.app['db'])
    return web.json_response(status=200,
                             data={
                                 "courier_id": courier.id,
                                 "courier_type": courier.type,
                                 "regions": courier.regions,
                                 "working_hours": courier.working_hours
                             })
Esempio n. 2
0
async def couriers(request):
    json = await request.json()
    objects = []
    incorrect_objects = []
    for k, v in json.items():
        if k == "data":
            for obj in v:
                courier = Courier(obj.get("courier_id"),
                                  obj.get("courier_type"), obj.get("regions"),
                                  obj.get("working_hours"), 0)
                incorrect = False
                for key, val in obj.items():
                    if key == "courier_id" and type(val) is int and val > 0:
                        courier.id = val
                    elif key == "courier_type" and type(
                            val) is str and val in ('foot', 'bike', 'car'):
                        courier.type = val
                    elif key == "regions" and type(val) is list and all(
                            type(x) is int and x > 0 for x in val):
                        courier.regions = val
                    elif key == "working_hours" and type(val) is list and all(
                            type(x) is str for x in val) and len(val) <= 3:
                        courier.working_hours = val
                    else:
                        incorrect = True
                if incorrect or courier.regions is None or courier.working_hours is None or courier.type is None:
                    incorrect_objects.append({"id": courier.id})
                else:
                    objects.append({"id": courier.id})
                    courier.save(request.app['db'])
        else:
            return web.Response(status=400)

    if len(incorrect_objects) > 0:
        return web.json_response(
            status=400,
            data={"validation_error": {
                "couriers": incorrect_objects
            }})
    else:
        return web.json_response(status=201, data={"couriers": objects})