예제 #1
0
def post_couriers():
    session = db_session.create_session()
    get_data = request.json
    validation_error = []
    ids = []
    for i in get_data['data']:
        if not check_keys(i, ('courier_id', 'courier_type', 'regions', 'working_hours')) or \
                not check_all_keys_in_dict(i, ('courier_id', 'courier_type', 'regions', 'working_hours')):
            validation_error.append({"id": i['courier_id']})
        else:
            ids.append({"id": i['courier_id']})
            regions = []
            for j in i['regions']:
                region = Region(region=j, courier_id=i['courier_id'])
                regions.append(region)
            working_hours = []
            for j in i['working_hours']:
                working_hour = WorkingHour(courier_id=i['courier_id'])
                working_hour.set_working_hour(j)
                working_hours.append(working_hour)
            courier = Courier(courier_id=i['courier_id'],
                              courier_type=i['courier_type'],
                              regions=regions,
                              working_hours=working_hours)
            session.add(courier)
            session.commit()
    if validation_error:
        return make_resp({'validation_error': {
            "couriers": validation_error
        }}, 400)
    else:
        return make_resp({"couriers": ids}, 201)
예제 #2
0
def post_orders():
    session = db_session.create_session()
    data = request.json
    validation_error = []
    ids = []
    for i in data['data']:
        if not check_keys(i, ('order_id', 'weight', 'region', 'delivery_hours')) or \
                not check_all_keys_in_dict(i, ('order_id', 'weight', 'region', 'delivery_hours')):
            validation_error.append({"id": i['order_id']})
        else:
            order = session.query(Order).filter(
                Order.order_id == i['order_id']).first()
            if order:
                session.delete(order)
                session.commit()
            ids.append({"id": i['order_id']})
            delivery_hours = []
            for j in i['delivery_hours']:
                delivery_hour = DeliveryHour(order_id=i['order_id'])
                delivery_hour.set_delivery_hour(j)
                delivery_hours.append(delivery_hour)
            order = Order(order_id=i['order_id'],
                          weight=i['weight'],
                          region=i['region'],
                          delivery_hours=delivery_hours)
            session.add(order)
            session.commit()
    if validation_error:
        return make_resp({'validation_error': {
            "orders": validation_error
        }}, 400)
    else:
        return make_resp({"orders": ids}, 201)
예제 #3
0
def orders_complete():
    session = db_session.create_session()
    get_data = request.json
    date_time = datetime.datetime.strptime(get_data['complete_time'],
                                           '%Y-%m-%dT%H:%M:%S.%fZ')
    if not check_all_keys_in_dict(get_data, ('courier_id', 'order_id', 'complete_time')) or \
            not check_keys(get_data, ('courier_id', 'order_id', 'complete_time')):
        return make_resp('', 400)
    complete_order = session.query(OrderInProgress).filter(
        OrderInProgress.courier_id == get_data['courier_id'],
        OrderInProgress.order_id == get_data['order_id']).first()
    if complete_order:
        complete_order.complete_time = date_time
        complete_order.set_duration(session)
        complete_id = complete_order.order_id
        session.commit()
        return make_resp({"order_id": complete_id}, 200)
    return make_resp('', 400)
예제 #4
0
def patch_courier(id):
    session = db_session.create_session()
    get_data = request.json
    if not check_all_keys_in_dict(
            get_data,
        ('courier_id', 'courier_type', 'regions', 'working_hours')):
        return make_resp('', 400)
    courier = session.query(Courier).filter(Courier.courier_id == id).first()
    if courier:
        if 'courier_type' in get_data.keys():
            session.query(Courier).filter(Courier.courier_id == id).update(
                {'courier_type': get_data['courier_type']})
        if 'regions' in get_data.keys():
            courier.update_regions(get_data['regions'], session)
        if 'working_hours' in get_data.keys():
            courier.update_working_hours(get_data['working_hours'], session)

        courier = session.query(Courier).filter(
            Courier.courier_id == id).first()
        courier_type = courier.courier_type
        regions = [i.region for i in courier.regions]
        working_hours = [i.working_hour for i in courier.working_hours]
        courier_orders = courier.orders
        for i in courier_orders:
            if i.order.region not in regions or not check_time_in_times(
                    courier.working_hours, i.order.delivery_hours[0]):
                i.order.is_taken = False
                session.query(OrderInProgress).filter(
                    OrderInProgress.order_id == i.order_id).delete()
        session.commit()
        return make_resp(
            {
                "courier_id": id,
                "courier_type": courier_type,
                "regions": regions,
                "working_hours": working_hours
            }, 200)
    else:
        return make_resp('', 400)