Ejemplo n.º 1
0
def booking(id: int):
    """
    Get, put, delete booking

    Args:
        id (int): booking id

    Returns: 403 if booking not found, 200 Ok

    """
    booking = Booking.query.get(id)
    if booking is None:
        return abort(403, description='Booking not found')

    if request.method == 'DELETE':
        Booking.query.filter_by(id=id).delete()
        handle_db_operation(db.session.commit)
        return jsonify('Booking deleted'), 200
    elif request.method == 'PUT':
        schema = BookingSchema()
        try:
            new_booking = schema.loads(request.get_json())
        except ValidationError as ve:
            return abort(403, description=ve.messages)

        booking.person_id = new_booking.person_id
        booking.car_id = new_booking.car_id
        booking.start_time = new_booking.start_time
        booking.end_time = new_booking.end_time
        booking.status = new_booking.status
        handle_db_operation(db.session.commit)
        return jsonify('Booking updated successfully'), 200
    else:
        schema = BookingSchema()
        return jsonify(schema.dumps(booking)), 200
Ejemplo n.º 2
0
    def test_schema_load_data_from_json(self):
        with self.app.app_context():
            person_schema = PersonSchema()
            person = person_schema.loads(DummyPerson.creat_random_json(id=None))
            self.assertEqual(type(person), Person)

            booking_schema = BookingSchema()
            # deserialize to json string
            booking_json = booking_schema.dumps(DummyBooking.person1_car1_available)
            # serialize from json string
            booking = booking_schema.loads(DummyBooking.b1_json)
            self.assertEqual(type(booking), Booking)