async def on_put(self, req: Request, resp: Response, *, school_id: int) -> None: """ Updates the school """ data = await req.media() # That's not the most elegant version. The two # attributes are write protected, so I pop # the two values from the data dict (if present). data.pop("created_at", None) data.pop("modified_at", None) if not isinstance(data, dict): resp.status_code = 400 # Bad_Request resp.text = "Bad formatted body content" return try: school: models.School = await models.School.get(id=school_id) school.update_from_dict(data) school.modified_at = datetime.utcnow() await school.save() # filter_fields = self.get_filter_fields(req) SchoolModel.from_orm(school).send_json(resp) except DoesNotExist: error_response(resp, 404, f"No school with id {school_id} found.") except Exception as error: # pylint: disable=W0703 logger.exception("Could not update school") error_response(resp, 500, str(error))
async def on_delete(self, req: Request, resp: Response, *, school_id: int): """ Delete a school specified by its id. If the school does not exisi, a 404 status code is send back. :param int school_id: The id of the school """ try: school: models.School = await models.School.get_or_none( id=school_id) if school is None: resp.status_code = 404 resp.text = f"School with id {school_id} does not exist." else: await school.delete() # filter_fields = self.get_filter_fields(req) SchoolModel.from_orm(school).send_json(resp) except DoesNotExist: error_response(resp, 404, f"School with id {school_id} does not exist.") except Exception as error: # pylint: disable=W0703 logger.exception("Could not delete school with id %d", school_id) error_response(resp, 500, str(error))
async def on_get(self, req: Request, resp: Response, user_id: int, space: str) -> None: """ Get a user :param int user_id: The id of the user. """ space_relation = { "student": "student_schools", "headmaster": "principal_schools", "teacher": "teacher_schools", } try: user = None relation = space_relation.get(space, None) if relation is None: resp.status_code = 404 resp.text = "unknown relation type" else: user = await models.User.get_or_none( id=user_id).prefetch_related(relation) SchoolModel.list_model([ SchoolModel.from_orm(school) for school in getattr(user, relation, []) ]).send_json(resp) except Exception as error: # pylint: disable=W0703 error_response(resp, 500, str(error))
async def on_get(self, req: Request, resp: Response, *, school_id: int): """ Get a single school :param int school_id: Th id of the requested school. """ try: school = await models.School.get(id=school_id) SchoolModel.from_orm(school).send_json(resp) except Exception as error: # pylint: disable=W0703 error_response(resp, 500, str(error))
async def get_school_by_name(req: Request, resp: Response, *, school_name): # pylint: disable=unused-variable if req.method == "get": school = await models.School.get_or_none(name=school_name) if school is None: resp.status_code = 404 resp.text = f"School with name {school_name} not found." else: SchoolModel.from_orm(school).send_json(resp) else: resp.status_code = 405 resp.text = "Method not allowed"
async def on_get(self, req: Request, resp: Response): """ Returns all schools """ try: query = self.only(req, School.all()) SchoolModel.list_model([ SchoolModel.from_orm(school) for school in await query ]).send_json(resp) except Exception as error: # pylint: disable=W0703 error_response(resp, 500, str(error))
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")