示例#1
0
    async def on_get(self, req: Request, resp: Response, *, right_id: int, role_id: int):
        """
        Returns a role that is associated with a given right.

        :param int right_id: The id of the right
        :param int role_id: The id of the role that has to be assiociated with the right.
        """
        try:
            # Prefetching roles for the right specified by its ID.
            # Requesting only role fields specified by the filter
            # criterias from ther request header or all, if no
            # filter was specified.
            # TODO: Maybe reverse the query, as we are looking for a specific
            # role. So start with 'await Role.get(...)'. Tis iliminates the need
            # for an extra search operation (aka find_role)
            right = await Right.get(id=right_id).prefetch_related(
                Prefetch("roles", queryset=self.only(req, Role.filter(id=role_id)))
            )
            role = find_role(right, role_id)

            if role is not None:
                RoleModel.from_orm(role).send_json(resp)
                return

            resp.status_code = 404
            resp.text = f"No role with id '{role_id}' found for right '{right.name} [{right.id}]'."

        except DoesNotExist:
            resp.status_code = 404
            resp.text = f"No right with id '{right_id}' found."

        except Exception as error:  # pylint: disable=broad-except
            error_response(resp, 500, error)
示例#2
0
 async def on_get(self, req: Request, resp: Response) -> None:
     """
     Get all roles
     """
     try:
         query = self.only(models.Role.all())
         RoleModel.list_model([
             RoleModel.from_orm(role) for role in await query
         ]).send_json(resp)
     except Exception as error:  # pylint: disable=W0703
         error_response(resp, 500, str(error))
示例#3
0
async def get_role_by_name(req: Request, resp: Response, *, data):
    # pylint: disable=unused-variable
    if req.method == "get":
        role = await models.Role.get_or_none(name=data)
        if role is None:
            resp.status_code = 404
            resp.text = f"Role with name {data} not found."
        else:
            RoleModel.from_orm(role).send_json(resp)
    else:
        resp.status_code = 405
        resp.text = "Method not allowed"
示例#4
0
    async def on_get(self, req: Request, resp: Response, *, user_id: int):
        """
        Get the roles of e certain user
        """
        try:
            user = await User.get(id=user_id).prefetch_related("roles")
            # filter_fields = self.get_filter_fields(req)
            RoleModel.list_model([
                RoleModel.from_orm(role) for role in user.roles
            ]).send_json(resp)
        except DoesNotExist:
            error_response(resp, 404, "User not found")

        except Exception as error:  # pylint: disable=W0703
            error_response(resp, 500, str(error))
示例#5
0
    async def on_delete(self, req: Request, resp: Response, *, role_id: int):
        """
        Delete a role specified by its id.

        :param int role_id: The id of the role
        """
        try:
            role = await models.Role.get(id=role_id)
            await role.delete()
            # filter_fields = self.get_filter_fields(req)
            RoleModel.from_orm(role).send_json(resp)
        except DoesNotExist:
            error_response(resp, 404,
                           f"Role with id {role_id} does not exist.")

        except Exception as error:  # pylint: disable=W0703
            error_response(resp, 500, str(error))
示例#6
0
    async def on_get(self, req: Request, resp: Response, *, right_id: int):
        """
        Get all roles, that belong to the specified id. If no right
        with the given id can be found, a 404 status is send back.

        :param int right_id: The id of the right.
        """
        try:
            right = await Right.get(id=right_id).prefetch_related("roles")
            # filter_fields = self.get_filter_fields(req)
            RoleModel.list_model([
                RoleModel.from_orm(role) for role in right.roles
            ]).send_json(resp)
            return
        except DoesNotExist:
            error_response(resp, 404, f"Right with id {right_id} not found.")

        except Exception as error:  # pylint: disable=W0703
            error_response(resp, 500, str(error))
示例#7
0
    async def on_get(self, req: Request, resp: Response, *, role_id: int):
        """
        Get the role specified by its id.

        If no role can be found with the given id, a 404 status
        code is send back.

        .. code-block:: html

            GET /roles/<role_id>


        """
        try:
            logger.debug("GET /roles/%s/", role_id)
            role = await models.Role.get(id=role_id)
            RoleModel.from_orm(role).send_json(resp)

        except Exception as error:  # pylint: disable=W0703
            error_response(resp, 500, str(error))
示例#8
0
    async def on_get(self, req: Request, resp: Response, *, user_id: int,
                     role_id: int):
        """
        Get a role, that is associated to a certain user.

        The requested user is specified by the id. If no user is found a status of 404 is
        send back.

        :param int user_id: The id of the user
        :param int role_id: the id of the role
        """
        try:
            role = await Role.get(id=role_id, users__id=user_id)
            # filter_fields = self.get_filter_fields(req)
            RoleModel.from_orm(role).send_json(resp)
        except DoesNotExist:
            resp.status_code = 404

        except Exception as error:  # pylint: disable=W0703
            error_response(resp, 500, str(error))
示例#9
0
    async def on_put(self, req: Request, resp: Response, *,
                     role_id: int) -> None:
        """
        Updates the role
        """
        data = await req.media()
        if not isinstance(data, dict):
            resp.status_code = 400  # Bad_Request
            resp.text = "Bad formatted body content"
            return

        try:
            role = await models.Role.get(id=role_id)
            role.update(data)
            await role.save()
            # filter_fields = self.get_filter_fields(req)
            RoleModel.from_orm(role).send_json(resp)
            resp.status_code = 200

        except DoesNotExist:
            error_response(resp, 404, f"No role with id {role_id} found.")

        except Exception as error:  # pylint: disable=W0703
            error_response(resp, 500, str(error))