Esempio n. 1
0
def issue(car_id: int):
    """
    Endpoint to add/update/delete issue
    Args:
        car_id (int): car if for this issue (issue in one to one)

    Returns: 403

    """
    car = Car.query.filter_by(id=car_id).first()
    if car is None:
        return abort(404, description='Car not found')

    schema = CarIssueSchema(exclude=['id'])
    try:
        issue = schema.loads(request.get_json())
    except ValidationError as ve:
        return abort(400, description=ve.messages)

    # issue text is removed -> delete issue from db
    if len(issue.issue) == 0 and car.issue:
        CarIssue.query.filter_by(id=car.issue.id).delete()
        handle_db_operation(db.session.commit)
        return jsonify('Issue deleted!'), 200

    if car.issue:
        car.issue.issue = issue.issue
    else:
        db.session.add(issue)
        car.issue = issue

    handle_db_operation(db.session.commit)

    return jsonify('Issue updated'), 200
Esempio n. 2
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
Esempio n. 3
0
def add_booking_admin():
    """
    Add a new booking

    Returns: Json string of booking, or error

    """

    schema = BookingSchema(exclude=['id'])
    try:
        booking = schema.loads(request.get_json())
    except ValidationError as ve:
        return abort(400, description=ve.messages)  # wow generic message
    # check that references to data in db is valid
    person = Person.query.filter_by(id=booking.person_id).first()
    car = Car.query.filter_by(id=booking.car_id).first()
    if None in [person, car]:
        return abort(403, description='Booking references invalid person/car id(s)')

    # Check that no booking with car is currently active
    if Booking.is_car_busy(booking.start_time, booking.end_time, booking.car_id):
        return abort(403, description=f'A booking with car id {booking.car_id}'
                                      f' is already mad in that time period')

    db.session.add(booking)
    handle_db_operation(db.session.commit)
    return schema.jsonify(booking), 201
Esempio n. 4
0
def add_car():
    """
    Endpoint to add car to db
    Returns: car added to db, or 400

    """
    schema = CarSchema(exclude=['id'])
    try:
        new_car = schema.loads(request.get_json())
    except ValidationError as ve:
        return abort(400, description=ve.messages)
    if Car.query.filter_by(reg_number=new_car.reg_number).first() is not None:
        return abort(403, description='Conflict, reg number exists!')
    db.session.add(new_car)
    handle_db_operation(db.session.commit)
    return jsonify(schema.dumps(new_car)), 201
Esempio n. 5
0
def add_person():
    """
    Endpoint to add a person
    Returns: Updated person 201, or invalid data error

    """
    schema = PersonSchema(exclude=['id'])
    try:
        new_person = schema.loads(request.get_json())
    except ValidationError as ve:
        return abort(400, description=ve.messages)
    # if password not on correct hash format, hash it
    if not new_person.password.startswith('pbkdf2'):
        new_person.password = generate_password_hash(new_person.password)
    db.session.add(new_person)
    handle_db_operation(db.session.commit)

    return jsonify(schema.dumps(new_person)), 201
Esempio n. 6
0
def car(id: int):
    """
    Get, update, or delete car
    Args:
        id (int): car id

    Returns: Error or 200

    """
    car = Car.query.get(id)
    if car is None:
        return abort(404, description='Car not found')
    schema = CarSchema()

    if request.method == 'GET':
        # Dump to dict, then add issue text
        car_data = schema.dump(car)
        if car.issue is not None:
            car_data['issue'] = car.issue.issue
        return jsonify(json.dumps(car_data)), 200

    elif request.method == 'PUT':
        try:
            new_car = schema.loads(request.get_json())
        except ValidationError as ve:
            return abort(400, description=ve.messages)

        car.reg_number = new_car.reg_number
        car.car_manufacturer = new_car.car_manufacturer
        car.car_colour = new_car.car_colour
        car.car_type = new_car.car_type
        car.seats = new_car.seats
        car.hour_rate = new_car.hour_rate
        car.longitude = new_car.longitude
        car.latitude = new_car.latitude

        handle_db_operation(db.session.commit)
        return jsonify('Car updated'), 200

    elif request.method == 'DELETE':
        Car.query.filter_by(id=id).delete()
        handle_db_operation(db.session.commit)
        return jsonify('Car deleted'), 200
Esempio n. 7
0
def person(id: int):
    """
    Endpoint to get/update/delete person
    Args:
        id (int): person id

    Returns: Error or 200 if task successful

    """
    person = Person.query.get(id)
    if person is None:
        return abort(404, description='Person not found')
    schema = PersonSchema()
    if request.method == 'GET':
        person_data = schema.dump(person)
        return jsonify(json.dumps(person_data)), 200

    elif request.method == 'PUT':
        try:
            new_person = schema.loads(request.get_json())
        except ValidationError as ve:
            return abort(400, description=ve.messages)
        person.username = new_person.username
        person.first_name = new_person.first_name
        person.last_name = new_person.last_name
        person.email = new_person.email
        person.type = new_person.type
        person.type = new_person.type

        # check if password is plaintext or hashed
        if not new_person.password.startswith('pbkdf2'):
            person.password = generate_password_hash(new_person.password)
        else:
            person.password = new_person.password

        handle_db_operation(db.session.commit)
        return jsonify('Person updated'), 200

    elif request.method == 'DELETE':
        Person.query.filter_by(id=id).delete()
        handle_db_operation(db.session.commit)
        return jsonify('Person deleted'), 200