Ejemplo n.º 1
0
    def driver_update():
        """
        Updates a record based on id supplied
        Endpoint URL: /driver/update
        :return: JSON successful message or exception response
        """
        if request.method == "PUT":
            if request.data is None:
                return jsonify({
                    "status_code": 400,
                    "message": "Invalid request"
                })
            request_data = request.data

            try:
                # Validate id parameter passed
                id = helpers.check_missing('list', request_data, 'id')
                id = helpers.validate_int(id, 'id')

                # Find the object to update
                params = {"id": id}
                driver = Driver.get(params)

                # Return 404 if not found the object to update
                if not driver:
                    return jsonify({
                        "status_code": 404,
                        "message": "Driver not found"
                    })

                # Find and validate any allowed parameters
                if "first_name" in request_data.keys():
                    driver.first_name = helpers.validate_string(
                        request_data['first_name'], 'first_name')
                if "middle_name" in request_data.keys():
                    driver.middle_name = helpers.validate_string(
                        request_data['middle_name'], 'middle_name')
                if "last_name" in request_data.keys():
                    driver.last_name = helpers.validate_string(
                        request_data['last_name'], 'last_name')
                if "dob" in request_data.keys():
                    driver.dob = helpers.validate_dob(request_data["dob"])

                # Save an object and return successful message
                driver.save()
                return jsonify({
                    "status_code": 200,
                    "message": "Driver record was updated"
                })
            except Exception as e:
                return jsonify({
                    "status_code": e.args[0]['status_code'],
                    "message": e.args[0]['message']
                })
Ejemplo n.º 2
0
    def driver_get():
        """
        Gets a record based on parameters supplied to the endpoint. Returns first suitable found object based on params
        Endpoint URL: /driver/get
        :return: JSON of an object or exception status
        """
        if request.method == "GET":
            if request.args is None:
                return jsonify({
                    "status_code": 400,
                    "message": "Invalid request"
                })

            try:
                params = {}  # list of params that we will search by
                # Check if any of the parameters are being passed and then validate them
                if "id" in request.args.keys():
                    params['id'] = helpers.validate_int(
                        request.args.get('id'), 'id')
                if "first_name" in request.args.keys():
                    params["first_name"] = helpers.validate_string(
                        request.args.get('first_name'), 'first_name')
                if "middle_name" in request.args.keys():
                    params["middle_name"] = helpers.validate_string(
                        request.args.get('middle_name'), 'middle_name')
                if "last_name" in request.args.keys():
                    params["last_name"] = helpers.validate_string(
                        request.args.get('last_name'), 'last_name')
                if "dob" in request.args.keys():
                    params["dob"] = helpers.validate_dob(
                        request.args.get('dob'))

                # If no allowed params were passed on - invalidate the request
                if not params:
                    return jsonify({
                        "status_code": 400,
                        "message": "Invalid request"
                    })

                # Get the object based on the given parameters
                driver = Driver.get(params)
                if not driver:
                    return jsonify({
                        "status_code": 404,
                        "message": "Driver not found"
                    })
                return jsonify(driver.serialize())
            except Exception as e:  # Return messages of any exceptions raised during validation
                return jsonify({
                    "status_code": e.args[0]['status_code'],
                    "message": e.args[0]['message']
                })
Ejemplo n.º 3
0
def validate_assigning(assigned_type, assigned_id):
    """
    Checks if we can assign this type to this id

    :param assigned_type:
    :param assigned_id:
    :return: list with type and id if passed validation
    :raises Exception: either type or id is invalid
    """
    from app.models import Branch, Driver

    # validate both as ints
    assigned_id = validate_int(assigned_id, 'assigned_id')
    assigned_type = validate_int(assigned_type, 'assigned_type')

    # only allow 1 and 2 to get through
    allowed_types = [1, 2]
    if assigned_type not in allowed_types:
        raise Exception({"status_code": 400, "message": "Invalid assigned_type"})

    if assigned_type == 1:  # 1 = driver
        # check if driver exists
        params = {"id": assigned_id}
        driver = Driver.get(params)
        if driver:
            assigned_id = driver.id
            return [assigned_type, assigned_id]
        else:
            raise Exception({"status_code": 404, "message": "Driver not found"})

    if assigned_type == 2:  # 2 = branch
        # check if branch exists
        params = {"id": assigned_id}
        branch = Branch.get(params)
        if branch:
            occupancy = branch.get_assigned_cars_count(assigned_id)
            if branch.capacity > occupancy:
                return [assigned_type, assigned_id]
            else:
                raise Exception({"status_code": 400, "message": "Branch has reached its capacity"})
        else:
            raise Exception({"status_code": 404, "message": "Branch not found"})
Ejemplo n.º 4
0
    def driver_delete():
        """
        Deletes a record based on the id
        Endpoint URL: /driver/delete
        :return: JSON successful message or exception response
        """
        if request.method == "DELETE":
            if request.args is None:
                return jsonify({
                    "status_code": 400,
                    "message": "Invalid request"
                })

            try:
                # Validate id parameter passed
                id = helpers.check_missing('args', request, 'id')
                id = helpers.validate_int(id, 'id')

                # Find the object to delete
                params = {"id": id}
                driver = Driver.get(params)

                # Return 404 if not found the object to delete
                if not driver:
                    return jsonify({
                        "status_code": 404,
                        "message": "Driver not found"
                    })

                # Delete object and return successful message
                driver.delete()
                return jsonify({
                    "status_code": 200,
                    "message": "Driver deleted"
                })
            except Exception as e:
                return jsonify({
                    "status_code": e.args[0]['status_code'],
                    "message": e.args[0]['message']
                })