def post(self) -> ({"message": str, "symbol": str, "description": str}, HTTPStatus):
        """
        Deletes a unit of measure in the unit database table by the _type value
        for example symbol = 'kg'. if the the symbol='kg' exists in the table it will be deleted
        Parameters can be passed using a POST request that contains a JSON with the following fields:
                :param symbol: units symbol eg kg for kilograms
                :type symbol: string
        :return: A json response  containing a message, unit type and unit description with an http status code of 204
        """
        if not get_jwt_claims()['admin']:
            return {"message": "Not Authorized."}, HTTPStatus.UNAUTHORIZED

        args = self.reqparser.parse_args()
        # get unit instance from db by symbol
        unit = Unit.get_by_symbol(args["symbol"])

        if not unit:
            # Unit symbol not found
            return {"message": "Unit not found",
                    "symbol": args["symbol"]
                    }, HTTPStatus.NOT_FOUND
        # delete unit from db
        unit.delete()
        unit.commit()

        return {"message": "Unit deleted",
                "symbol": unit.symbol,
                "description": unit.description
                }, HTTPStatus.NO_CONTENT
예제 #2
0
    def get_dummy_unit(self):
        unit = Unit.get_by_symbol("_A_UNIT_FOR_TESTING")
        if not unit:
            unit = Unit("_A_UNIT_FOR_TESTING", "_A_TEST_UNIT_DESCRIPTION_")
            unit.save()
            unit.commit()
        if unit not in self.clean_up:
            self.clean_up.append(unit)

        return unit
    def post(
        self) -> ({
            "message": str,
            "symbol": str,
            "description": str
        }, HTTPStatus):
        """
        Fetches a specified unit instance from the database by its symbol or description
        :post:
        :arg symbol:        Symbol string of the unit eg 'kg'
        :arg description:   The description of the unit
        :type symbol:       str
        :type description:  str

        :return: on success a JSON of the unit and an HTTPStatus code 200 is return otherwise a JSON decription of the
        error and the appropriate HTTPStatus code
        """
        if not get_jwt_claims()['admin']:
            return {"message": "Not Authorized."}, HTTPStatus.UNAUTHORIZED

        # Get arguments passed in POST request
        args = self.reqparser.parse_args()
        # is symbol or discription present?
        if args["symbol"] is None and args["description"] is None:
            # no symbol or description of the unit to find is present.
            return {
                "message": "symbol or description is required.",
                "symbol": "not supplied",
                "description": "not supplied"
            }, HTTPStatus.BAD_REQUEST
        # try fetch the unit from the database
        unit = Unit.get_by_symbol(
            args["symbol"]
        ) if args["symbol"] is not None else Unit.get_by_description(
            args["description"])

        # was the unit found?
        if not unit:
            # the unit with the symbol or description was not found
            return {
                "message":
                "Unit not found.",
                "symbol":
                args["symbol"] if "symbol" in args else "-",
                "description":
                args["description"] if "description" in args else "-"
            }, HTTPStatus.NOT_FOUND

        return unit.json(), HTTPStatus.OK
예제 #4
0
    def create_unit(self, number: int) -> Unit:
        """
        Create unit/s
        :param number: a number to make the unit type unique
        """
        unit = Unit.get_by_symbol("Unit_type_{}".format(number))
        if unit:
            self.units.append(unit)
            return unit

        unit = Unit("Unit_type_{}".format(number),
                    "Description_{}".format(number))
        unit.save()
        unit.commit()
        self.units.append(unit)
        return unit
def create_unit(symbol: str = 'kg', description: str = 'Kilogram') -> None:
    """
    Create Unit
    :param symbol: Units symbol
    :param description: Units description
    :raises ValueError: If the new Unit is not persisted to the dB
    """
    unit = Unit.get_by_symbol(symbol)
    if not unit:
        unit = Unit(symbol, description)
        unit.save()
        unit.commit()

    if not unit:
        logger.critical('ValueError raised while creating Unit')
        raise ValueError
예제 #6
0
    def post(self) -> ({"message": str, "symbol": str, "description": str}, HTTPStatus):
        """
        Fetches a specified unit instance from the database by its symbol and updates the symbol and/or the description
        :post:
        :arg symbol:            Symbol string of the unit eg 'kg'
        :arg symbol:            The new symbol string of the unit eg 'kg'
        :arg new_description:   The new description of the unit
        :type symbol:           str
        :type new_symbol:       str
        :type new_description:  str

        :return: on success a JSON of the unit and an HTTPStatus code is return otherwise
        """
        if not get_jwt_claims()['admin']:
            return {"message": "Not Authorized."}, HTTPStatus.UNAUTHORIZED

        # Get arguments passed in POST request
        args = self.reqparser.parse_args()

        # try fetch the unit from the database
        unit = Unit.get_by_symbol(args["symbol"])

        # was the unit found?
        if not unit:
            # the unit with the symbol or description was not found
            return {"message": "Unit not found.",
                    "symbol": args["symbol"] if "symbol" in args else "-",
                    "description": args["description"] if "description" in args else "-"
                    }, HTTPStatus.NOT_FOUND

        if "new_description" in args:
            unit.description = args["new_description"]

        if "new_symbol" in args:
            unit.symbol = args["new_symbol"]

        unit.save()
        unit.commit()

        return unit.json(), HTTPStatus.OK
    def post(
        self) -> ({
            "message": str,
            "symbol": str,
            "description": str
        }, HTTPStatus):
        """
        Creates a new unit of measure in the unit database table; with a symbol and a description
        for example symbol = 'kg'  description = 'Kilogram
        Parameters can be passed using a POST request that contains a JSON with the following fields:
            :param symbol: units symbol eg kg for kilograms
            :param description: a description of the measurement unit eg.. kilograms
            :type symbol: string
            :type description: string

        :return: A json response  containing a message, unit type and unit description with an http status code of 200
                 otherwise a JSON with the error discription and the appropriate HTTPStatus code
        """
        if not get_jwt_claims()['admin']:
            return {"message": "Not Authorized."}, HTTPStatus.UNAUTHORIZED

        args = self.reqparser.parse_args()
        # does the measurement unit already exist?
        if Unit.get_by_symbol(args["symbol"]):
            # measurement unit already exists
            return {
                "message": "Unit already exists.",
                "symbol": args["symbol"],
                "description": args["description"]
            }, HTTPStatus.BAD_REQUEST
        # Create new measurement unit
        new_unit = Unit(args["symbol"], args["description"])
        new_unit.save()
        new_unit.commit()

        return {
            "message": "New unit created",
            "symbol": args["symbol"],
            "description": args["description"]
        }, HTTPStatus.OK
def test_update_unit(dummy_unit: Unit) -> NoReturn:
    """
    Tests a unit can be updated using the '/admin/units/update' endpoint
    :param dummy_unit: a unit to be used for the
    :param dummy_unit: a dummy unit yielded from a pytest fixture
    :type dummy_unit:  Unit
    """
    global dependencies
    json_payload = {
        "symbol": "NOT_A_SYMBOL",
        "new_symbol": "PEANUT_BUTTER",
        "new_description": "PEANUT_BUTTER"
    }
    response = request('/admin/units/update', json_payload)
    assert response.status_code == 200

    unit = Unit.get_by_symbol("PEANUT_BUTTER")
    if not unit:
        assert False
        return

    assert unit.description == "PEANUT_BUTTER"
    assert unit.symbol == "PEANUT_BUTTER"