def reserve():
    '''Reserve URI.

    Parameters for GET method
    -------------------------
    id : int
        Car id

    Parameters for POST method
    --------------------------
    carId : int
        Car id.
    reservedTime : str
        Time when the car can be unlocked.
    reservedHours : int
        Hours that the car is reserved for.
    '''

    member = security.verifySession(session)
    if member is None:
        return redirect(url_for(".welcome"))

    error = None
    if request.method == "GET":
        keyTypes = {"id": int}
        params = webdata.parseInput(request.args, keyTypes)
        car = getCar(params["id"])
        if car is None or not car["available"]:
            error = True
    else:  # POST
        keyTypes = {
            "carId": int,
            "reservedTime": "datetime",
            "reservedHours": int
        }
        params = webdata.parseInput(request.form, keyTypes)
        params["memberId"] = member["id"]
        params["reservedTime"] = webdata.convertLocalToUtc(
            params["reservedTime"])
        car = getCar(params["carId"])
        if car is None or not car["available"]:
            error = True
        else:
            resp = rest.post("reservation", data=params)
            if resp is None or resp["error"] is not None:
                error = True
            else:
                reservation = resp["body"]
                reservation["reservedTime"] = webdata.strToDatetime(
                    reservation["reservedTime"])
                #if not current_app.testing:
                #    calendar.insertEvent(session["credentials"], member, car, reservation)
                error = False

    return render_template("reserve.html", error=error, member=member, car=car)
def routeSearchMembers():
    '''RESTful endpoint to members data.

    Parameters for GET method
    -------------------------
    username : str (optional)
        Member's username.
    email : str (optional)
        Member's email.

    Returns
    -------
    dict {
        body : list
            A list of member dicts.
        error : str
            Error message if there's any.
    }
        Predefined API return data structure.
    '''

    # Allow only clients from specific origins
    if not security.legitimateClient(request.remote_addr):
        abort(401, "unauthorised request")

    # Define parameter types
    keyTypes = {"username": str, "email": str}
    # Parse input parameters
    params = webdata.parseInput(request.args, keyTypes)
    resp = {"error": None, "body": searchMembers(params)}

    return jsonify(resp)
def reportCar():
    '''RESTful endpoint to members data.

    Parameters for GET method
    -------------------------
    id : str 
        Car's email.
    email : str 
        Member's email.
    content : str
        Report's content.

    Returns
    -------
    int
        Id of the car just updated.
    '''
    # Allow only clients from specific origins
    if not security.legitimateClient(request.remote_addr):
        abort(401, "unauthorised request")

    # Define parameter types
    keyTypes = {"id": int, "email": str, "content": str}
    # Parse input parameters
    params = webdata.parseInput(request.args, keyTypes)

    resp = {"error": None, "body": createReport(params)}

    return jsonify(resp)
def cancel():
    '''Reserve URI.

    Parameters for GET method
    -------------------------
    id : int
        Reservation id.
    '''

    member = security.verifySession(session)
    if member is None:
        return redirect(url_for(".welcome"))

    keyTypes = {"id": int}
    params = webdata.parseInput(request.args, keyTypes)
    params["status"] = -1
    resp = rest.put("reservation", data=params)
    if resp is None or resp["error"] is not None:
        error = True
    else:
        reservation = resp["body"]
        reservation["reservedTime"] = webdata.strToDatetime(
            reservation["reservedTime"])
        #if not current_app.testing:
        #    calendar.deleteEvent(session["credentials"], member, reservation)
        error = False

    return render_template("cancel.html", error=error, member=member)
def welcome():
    '''Welcome URI.

    Parameters for GET method
    -------------------------
    None

    Parameters for POST method
    --------------------------
    username : str
        Member's username.
    password : str
        Member's password.
    '''

    member = security.verifySession(session)
    if member is not None:

        if member["userType"] == "admin":
            return redirect(url_for(".admin"))

        if member["userType"] == "manager":
            return redirect(url_for(".manager"))

        if member["userType"] == "engineer":
            return redirect(url_for(".engineer"))

        return redirect(url_for(".cars"))

    error = None
    if request.method == "POST":
        keyTypes = {"username": str, "password": str}
        params = webdata.parseInput(request.form, keyTypes)
        member = security.verifyPassword(params["username"],
                                         params["password"])
        if member is None:
            error = True
        else:
            session.clear()
            session["m"] = member["id"]
            error = False

            if member["userType"] == "admin":
                return redirect(url_for(".admin"))

            if member["userType"] == "manager":
                return redirect(url_for(".manager"))

            if member["userType"] == "engineer":
                return redirect(url_for(".engineer"))

            if not current_app.testing:
                return redirect(url_for("oauth.authorize"))

    return render_template("welcome.html", error=error, member=member)
def routeCar():
    '''RESTful endpoint to access car data.

    Parameters for GET method
    -------------------------
    id : int (optional)
        Car's id.
    make : str (optional)
        Car's brand.
    bodyType : str (optional)
        Car's body type.
    colour : str (optional)
        Car's colour.
    seats : int (optional)
        Number of seats.
    location : str (optional)
        Car's current location.
    costPerHour : float (optional)
        Cost per hour.
    available : bool (optional)
        Whether the car is available.

    Returns
    -------
    dict {
        body : list
            A list of car dicts.
        error : str
            Error message if there's any.
    }
        Predefined API return data structure.
    '''

    # Allow only clients from specific origins
    if not security.legitimateClient(request.remote_addr):
        abort(401, "unauthorised request")

    # Define parameter types
    keyTypes = {
        "id": int,
        "make": str,
        "bodyType": str,
        "colour": str,
        "seats": int,
        "location": str,
        "costPerHour": float,
        "available": bool,
        "reported": bool
    }
    # Parse input parameters
    params = webdata.parseInput(request.args, keyTypes)
    resp = {"error": None, "body": getCars(params)}

    return jsonify(resp)
def report():
    '''Report URL.

    Parameters
    ----------
    id : int
        Car id.

    Returns
    -------
    dict
        Car dict collected via RESTful API.
    '''
    error = None
    if request.method == "GET":
        keyTypes = {"id": int}
        params = webdata.parseInput(request.args, keyTypes)
        car = getCar(params["id"])
        if car is None:
            error = True

    if request.method == "POST":

        keyTypes = {
            "id": int,
            "email": str,
            "content": str,
        }
        params = webdata.parseInput(request.form, keyTypes)
        car = getCar(params["id"])
        if car is None:
            error = True
        else:
            resp = rest.get("reportCar", params=params)
            if resp is None or resp["error"] is not None:
                error = True
            else:
                error = False

    return (render_template("report.html", car=car, error=error))
def history():
    '''Reserve URI.

    Parameters for GET method
    -------------------------
    None
    '''

    member = security.verifySession(session)
    if member is None:
        return redirect(url_for(".welcome"))
    else:
        staff = member

    if request.method == "GET":
        keyTypes = {"id": int}
        params = webdata.parseInput(request.args, keyTypes)
        resp = rest.get("member", params=params)
        for m in resp['body']:
            member = m

    params = {"memberId": member["id"]}
    resp = rest.get("reservation", params=params)
    if resp is None or resp["error"] is not None:
        reservations = None
    else:
        reservations = resp["body"]

    if reservations is not None:
        for reservation in reservations:
            params = {"id": reservation["carId"]}
            resp = rest.get("car", params=params)
            if resp is None or resp["error"] is not None:
                reservation["car"] = None
            else:
                reservation["car"] = resp["body"][0]

            reservation["reservedTime"] = webdata.strToDatetime(
                reservation["reservedTime"])
            reservation["reservedTime"] = webdata.convertUtcToLocal(
                reservation["reservedTime"])
            reservation["reservedTimeStr"] = webdata.datetimeToStr(
                reservation["reservedTime"])
            reservation["statusStr"] = webdata.statusToStr(
                reservation["status"])

    return render_template("history.html",
                           staff=staff,
                           member=member,
                           reservations=reservations)
def cars():
    '''Cars URI.

    Parameters for GET method
    -------------------------
    None

    Parameters for POST method
    --------------------------
    make : str (optional)
        Car's brand.
    bodyType : str (optional)
        Car's body type.
    colour : str (optional)
        Car's colour.
    seats : int (optional)
        Number of seats.
    location : str (optional)
        Car's current location.
    costPerHour : float (optional)
        Cost per hour.
    '''

    member = security.verifySession(session)
    if member is None:
        return redirect(url_for(".welcome"))

    params = {}
    if request.method == "POST":
        keyTypes = {
            "make": str,
            "bodyType": str,
            "colour": str,
            "seats": int,
            "location": str,
            "costPerHour": float
        }
        params = webdata.parseInput(request.form, keyTypes)

    params["available"] = True
    resp = rest.get("car", params=params)
    if resp is None or resp["error"] is not None:
        cars = None
    else:
        cars = resp["body"]

    return render_template("cars.html",
                           member=member,
                           params=params,
                           cars=cars)
Example #10
0
def engineer():
    '''Engineer URI.

    Parameters for GET method
    -------------------------
    make : str 
        Car's brand.
    bodyType : str 
        Car's body type.
    colour : str 
        Car's colour.
    seats : int (optional)
        Number of seats.
    location : str 
        Car's current location.
    costPerHour : float 
        Cost per hour.
    reported : bool (True)
        Reported cars
    '''

    member = security.verifySession(session)
    if member is None:
        return redirect(url_for(".welcome"))

    keyTypes = {
        "make": str,
        "bodyType": str,
        "colour": str,
        "seats": int,
        "location": str,
        "costPerHour": float
    }
    params = webdata.parseInput(request.form, keyTypes)

    params["reported"] = True
    resp = rest.get("car", params=params)

    if resp is None or resp["error"] is not None:
        cars = None
    else:
        cars = resp["body"]

    return render_template("engineer.html",
                           member=member,
                           params=params,
                           cars=cars)
def modify():
    '''RESTful endpoint to member.

    Parameters for GET method
    -------------------------
    username : str
            Member's username.
    password : str
        Member's encrypted password.
    firstName : str
        Member's first name.
    lastName : str
        Member's last name.
    email : str
        Member's email address.
    userType : str
        Member's type.

    Returns
    -------
    int
        Id of the member just updated.
    '''
    # Allow only clients from specific origins
    if not security.legitimateClient(request.remote_addr):
        abort(401, "unauthorised request")

    # Define parameter types
    keyTypes = {
        "id": int,
        "username": str,
        "password": "******",
        "firstName": str,
        "lastName": str,
        "email": str,
        "userType": str,
    }
    # Parse input parameters
    params = webdata.parseInput(request.form, keyTypes)

    resp = {
        "error": None,
        "body": modifyUser(params),
    }

    return jsonify(resp)
Example #12
0
def emaps():
    '''Maps URI.
    '''

    member = security.verifySession(session)
    if member is None:
        return redirect(url_for(".welcome"))

    params = {}
    if request.method == "POST":
        keyTypes = {
            "make": str,
            "bodyType": str,
            "colour": str,
            "seats": int,
            "location": str,
            "costPerHour": float
        }
        params = webdata.parseInput(request.form, keyTypes)

    params["reported"] = True
    resp = rest.get("car", params=params)
    if resp is None or resp["error"] is not None:
        cars = None
    else:
        cars = resp["body"]

    if cars is None:
        centre = {"latitude": -37.8744, "longitude": 145.1668}
    else:
        centre = {"latitude": 0.0, "longitude": 0.0}

        for car in cars:
            centre["latitude"] += car["latitude"]
            centre["longitude"] += car["longitude"]

        centre["latitude"] /= len(cars)
        centre["longitude"] /= len(cars)

    return render_template("emaps.html",
                           member=member,
                           params=params,
                           cars=cars,
                           centre=centre)
Example #13
0
def admin():

    member = security.verifySession(session)
    if member is None:
        return redirect(url_for(".welcome"))

    #View history
    resp = rest.get("allMembers")
    if resp is None or resp["error"] is not None:
        members = None
    else:
        members = resp["body"]

    users = members

    #Report car
    resp = rest.get("car")
    if resp is None or resp["error"] is not None:
        cars = None
    else:
        cars = resp["body"]

    #User search
    params = {}
    if request.method == "POST":
        keyTypes = {
            "username": str,
            "email": str,
        }
        params = webdata.parseInput(request.form, keyTypes)

        resp = rest.get("searchMembers", params=params)
        if resp is None or resp["error"] is not None:
            users = None
        else:
            users = resp["body"]

    return (render_template("admin.html",
                            staff=member,
                            members=members,
                            users=users,
                            cars=cars))
Example #14
0
def register():
    '''Register URI.

    Parameters for GET method
    -------------------------
    None

    Parameters for POST method
    --------------------------
    username : str
        Member's username.
    password : str
        Member's password.
    firstName : str
        Member's first name.
    lastName : str
        Member's last name.
    email : str
        Member's email address.
    userType : str
        Member's type.
    '''

    error = None
    if request.method == "POST":
        keyTypes = {
            "username": str,
            "password": str,
            "firstName": str,
            "lastName": str,
            "email": str,
            "userType": str,
        }
        params = webdata.parseInput(request.form, keyTypes)
        resp = rest.post("member", data=params)
        if resp is None or resp["error"] is not None:
            error = True
        else:
            session.clear()
            error = False

    return render_template("register.html", error=error)
Example #15
0
def modify():
    '''Modify Member URI.

    Parameters for GET method
    -------------------------
    id : str
        Member's id.

    Parameters for POST method
    --------------------------
    id : str (optional)
        Member's id.
    username : str
        Member's username.
    password : str
        Member's password.
    firstName : str
        Member's first name.
    lastName : str
        Member's last name.
    email : str
        Member's email address.
    userType : str
        Member's type.
    '''
    error = None
    if request.method == "GET":
        keyTypes = {
            "id": int,
        }
        params = webdata.parseInput(request.args, keyTypes)

        if not params:  #add user
            user = None

        else:  #modify user
            resp = rest.get("member", params=params)
            user = resp['body']
            for i in user:
                user = i

    if request.method == "POST":

        keyTypes = {
            "id": int,
            "username": str,
            "password": str,
            "firstName": str,
            "lastName": str,
            "email": str,
            "userType": str,
        }
        params = webdata.parseInput(request.form, keyTypes)

        if (params['id'] != 0):  #modify user

            resp = rest.post("modifyUser", data=params)
            if resp is None or resp["error"] is not None:
                error = True
            else:
                user = getUser(resp['body'])
                error = False

        else:  #add new user
            keyTypes = {
                "username": str,
                "password": str,
                "firstName": str,
                "lastName": str,
                "email": str,
                "userType": str,
            }
            params = webdata.parseInput(request.form, keyTypes)
            resp = rest.post("member", data=params)
            if resp is None or resp["error"] is not None:
                error = True
            else:
                error = False

            user = None

    return (render_template("modify.html", error=error, user=user))
def routeReservation():
    '''RESTful endpoint to access reservation data.

    Parameters for GET method
    -------------------------
    id : int (optional)
        Reservation id.
    memberId : int (optional)
        Id of member who made the reservation.
    carId : int (optional)
        Id of car that is reserved.
    status : int (optional)
        Status of reservation (-1: canceled, 0: reserved, 1: in-use, 2: returned)

    Returns
    -------
    dict {
        body : list
            A list of reservation dicts.
        error : str
            Error message if there's any.
    }
        Predefined API return data structure.

    Parameters for POST method
    --------------------------
    memberId : int
        Id of member who is going to make the reservation.
    carId : int
        Id of car that is going to be reserved.
    reservedTime : str
        Time when the car can be unlocked.
    reservedHours : int
        Hours that the car is reserved for.

    Returns
    -------
    dict {
        body : dict
            A reservation dict.
        error : str
            Error message if there's any.
    }
        Predefined API return data structure.

    Parameters for PUT method
    -------------------------
    id : int
        Reservation id.
    location : str (optional)
        Car's current location.
    latitude : float (optional)
        Car's current geographic coordinate.
    longitude : float (optional)
        Car's current geographic coordinate.
    status : int
        Car's current status.

    Returns
    -------
    dict {
        body : dict
            A reservation dict.
        error : str
            Error message if there's any.
    }
        Predefined API return data structure.
    '''

    # Allow only clients from specific origins
    if not security.legitimateClient(request.remote_addr):
        abort(401, "unauthorised request")

    if request.method == "GET":
        # Define parameter types
        keyTypes = {"id": int, "memberId": int, "carId": int, "status": int}
        # Parse input parameters
        params = webdata.parseInput(request.args, keyTypes)
        resp = {"error": None, "body": getReservations(params)}
    elif request.method == "POST":
        # Define parameter types
        keyTypes = {
            "memberId": int,
            "carId": int,
            "reservedTime": "datetime",
            "reservedHours": int
        }
        # Parse input parameters
        params = webdata.parseInput(request.form, keyTypes)
        reservationId = createReservation(params)
        if reservationId > 0:
            resp = {
                "error": None,
                "body": getReservations({"id": reservationId})[0]
            }
        else:
            resp = {"error": "Error on creating reservation"}
    else:  # PUT
        # Define parameter types
        keyTypes = {
            "id": int,
            "location": str,
            "latitude": float,
            "longitude": float,
            "status": int
        }
        # Parse input parameters
        params = webdata.parseInput(request.form, keyTypes)
        reservationId = updateReservation(params)
        if reservationId > 0:
            resp = {
                "error": None,
                "body": getReservations({"id": reservationId})[0]
            }
        else:
            resp = {"error": "Error on updating reservation"}

    return jsonify(resp)
def routeMember():
    '''RESTful endpoint to access member data.

    Parameters for GET method
    -------------------------
    id : int
        Member id.
    username : str
        Member's username.

    Returns
    -------
    dict {
        body : list
            A list of member dicts.
        error : str
            Error message if there's any.
    }
        Predefined API return data structure.

    Parameters for POST method
    --------------------------
    username : str
        Member's username.
    password : str
        Member's password.
    firstName : str
        Member's first name.
    lastName : str
        Member's last name.
    email : str
        Member's email.
    userType : str
        Member's type.

    Returns
    -------
    dict {
        body : dict
            A member dict.
        error : str
            Error message if there's any.
    }
        Predefined API return data structure.
    '''

    # Allow only clients from specific origins
    if not security.legitimateClient(request.remote_addr):
        abort(401, "unauthorised request")

    if request.method == "GET":
        # Define parameter types
        keyTypes = {"id": int, "username": str}
        # Parse input parameters
        params = webdata.parseInput(request.args, keyTypes)
        resp = {"error": None, "body": getMembers(params)}
    else:  # POST
        # Define parameter types
        keyTypes = {
            "username": str,
            "password": "******",
            "firstName": str,
            "lastName": str,
            "email": str,
            "userType": str,
        }
        # Parse input parameters
        params = webdata.parseInput(request.form, keyTypes)
        memberId = createMember(params)
        if memberId > 0:
            resp = {"error": None, "body": getMembers({"id": memberId})[0]}
        else:
            resp = {"error": "Error on creating member"}

    return jsonify(resp)