Ejemplo n.º 1
0
def get_driver_profile():
    cur = get_db().cursor()
    token = verify_token(request.headers['Authorization'].split(" ")[1],
                         current_app.config['SECRET_KEY'])
    print(request.method)

    if not token:
        return make_response(
            jsonify({
                "success": 0,
                "message": "Driver doesn't exist"
            }), 404)
    elif token["typ"] != "transporter":
        return make_response(
            jsonify({
                "success":
                0,
                "message":
                "You need to be logged in as a driver to get profile"
            }), 400)

    cur.execute("SELECT * FROM transporter WHERE id = {}".format(token["sub"]))
    response = {"success": 1, "details": cur.fetchone()}
    cur.execute("SELECT * FROM vehicle WHERE transporter_id = {}".format(
        token["sub"]))
    vehicles = cur.fetchall()
    response["vehicles"] = vehicles
    if not vehicles:
        response["vehicles"] = "No vehicles found"
    return make_response(jsonify(response), 200)
Ejemplo n.º 2
0
def upload_image(vehicle_id):
    db_conn = get_db()
    cur = db_conn.cursor()

    #get token and check token validity
    token = verify_token(request.headers['Authorization'].split(" ")[1],
                         current_app.config['SECRET_KEY'])

    if vehicle_id is None:
        return make_response(
            jsonify({
                "success":
                0,
                "message":
                "You must specify the vehicle id to post a  picture of it."
            }), 400)
    elif not token:
        return make_response(
            jsonify({
                "success": 0,
                "message": "Driver doesn't exist"
            }), 404)
    elif token["typ"] != "driver":
        return make_response(
            jsonify({
                "success":
                0,
                "message":
                "You need to be logged in as a driver to register a vehicle"
            }), 400)

    body = request.get_json(force=True)

    if body["image"] == '':
        return make_response(
            jsonify({
                "success": 0,
                "message": "no files uploaded"
            }), 400)
    elif body["image"] and allowed_file(body["filename"]):
        filename = secure_filename(body["filename"])
        path = os.path.join(current_app.config['IMAGE_STORE_PATH'],
                            str(token["sub"]), body["filename"])
        if not os.path.exists(os.path.dirname(path)):
            os.makedirs(os.path.dirname(path))

        with open(path, "wb") as f:
            f.write(b64decode(body["image"]))
            f.close()

        # save the file path to the database
        cur.execute("UPDATE vehicle SET pictures = %s WHERE id = %s",
                    (path, vehicle_id))
        db_conn.commit()
        return make_response(
            jsonify({
                "success": 1,
                "message": "Successfully uploaded image"
            }), 200)
Ejemplo n.º 3
0
def book_vehicle(v_id):
    db_conn = get_db()
    cur = db_conn.cursor()

    # get vehicle id from the route
    v_id = int(v_id)
    if v_id is None:
        return make_response(
            jsonify({
                "success": 0,
                "message": "Specify vehicle to book"
            }), 400)

    token = verify_token(request.headers['Authorization'].split(" ")[1],
                         current_app.config['SECRET_KEY'])

    if not token:
        return make_response(
            jsonify({
                "success": 0,
                "message": "Client doesn't exist"
            }), 404)
    elif token['typ'] != 'user':
        return make_response(
            jsonify({
                "success":
                0,
                "message":
                "You must have logged in with a client account to book a vehicle."
            }), 400)

    # check that vehicle exists
    cur.execute("SELECT * FROM vehicle WHERE id = {}".format(v_id))
    db_conn.commit()

    vehicle = cur.fetchone()
    if vehicle['booked'] != 'no':
        return make_response(
            jsonify({
                "success": 0,
                "message": "The vehicle is not available for booking"
            }), 404)

    cur.execute("UPDATE vehicle SET booked = %s WHERE id = %s",
                (token['sub'], v_id))
    db_conn.commit()

    if cur.rowcount < 0:
        return make_response(
            jsonify({
                "success": 0,
                "message": "Booking Unsuccessful"
            }), 500)

    return make_response(
        jsonify({
            "success": 1,
            "message": "Successfully booked vehicle"
        }), 200)
Ejemplo n.º 4
0
def get_bookings():
    cur = get_db().cursor()

    #get token and check token validity
    token = verify_token(request.headers['Authorization'].split(" ")[1],
                         current_app.config['SECRET_KEY'])

    if not token:
        return make_response(
            jsonify({
                "success": 0,
                "message": "Client doesn't exist"
            }), 404)
    elif token["typ"] != "user":
        return make_response(
            jsonify({
                "success":
                0,
                "message":
                "You need to be logged in as a client to view bookings"
            }), 400)

    fetch_query = "SELECT vehicle.id, vehicle.vehicle_type, vehicle.capacity, vehicle.price, vehicle.number_plate, vehicle.pictures, vehicle.booked, payment.payment_id, payment.amount, payment.receipt_no, payment.client_id, payment.vehicle_id, payment.payment_time FROM vehicle LEFT JOIN payment ON vehicle.id=payment.vehicle_id WHERE vehicle.booked = %s" % token[
        "sub"]

    cur.execute(fetch_query)
    result = cur.fetchall()
    if not result:
        return make_response(
            jsonify({
                'success': 0,
                'message': 'No vehicles found'
            }), 404)
    else:
        return make_response(
            jsonify({
                'success': 1,
                'message': "Vehicles found",
                'vehicles': result
            }), 200)
Ejemplo n.º 5
0
def register_vehicle():
    db_conn = get_db()
    cur = db_conn.cursor()

    body = request.get_json(force=True)

    #get token and check token validity
    token = verify_token(request.headers['Authorization'].split(" ")[1],
                         current_app.config['SECRET_KEY'])

    if not token:
        return make_response(
            jsonify({
                "success": 0,
                "message": "Driver doesn't exist"
            }), 404)
    if token["typ"] != "transporter":
        return make_response(
            jsonify({
                "success":
                0,
                "message":
                "You need to be logged in as a driver to register a vehicle"
            }), 400)

    insert_query = "INSERT INTO vehicle (vehicle_type, capacity, price, number_plate, pictures, transporter_id, booked) VALUES "
    insert_query += "('{}', '{}', '{}', '{}', 'No image', {}, 'no')".format(
        body["type"], body["capacity"], body["price"], body["number_plate"],
        token["sub"])

    cur.execute(insert_query)
    db_conn.commit()
    if cur.rowcount > 0:
        fetch_query = "SELECT id FROM vehicle WHERE number_plate = '%s'" % body[
            "number_plate"]
        cur.execute(fetch_query)
        response = jsonify({"success": 1, "vehicle_id": cur.fetchone()["id"]})
        return make_response(response, 200)
Ejemplo n.º 6
0
def debit():
    payload = verify_token(request.headers['Authorization'].split(' ')[1],
                           current_app.config['SECRET_KEY'])
    if not payload:
        return make_response(
            {
                'status': 0,
                'message': 'Must be logged in to make a payment.'
            }, 404)
    else:
        request_data = request.get_json()
        vehicle = request_data['vehicle_id']
        amount = request_data['amount']
        client = payload['sub']

        db_conn = get_db()
        cur = db_conn.cursor()
        client_phone_query = "SELECT phone FROM user WHERE id = {} LIMIT 1".format(
            client)
        cur.execute(client_phone_query)
        client_phone = cur.fetchone()

        transporter_query = "SELECT transporter_id FROM vehicle WHERE id = {}".format(
            vehicle)
        cur.execute(transporter_query)
        db_conn.commit()
        transporter = cur.fetchone()

        payment_response = make_payment(vehicle, amount, client_phone,
                                        transporter)
        if not payment_response:
            return make_response({
                'status': 1,
                'message': 'success',
                'data': payment_response
            })
        else:
            return make_response({'status': 0, 'message': payment_response})
Ejemplo n.º 7
0
def get_user_payments():
    payload = verify_token(request.headers['Authorization'].split(' ')[1],
                           current_app.config['SECRET_KEY'])
    if not payload:
        return make_response(
            {
                'status': 0,
                'message': 'Must be logged in to check payment history.'
            }, 404)
    else:
        db_conn = get_db()
        cur = db_conn.cursor()

        query = "SELECT * FROM payment WHERE client_id = {}".format(
            payload['sub'])
        cur.execute(query)
        user_payments = cur.fetchall()

        return make_response({
            'status': 1,
            'message': 'success',
            'data': user_payments
        })