예제 #1
0
def update_car(license_plate):
    user = User.get(User.username == auth.username())

    try:
        car = Car.find_car_by_plate(user, license_plate)
    except DoesNotExist:
        raise BadApiRequest('Car not found')

    required_fields = ('latitude', 'longitude')
    validate_json_request(request, required_fields=required_fields)

    try:
        location = Location.create_from_string(request.json['latitude'],
                                               request.json['longitude'])
    except ValueError as e:
        raise BadApiRequest(str(e))

    car.update_location(location)

    return json_response(model_to_dict(car, recurse=False))
예제 #2
0
def create_car():
    user = User.get(User.username == auth.username())

    required_fields = ('license_plate', 'latitude', 'longitude')
    validate_json_request(request, required_fields=required_fields)

    try:
        location = Location.create_from_string(request.json['latitude'],
                                               request.json['longitude'])
    except ValueError as e:
        raise BadApiRequest(str(e))

    try:
        car = Car.create_with_user(user, request.json['license_plate'],
                                   location)
    except IntegrityError:
        raise BadApiRequest('Car already exists')
    except ValueError as e:
        raise BadApiRequest(str(e))

    return json_response(model_to_dict(car, recurse=False))
예제 #3
0
 def test_incorrect_longitude(self):
     with pytest.raises(ValueError):
         Location.create_from_string('-32.345234', '-209.134938')
예제 #4
0
 def test_incorrect_format(self):
     with pytest.raises(ValueError):
         Location.create_from_string('Hello', 'World')
예제 #5
0
 def test_create(self):
     loc = Location.create_from_string('19.456233', '-99.109234')
     assert loc.latitude == 19.456233
     assert loc.longitude == -99.109234