def post(self) -> ({str: str}, HTTPStatus):
        """
        Delete an existing SubTheme.
        :param  name: the name of SubTheme.
        :param id: id of SubTheme.
        :param theme_id: Parent theme id.
        :type  name: str
        :type  id: str
        :type theme_id: str
        :returns: A no content with a http status code of 204, otherwise a JSON of the error details
                  and the appropriate http status code
        """
        if not get_jwt_claims()['admin']:
            return {"error": "administration privileges required"}, HTTPStatus.FORBIDDEN

        # Get arguments
        args = self.reqparser.parse_args()
        subtheme = None
        # does the theme exist?
        if "id" in args:
            subtheme = SubTheme.get_by(id=args["id"])
        elif "name" in args and "theme_id" in args:
            subtheme = SubTheme.get_by(name=args["name"], t_id=args["theme_id"])

        if not subtheme:
            if not ("name" in args and "theme_id" in args):
                return ({"error": "Argument dependency error: deletion by name requires theme_id"},
                        HTTPStatus.BAD_REQUEST)

            # cannot delete a theme that does not exist.
            return {'error': 'Sub-theme does not exists.'}, HTTPStatus.NOT_FOUND

        # delete the theme
        subtheme.delete()
        subtheme.commit()

        return "", HTTPStatus.NO_CONTENT
Esempio n. 2
0
    def post(self) -> ({str: str}, HTTPStatus):
        """
        Update Attributes SubTheme
        :param attribute_id: Attributes identification number
        :param sub_theme_id: SubTheme identification number
        :type attribute_id: str
        :type sub_theme_id: int
        :return: A JSON containing a message, Attribute id, SubTheme id and a HTTPStatus 200 (OK) on success
                otherwise a JSON with a error message and a HTTPStatus 404 (NotFound)
        """
        args = self.reqpaser.parse_args()

        attribute = AttrAlias.get_by_attr_id(args["attribute_id"])
        if not attribute:
            attribute = Attributes.get_by_id(args["attribute_id"])

        if not attribute:
            return {
                "error": "Attribute not found",
                "id": args["attribute_id"]
            }, HTTPStatus.NOT_FOUND

        sub_theme = SubTheme.get_by(id=args["sub_theme_id"])
        if not sub_theme:
            return {
                "error": "SubTheme not found",
                "id": args["sub_theme_id"]
            }, HTTPStatus.NOT_FOUND

        attribute.sub_theme_id(sub_theme.id)
        attribute.save()
        attribute.commit()

        return {
            "message": "Attribute SubTheme updated",
            "attribute_id": args["attribute_id"],
            "sub_theme_id": args["sub_theme_id"]
        }, HTTPStatus.OK
    def post(self) -> ({str: str}, HTTPStatus):
        """
        Rename an existing SubTheme
        :param current_name: the name of the sub theme to rename
        :param new_name: the new name for the sub theme
        :param theme_id: Parent Theme id
        :param id: SubTheme id
        :type  current_name: str
        :type  new_name: str
        :type  theme_id: str
        :type  id: str
        :returns: A JSON of the changes made to the sub theme with a http status code of 200, otherwise
                  a JSON of the error details and the appropriate http status code
        """
        if not get_jwt_claims()['admin']:
            return {
                "error": "administration privileges required"
            }, HTTPStatus.FORBIDDEN

        # Get arguments
        args = self.reqparser.parse_args()

        # Check the current theme name and the new theme name  is not empty, abort if it is empty
        if "theme_id" not in args and "id" not in args:
            return ({
                'error':
                'Argument dependency: theme_id or id required to rename a SubTheme',
                "args": args
            }, HTTPStatus.BAD_REQUEST)

        subtheme = None
        if "id" in args:
            subtheme = SubTheme.get_by(id=args["id"])
        elif "theme_id" in args:
            subtheme = SubTheme.get_by(name=args["current_name"],
                                       t_id=args["theme_id"])

        if not subtheme:
            # cannot rename a subtheme that does not exist.
            return {
                'error': 'Sub-theme does not exists.',
                'args': args
            }, HTTPStatus.NOT_FOUND

        # Does the new name for theme exist?
        if SubTheme.get_by(name=args["new_name"], t_id=subtheme.t_id):
            return {
                'error':
                'Cannot rename sub-theme to {} ; Sub-theme {} already exists.'.
                format(args["new_name"], args["new_name"]),
                'id':
                "",
                'name':
                args["current_name"]
            }, HTTPStatus.BAD_REQUEST

        # rename the new theme
        subtheme.name = args["new_name"]
        subtheme.save()
        subtheme.commit()

        return ({
            "message": "Subtheme renamed",
            "id": subtheme.id,
            "old_name": args["current_name"],
            "new_name": subtheme.name
        }, HTTPStatus.OK)
    def post(self) -> ({str: str}, HTTPStatus):
        """
        Create new SubTheme
        :param  theme:      the name of the parent theme
        :param theme_id:    the identification number of the parent theme
        :param  subtheme:   the name of the sub theme
        :type  theme:       str
        :type  theme_id:    str
        :type  subtheme:    str
        :returns: A JSON of the new SubTheme with a http status code of 200, otherwise a JSON of the error details
                  and the appropriate http status code
        """
        if not get_jwt_claims()['admin']:
            return {
                "error": "administration privileges required"
            }, HTTPStatus.FORBIDDEN

        args = self.reqparser.parse_args()

        if "theme" not in args and "theme_id" not in args:
            return {
                "error": "theme or theme_id required"
            }, HTTPStatus.BAD_REQUEST

        theme = None
        if "theme_id" in args:
            theme = Theme.get_by_id(args["theme_id"])

        elif "theme" in args:
            theme = Theme.get_by_theme(args["theme"])

        if not theme:
            return ({
                "error":
                "Theme not found",
                "Theme":
                args["theme_id"] if args["theme_id"] else args["theme"]
            }, HTTPStatus.NOT_FOUND)

        sub_theme = SubTheme.get_by(name=args["subtheme"], t_id=theme.id)

        # Avoid duplicating sub themes
        if sub_theme:
            return {
                "error": "sub theme already exists",
                "theme_id": theme.id,
                "sub_theme_id": sub_theme.id,
                "Theme": theme.name,
                "subtheme": sub_theme.name
            }, HTTPStatus.BAD_REQUEST

        sub_theme = SubTheme(theme.id, args["subtheme"])
        sub_theme.save()
        sub_theme.commit()
        return {
            "message": "sub theme created",
            "theme_id": theme.id,
            "sub_theme_id": sub_theme.id,
            "Theme": theme.name,
            "subtheme": sub_theme.name
        }, HTTPStatus.OK