Example #1
0
def read_warehouse_controller(command):
    try:
        warehouse_id = command.get('data').get('warehouse').get('id')
        if not warehouse_id:
            raise IndexError
    except IndexError:
        return create_response(command, WRONG_REQUEST,
                               {'message': 'No id or data specified'})
    else:
        with session_scope() as session:
            warehouse = session.query(Warehouse).filter_by(
                id=warehouse_id).first()
            if warehouse:
                warehouse = {
                    attr: getattr(warehouse, attr)
                    for attr in warehouse.__dict__ if attr[0] != '_'
                }
                return create_response(command, OK, {
                    'warehouse': warehouse,
                    'message': 'Warehouse read'
                })
            else:
                return create_response(
                    command, NOT_FOUND,
                    {'message': f'Warehouse with id={warehouse_id} not found'})
Example #2
0
def update_load_controller(command):
    data = command.get('data')
    try:
        load_attrs = {
            attr: data.get('load').get(attr)
            for attr in Load.__dict__ if attr[0] != '_'
        }
    except IndexError:
        return create_response(command, WRONG_REQUEST,
                               {'message': 'No id or data specified'})
    else:
        with session_scope() as session:
            load = session.query(Load).filter_by(
                id=load_attrs.get('id')).first()
            if load:
                for k, v in load_attrs.items():
                    if v:
                        setattr(load, k, v)
                return create_response(command, OK,
                                       {'message': 'Load updated'})
            else:
                return create_response(command, NOT_FOUND, {
                    'message':
                    f'Load with id={load_attrs.get("id")} not found'
                })
Example #3
0
def update_route_controller(command):
    data = command.get('data')
    try:
        route_attrs = {
            attr: data.get('route').get(attr)
            for attr in Route.__dict__ if attr[0] != '_'
        }
    except IndexError:
        return create_response(command, WRONG_REQUEST,
                               {'message': 'No id or data specified'})
    else:
        with session_scope() as session:
            route = session.query(Route).filter_by(
                id=route_attrs.get('id')).first()
            if route:
                for k, v in route_attrs.items():
                    if v:
                        setattr(route, k, v)
                return create_response(command, OK,
                                       {'message': 'Route updated'})
            else:
                return create_response(command, NOT_FOUND, {
                    'message':
                    f'Route with id={route_attrs.get("id")} not found'
                })
Example #4
0
def read_routes_of_vehicle_controller(command):
    try:
        vehicle_id = command.get('data').get('vehicle').get('id')
        if not vehicle_id:
            raise IndexError
    except IndexError:
        return create_response(command, WRONG_REQUEST,
                               {'message': 'No id or data specified'})
    else:
        with session_scope() as session:
            vehicle = session.query(Vehicle).filter_by(id=vehicle_id).first()
            if vehicle:
                routes = vehicle.routes
                routes = [{
                    attr: getattr(route, attr)
                    for attr in route.__dict__ if attr[0] != '_'
                } for route in routes]
                return create_response(command, OK, {
                    'routes': routes,
                    'message': 'Routes read'
                })
            else:
                return create_response(
                    command, NOT_FOUND,
                    {'message': f'Vehicle with id={vehicle_id} not found'})
Example #5
0
def handler(command):
    logger.debug(f"Checking command : {command}")
    if common_check_command(command):
        logger.debug(f"Command '{command}' was checked")
        action_name = command.get('action')
        controller = resolve(action_name)
        if controller:
            try:
                logger.debug(
                    f'Controller {action_name} resolved with request: {command}'
                )
                response = controller(command)
            except Exception as e:
                logger.critical(f'Controller {action_name} error: {e}')
                response = create_response(command, SERVER_ERROR,
                                           {'message': 'Internal cargo error'})
        else:
            logger.error(f'Controller {action_name} not found')
            response = create_response(
                command, NOT_FOUND,
                {'message': f'Action with name {action_name} not supported'})
    else:
        logger.error(f'Command is incorrect: {command}')
        response = create_response(command, WRONG_REQUEST,
                                   {'message': 'Command is incorrect'})
    return response
Example #6
0
def update_person_controller(command):
    data = command.get('data')
    try:
        person_attrs = {
            attr: data.get('person').get(attr)
            for attr in Person.__dict__ if attr[0] != '_'
        }
    except IndexError:
        return create_response(command, WRONG_REQUEST,
                               {'message': 'No id or data specified'})
    else:
        with session_scope() as session:
            person = session.query(Person).filter_by(
                id=person_attrs.get('id')).first()
            if person:
                for k, v in person_attrs.items():
                    if v:
                        setattr(person, k, v)
                return create_response(command, OK,
                                       {'message': 'Person updated'})
            else:
                return create_response(
                    command, NOT_FOUND, {
                        'message':
                        f'Person with id={person_attrs.get("id")} not found'
                    })
Example #7
0
def update_city_controller(command):
    data = command.get('data')
    try:
        city_attrs = {
            attr: data.get('city').get(attr)
            for attr in City.__dict__ if attr[0] != '_'
        }
    except IndexError:
        return create_response(command, WRONG_REQUEST,
                               {'message': 'No id or data specified'})
    else:
        with session_scope() as session:
            city = session.query(City).filter_by(
                id=city_attrs.get('id')).first()
            if city:
                for k, v in city_attrs.items():
                    if v:
                        setattr(city, k, v)
                return create_response(command, OK,
                                       {'message': 'City updated'})
            else:
                return create_response(command, NOT_FOUND, {
                    'message':
                    f'City with id={city_attrs.get("id")} not found'
                })
Example #8
0
def create_load_controller(command):
    data = command.get('data')
    with session_scope() as session:
        load = Load()
        for k, v in data.get('load').items():
            if v:
                setattr(load, k, v)
        session.add(load)
    return create_response(command, OK, {'message': 'Load added'})
Example #9
0
def create_route_controller(command):
    data = command.get('data')
    with session_scope() as session:
        route = Route()
        for k, v in data.get('route').items():
            if v:
                setattr(route, k, v)
        session.add(route)
    return create_response(command, OK, {'message': 'Route added'})
Example #10
0
def create_vehicle_controller(command):
    data = command.get('data')
    with session_scope() as session:
        vehicle = Vehicle()
        for k, v in data.get('vehicle').items():
            if v:
                setattr(vehicle, k, v)
        session.add(vehicle)
    return create_response(command, OK, {'message': 'Vehicle added'})
Example #11
0
def create_person_controller(command):
    data = command.get('data')
    with session_scope() as session:
        person = Person()
        for k, v in data.get('person').items():
            if v:
                setattr(person, k, v)
        session.add(person)
    return create_response(command, OK, {'message': 'Person added'})
Example #12
0
def create_warehouse_controller(command):
    data = command.get('data')
    with session_scope() as session:
        warehouse = Warehouse()
        for k, v in data.get('warehouse').items():
            if v:
                setattr(warehouse, k, v)
        session.add(warehouse)
    return create_response(command, OK, {'message': 'Warehouse added'})
Example #13
0
def create_city_controller(command):
    data = command.get('data')
    with session_scope() as session:
        city = City()
        for k, v in data.get('city').items():
            if v:
                setattr(city, k, v)
        session.add(city)
    return create_response(command, OK, {'message': 'City added'})
Example #14
0
def delete_load_controller(command):
    try:
        load_id = command.get('data').get('load').get('id')
        if not load_id:
            raise IndexError
    except IndexError:
        return create_response(command, WRONG_REQUEST,
                               {'message': 'No id or data specified'})
    else:
        with session_scope() as session:
            load = session.query(Load).filter_by(id=load_id).first()
            if load:
                session.delete(load)
                return create_response(command, OK,
                                       {'message': 'Load deleted'})
            else:
                return create_response(
                    command, NOT_FOUND,
                    {'message': f'Load with id={load_id} not found'})
Example #15
0
def delete_vehicle_controller(command):
    try:
        vehicle_id = command.get('data').get('vehicle').get('id')
        if not vehicle_id:
            raise IndexError
    except IndexError:
        return create_response(command, WRONG_REQUEST,
                               {'message': 'No id or data specified'})
    else:
        with session_scope() as session:
            vehicle = session.query(Vehicle).filter_by(id=vehicle_id).first()
            if vehicle:
                session.delete(vehicle)
                return create_response(command, OK,
                                       {'message': 'Vehicle deleted'})
            else:
                return create_response(
                    command, NOT_FOUND,
                    {'message': f'Vehicle with id={vehicle_id} not found'})
Example #16
0
def delete_city_controller(command):
    try:
        city_id = command.get('data').get('city').get('id')
        if not city_id:
            raise IndexError
    except IndexError:
        return create_response(command, WRONG_REQUEST,
                               {'message': 'No id or data specified'})
    else:
        with session_scope() as session:
            city = session.query(City).filter_by(id=city_id).first()
            if city:
                session.delete(city)
                return create_response(command, OK,
                                       {'message': 'City deleted'})
            else:
                return create_response(
                    command, NOT_FOUND,
                    {'message': f'City with id={city_id} not found'})
Example #17
0
def delete_person_controller(command):
    try:
        person_id = command.get('data').get('person').get('id')
        if not person_id:
            raise IndexError
    except IndexError:
        return create_response(command, WRONG_REQUEST,
                               {'message': 'No id or data specified'})
    else:
        with session_scope() as session:
            person = session.query(Person).filter_by(id=person_id).first()
            if person:
                session.delete(person)
                return create_response(command, OK,
                                       {'message': 'Person deleted'})
            else:
                return create_response(
                    command, NOT_FOUND,
                    {'message': f'Person with id={person_id} not found'})
Example #18
0
def delete_route_controller(command):
    try:
        route_id = command.get('data').get('route').get('id')
        if not route_id:
            raise IndexError
    except IndexError:
        return create_response(command, WRONG_REQUEST,
                               {'message': 'No id or data specified'})
    else:
        with session_scope() as session:
            route = session.query(Route).filter_by(id=route_id).first()
            if route:
                session.delete(route)
                return create_response(command, OK,
                                       {'message': 'Route deleted'})
            else:
                return create_response(
                    command, NOT_FOUND,
                    {'message': f'Route with id={route_id} not found'})
Example #19
0
def read_persons_controller(command):
    with session_scope() as session:
        persons = session.query(Person).all()
        persons = [{
            attr: getattr(person, attr)
            for attr in person.__dict__ if attr[0] != '_'
        } for person in persons]
        return create_response(command, OK, {
            'persons': persons,
            'message': 'Persons read'
        })
Example #20
0
def read_cities_controller(command):
    with session_scope() as session:
        cities = session.query(City).all()
        cities = [{
            attr: getattr(city, attr)
            for attr in city.__dict__ if attr[0] != '_'
        } for city in cities]
        return create_response(command, OK, {
            'cities': cities,
            'message': 'Cities read'
        })
Example #21
0
def read_routes_controller(command):
    with session_scope() as session:
        routes = session.query(Route).all()
        routes = [{
            attr: getattr(route, attr)
            for attr in route.__dict__ if attr[0] != '_'
        } for route in routes]
        return create_response(command, OK, {
            'routes': routes,
            'message': 'Routes read'
        })
Example #22
0
def read_vehicles_controller(command):
    with session_scope() as session:
        vehicles = session.query(Vehicle).all()
        vehicles = [{
            attr: getattr(vehicle, attr)
            for attr in vehicle.__dict__ if attr[0] != '_'
        } for vehicle in vehicles]
        return create_response(command, OK, {
            'vehicles': vehicles,
            'message': 'Vehicles read'
        })
Example #23
0
def delete_warehouse_controller(command):
    try:
        warehouse_id = command.get('data').get('warehouse').get('id')
        if not warehouse_id:
            raise IndexError
    except IndexError:
        return create_response(command, WRONG_REQUEST,
                               {'message': 'No id or data specified'})
    else:
        with session_scope() as session:
            warehouse = session.query(Warehouse).filter_by(
                id=warehouse_id).first()
            if warehouse:
                session.delete(warehouse)
                return create_response(command, OK,
                                       {'message': 'Warehouse deleted'})
            else:
                return create_response(
                    command, NOT_FOUND,
                    {'message': f'Warehouse with id={warehouse_id} not found'})
Example #24
0
def read_warehouses_controller(command):
    with session_scope() as session:
        warehouses = session.query(Warehouse).all()
        warehouses = [{
            attr: getattr(warehouse, attr)
            for attr in warehouse.__dict__ if attr[0] != '_'
        } for warehouse in warehouses]
        return create_response(command, OK, {
            'warehouses': warehouses,
            'message': 'Warehouses read'
        })
Example #25
0
def read_loads_controller(command):
    with session_scope() as session:
        loads = session.query(Load).all()
        loads = [{
            attr: getattr(load, attr)
            for attr in load.__dict__ if attr[0] != '_'
        } for load in loads]
        return create_response(command, OK, {
            'loads': loads,
            'message': 'Loads read'
        })
Example #26
0
def read_warehouses_of_city_controller(command):
    city_id = command.get('data').get('city').get('id')
    with session_scope() as session:
        warehouses = session.query(Warehouse).filter_by(city_id=city_id)
        warehouses = [{
            attr: getattr(warehouse, attr)
            for attr in warehouse.__dict__ if attr[0] != '_'
        } for warehouse in warehouses]
        return create_response(command, OK, {
            'warehouses': warehouses,
            'message': 'Warehouses read'
        })
Example #27
0
def read_city_id_by_name_controller(command):
    try:
        city_name = command.get('data').get('city').get('name')
        if not city_name:
            raise IndexError
    except IndexError:
        return create_response(command, WRONG_REQUEST,
                               {'message': 'No id or data specified'})
    else:
        with session_scope() as session:
            city = session.query(City).filter_by(name=city_name).first()
            if city:
                city = {
                    attr: getattr(city, attr)
                    for attr in city.__dict__ if attr[0] != '_'
                }
                return create_response(command, OK, {
                    'city': city,
                    'message': 'City read'
                })
            else:
                return create_response(
                    command, NOT_FOUND,
                    {'message': f'City with name={city_name} not found'})
Example #28
0
def read_route_controller(command):
    try:
        route_id = command.get('data').get('route').get('id')
        if not route_id:
            raise IndexError
    except IndexError:
        return create_response(command, WRONG_REQUEST,
                               {'message': 'No id or data specified'})
    else:
        with session_scope() as session:
            route = session.query(Route).filter_by(id=route_id).first()
            if route:
                route = {
                    attr: getattr(route, attr)
                    for attr in route.__dict__ if attr[0] != '_'
                }
                return create_response(command, OK, {
                    'route': route,
                    'message': 'Route read'
                })
            else:
                return create_response(
                    command, NOT_FOUND,
                    {'message': f'Route with id={route_id} not found'})
Example #29
0
def read_load_controller(command):
    try:
        load_id = command.get('data').get('load').get('id')
        if not load_id:
            raise IndexError
    except IndexError:
        return create_response(command, WRONG_REQUEST,
                               {'message': 'No id or data specified'})
    else:
        with session_scope() as session:
            load = session.query(Load).filter_by(id=load_id).first()
            if load:
                load = {
                    attr: getattr(load, attr)
                    for attr in load.__dict__ if attr[0] != '_'
                }
                return create_response(command, OK, {
                    'load': load,
                    'message': 'Load read'
                })
            else:
                return create_response(
                    command, NOT_FOUND,
                    {'message': f'Load with id={load_id} not found'})
Example #30
0
def read_person_controller(command):
    try:
        person_id = command.get('data').get('person').get('id')
        if not person_id:
            raise IndexError
    except IndexError:
        return create_response(command, WRONG_REQUEST,
                               {'message': 'No id or data specified'})
    else:
        with session_scope() as session:
            person = session.query(Person).filter_by(id=person_id).first()
            if person:
                person = {
                    attr: getattr(person, attr)
                    for attr in person.__dict__ if attr[0] != '_'
                }
                return create_response(command, OK, {
                    'person': person,
                    'message': 'Person read'
                })
            else:
                return create_response(
                    command, NOT_FOUND,
                    {'message': f'Person with id={person_id} not found'})