async def get(self, id=None, *args, **kwargs) -> Response: if not id: raise OrganizationNotFound organization = Organization.get_item(id) if not organization: raise OrganizationNotFound return await self.to_response(await self.serialize(data=organization))
async def delete(self, id=None, *args, **kwargs) -> Response: if not id: raise OrganizationNotFound organization = Organization.get_item(id) if not organization: raise OrganizationNotFound organization.delete() return JSONAPIResponse(status_code=204)
async def post(self, *args, **kwargs) -> Response: json_body = await self.deserialize_body() user = User() username = json_body.get('username') if username: user.username = username else: raise HTTPException(status_code=400, detail='A valid `username` is required.') organization_id = json_body.get('organization') org = Organization.get_item(int(organization_id)) if not org: raise OrganizationNotFound user.organization = org user.save() result = await self.serialize(data=user) return await self.to_response(result, status_code=201)
async def patch(self, id=None, *args, **kwargs) -> Response: if not id: raise OrganizationNotFound organization = Organization.get_item(id) if not organization: raise OrganizationNotFound json_body = await self.deserialize_body(partial=True) name = json_body.get('name') if name: organization.name = name contact_phone = json_body.get('contact_phone') if contact_phone: organization.contact_phone = contact_phone contact_url = json_body.get('contact_url') if contact_url: organization.contact_url = contact_url organization.save() return await self.to_response(await self.serialize(data=organization))
async def patch(self, id=None, *args, **kwargs) -> Response: if not id: raise UserNotFound user = User.get_item(id) if not user: raise UserNotFound json_body = await self.deserialize_body(partial=True) username = json_body.get('username') if username: user.username = username organization_id = json_body.get('organization') if organization_id: org = Organization.get_item(int(organization_id)) if not org: raise OrganizationNotFound user.organization = org user.save() return await self.to_response(await self.serialize(data=user))