async def get_school_by_attr(req: Request, resp: Response, *, data): try: result = await schools_blueprint.build_query_set(School, req) if result is None: resp.media = None elif isinstance(result, int): resp.media = result elif isinstance(result, School): SchoolModel.from_orm(result).send_json(resp) else: SchoolModel.list_model([ SchoolModel.from_orm(school) for school in result ]).send_json(resp) except Exception: # pylint: disable=bare-except logger.exception("Unable to perform filter")
async def on_get(self, req: Request, resp: Response, *, user_id: int) -> None: """ Get all rights that are associated to this user via roles. The response contains an array of strings with the distinct names of the rights. """ try: rights = await Right.filter(roles__users__id=1 ).distinct().values("name") resp.media = [right["name"] for right in rights] resp.status_code = 200 except Exception as error: # pylint: disable=broad-except error_response(resp, 500, str(error))
async def on_get(self, req: Request, resp: Response) -> None: what = req.params.get("w", None) if what == "home_routes": routes = {} for role in await Role.all(): routes[role.name] = role.home_route resp.media = {"home_routes": routes} else: resp.status_code = 404 # File not found resp.text = f"I do not understand {what}"
async def on_delete(self, req: Request, resp: Response, *, user_id: int): """ Deletes a user from the database. :param int user_id: The id of the user """ try: user = await UserModel.get(id=user_id) await user.delete() resp.media = self.to_json(req, user) except DoesNotExist: error_response(resp, 404, f"User with id {user_id} does not exist.") except Exception as error: # pylint: disable=W0703 error_response(resp, 500, str(error))
async def on_get(self, req: Request, resp: Response) -> None: """ Get all rights that are associated to this user via roles. The response contains an array of strings with the distinct names of the rights. """ try: resp.media = await get_user_rights(self.current_user) resp.status_code = 200 except Exception as error: # pylint: disable=broad-except logger.exception( "Error occurred requesting rights for current user.%s", self.current_user, exc_info=error, stack_info=True, ) error_response(resp, 500, str(error))