Esempio n. 1
0
def update_auto(request: Request):
    car_id = request.matchdict.get('car_id')
    car = Repository.car_by_id(car_id)
    if car_id == '__first__':
        car_id = Repository.all_cars()[0].id

    if not car:
        msg = "The car with id '{}' was not found.".format(car_id)
        return Response(status=404, json_body={'error': msg})

    try:
        car_data = request.json_body
    except:
        return Response(status=400, body='Could not parse your post as JSON.')

    vm = UpdateAutoViewModel(car_data, car_id)
    vm.compute_details()
    if vm.errors:
        return Response(status=400, body=vm.error_msg)

    try:
        Repository.update_car(vm.car)
        return Response(status=204, body='Car updated successfully.')
    except:
        return Response(status=400, body='Could not update car.')
Esempio n. 2
0
def single_auto(request: Request):
    car_id = request.matchdict.get('car_id')
    if car_id == '__first__':
        car_id = Repository.all_cars()[0].id

    car = Repository.car_by_id(car_id)
    if not car:
        msg = "The car with id '{}' was not found.".format(car_id)
        return Response(status=404, json_body={'error': msg})

    return car
Esempio n. 3
0
def single_auto(request: Request):
    car_id = request.matchdict.get('car_id')
    log.trace("Car details for car {} and user {}".format(
        car_id, request.api_user.name))
    if car_id == '__first__':
        car_id = Repository.all_cars()[0].id

    car = Repository.car_by_id(car_id)
    if not car:
        msg = "The car with id '{}' was not found.".format(car_id)
        return Response(status=404, json_body={'error': msg})

    return car
Esempio n. 4
0
def update_auto(request: Request):
    car_id = request.matchdict.get('car_id')
    car = Repository.car_by_id(car_id)
    if car_id == '__first__':
        car_id = Repository.all_cars()[0].id

    if not car:
        msg = "The car with id '{}' was not found.".format(car_id)
        return Response(status=404, json_body={'error': msg})

    try:
        car_data = request.json_body
        car = Car.from_dict(car_data)
    except:
        return Response(status=400, body='Could not parse your post as JSON.')

    # TODO: Validate

    try:
        Repository.update_car(car)
        return Response(status=204, body='Car updated successfully.')
    except:
        return Response(status=400, body='Could not update car.')
def all_autos(_):
    cars = Repository.all_cars(limit=25)
    return cars
Esempio n. 6
0
def all_autos_csv(_):
    cars = Repository.all_cars(limit=25)
    # return [car.to_dict() for car in cars]
    # return [(1, 1), (2, 1)]
    return cars
Esempio n. 7
0
def all_autos(request):
    log.trace("Listing cars for {}".format(request.api_user.name))
    cars = Repository.all_cars(limit=25)
    return cars