def fetch_team_for_account(account: Account, team_pk: typing.Optional[int]) -> Team: """ Get Team with pk=team_pk. If team_pk is None a new Team is instantiated and returned (not saved to DB). The function checks if the Team belongs to the passed in Account and if not raises PermissionDenied. It is assumed that the current user already has administrative permissions for the Account and this has been checked. """ if team_pk is not None: team = get_object_or_404(Team, pk=team_pk) if team.account != account: raise PermissionDenied else: team = Team() team.account = account return team
async def patch(self, id=None, *args, **kwargs) -> Response: if not id: raise TeamNotFound team = Team.get_item(id) if not team: raise TeamNotFound json_body = await self.deserialize_body(partial=True) name = json_body.get('name') if name: team.name = name user_ids = json_body.get('users') if user_ids: users = [] for user_id in user_ids: user = User.get_item(int(user_id)) if not user: raise UserNotFound users.append(user) team.users = users team.save() return await self.to_response(await self.serialize(data=team))
async def get(self, id=None, *args, **kwargs) -> Response: if not id: raise TeamNotFound team = Team.get_item(id) if not team: raise TeamNotFound return await self.to_response(await self.serialize(data=team))
async def delete(self, id=None, *args, **kwargs) -> Response: if not id: raise TeamNotFound team = Team.get_item(id) if not team: raise TeamNotFound team.delete() return JSONAPIResponse(status_code=204)
async def delete(self, parent_id: str, *args, **kwargs) -> Response: team = Team.get_item(int(parent_id)) if not team: raise TeamNotFound user_ids = await self.deserialize_ids() if not user_ids: user_ids = [] users = [] for user in team.users: if str(user.id) not in user_ids: users.append(user) team.users = users team.save() return await self.to_response(await self.serialize(data=team))
async def post(self, *args, **kwargs) -> Response: json_body = await self.deserialize_body() name = json_body.get('name') user_ids = json_body['users'] users = [] for user_id in user_ids: try: user = self.db_session.query(User).filter_by( id=int(user_id)).one() except NoResultFound: raise UserNotFound users.append(user) team = Team(name=name) team.users = users self.db_session.add(team) self.db_session.commit() result = await self.serialize(data=team) return await self.to_response(result, status_code=201)
async def post(self, parent_id: str, *args, **kwargs) -> Response: team = Team.get_item(int(parent_id)) if not team: raise TeamNotFound user_ids = await self.deserialize_ids() if not user_ids: users = [] else: users = team.users for user_id in user_ids: user = User.get_item(int(user_id)) if not user: raise UserNotFound users.append(user) team.users = users team.save() return await self.to_response(await self.serialize(data=team))
async def get_related(self, id: Any, relationship: str, related_id: Any = None, *args, **kwargs) -> Response: team = Team.get_item(id) if not team: raise TeamNotFound if relationship == 'users': if not related_id: return await self.to_response(await self.serialize_related( team.users, many=True)) else: filtered_users = list( filter(lambda user: user.id == related_id, team.users)) if len(filtered_users) == 1: return await self.to_response(await self.serialize_related( filtered_users[0])) raise HTTPException(status_code=404)
async def post(self, *args, **kwargs) -> Response: json_body = await self.deserialize_body() team = Team() name = json_body.get('name') if name: team.name = name user_ids = json_body['users'] users = [] for user_id in user_ids: user = User.get_item(int(user_id)) if not user: raise UserNotFound users.append(user) team.users = users team.save() result = await self.serialize(data=team) return await self.to_response(result, status_code=201)
async def get_all(self, *args, **kwargs) -> Response: teams = Team.get_items() return await self.to_response(await self.serialize(data=teams, many=True))
async def get(self, parent_id: str, *args, **kwargs) -> Response: team = Team.get_item(int(parent_id)) if not team: raise TeamNotFound return await self.to_response(await self.serialize(data=team))
def create(self, validated_data): team = Team(**validated_data) team.save() team.users.add(self.context["request"].user) team.save() return team