|
| 1 | +import logging |
| 2 | +from typing import List |
| 3 | + |
| 4 | +from marshmallow_jsonapi import fields |
| 5 | +from starlette.responses import Response |
| 6 | + |
| 7 | +from starlette_jsonapi.fields import JSONAPIRelationship |
| 8 | +from starlette_jsonapi.resource import BaseResource, BaseRelationshipResource |
| 9 | +from starlette_jsonapi.responses import JSONAPIResponse |
| 10 | +from starlette_jsonapi.schema import JSONAPISchema |
| 11 | + |
| 12 | +from accounts.exceptions import TeamNotFound, UserNotFound |
| 13 | +from accounts.models import User, Team |
| 14 | + |
| 15 | +logger = logging.getLogger(__name__) |
| 16 | + |
| 17 | + |
| 18 | +class TeamSchema(JSONAPISchema): |
| 19 | + id = fields.Str(dump_only=True) |
| 20 | + name = fields.Str(required=True) |
| 21 | + |
| 22 | + users = JSONAPIRelationship( |
| 23 | + type_='users', |
| 24 | + schema='UserSchema', |
| 25 | + include_resource_linkage=True, |
| 26 | + many=True, |
| 27 | + required=True, |
| 28 | + ) |
| 29 | + |
| 30 | + class Meta: |
| 31 | + type_ = 'teams' |
| 32 | + self_route = 'teams:get' |
| 33 | + self_route_kwargs = {'id': '<id>'} |
| 34 | + self_route_many = 'teams:get_all' |
| 35 | + |
| 36 | + |
| 37 | +class TeamsResource(BaseResource): |
| 38 | + type_ = 'teams' |
| 39 | + schema = TeamSchema |
| 40 | + id_mask = 'int' |
| 41 | + |
| 42 | + async def prepare_relations(self, obj: Team, relations: List[str]): |
| 43 | + """ |
| 44 | + We override this to allow `included` requests against this resource, |
| 45 | + but we don't actually have to do anything here. |
| 46 | + The attribute is already populated because we're using plain NamedTuples. |
| 47 | + """ |
| 48 | + return None |
| 49 | + |
| 50 | + async def get(self, id=None, *args, **kwargs) -> Response: |
| 51 | + if not id: |
| 52 | + raise TeamNotFound |
| 53 | + team = Team.get_item(id) |
| 54 | + if not team: |
| 55 | + raise TeamNotFound |
| 56 | + |
| 57 | + return await self.serialize(data=team) |
| 58 | + |
| 59 | + async def patch(self, id=None, *args, **kwargs) -> Response: |
| 60 | + if not id: |
| 61 | + raise TeamNotFound |
| 62 | + team = Team.get_item(id) |
| 63 | + if not team: |
| 64 | + raise TeamNotFound |
| 65 | + |
| 66 | + json_body = await self.deserialize_body(partial=True) |
| 67 | + name = json_body.get('name') |
| 68 | + if name: |
| 69 | + team.name = name |
| 70 | + |
| 71 | + user_ids = json_body.get('users') |
| 72 | + if user_ids: |
| 73 | + users = [] |
| 74 | + for user_id in user_ids: |
| 75 | + user = User.get_item(int(user_id)) |
| 76 | + if not user: |
| 77 | + raise UserNotFound |
| 78 | + users.append(user) |
| 79 | + team.users = users |
| 80 | + |
| 81 | + team.save() |
| 82 | + |
| 83 | + return await self.serialize(data=team) |
| 84 | + |
| 85 | + async def delete(self, id=None, *args, **kwargs) -> Response: |
| 86 | + if not id: |
| 87 | + raise TeamNotFound |
| 88 | + team = Team.get_item(id) |
| 89 | + if not team: |
| 90 | + raise TeamNotFound |
| 91 | + |
| 92 | + team.delete() |
| 93 | + |
| 94 | + return JSONAPIResponse(status_code=204) |
| 95 | + |
| 96 | + async def get_all(self, *args, **kwargs) -> Response: |
| 97 | + teams = Team.get_items() |
| 98 | + return await self.serialize(data=teams, many=True) |
| 99 | + |
| 100 | + async def post(self, *args, **kwargs) -> Response: |
| 101 | + json_body = await self.deserialize_body() |
| 102 | + |
| 103 | + team = Team() |
| 104 | + name = json_body.get('name') |
| 105 | + if name: |
| 106 | + team.name = name |
| 107 | + |
| 108 | + user_ids = json_body['users'] |
| 109 | + users = [] |
| 110 | + for user_id in user_ids: |
| 111 | + user = User.get_item(int(user_id)) |
| 112 | + if not user: |
| 113 | + raise UserNotFound |
| 114 | + users.append(user) |
| 115 | + team.users = users |
| 116 | + |
| 117 | + team.save() |
| 118 | + |
| 119 | + result = await self.serialize(data=team) |
| 120 | + result.status_code = 201 |
| 121 | + return result |
| 122 | + |
| 123 | + |
| 124 | +class TeamUsersResource(BaseRelationshipResource): |
| 125 | + parent_resource = TeamsResource |
| 126 | + relationship_name = 'users' |
| 127 | + |
| 128 | + async def get(self, parent_id: str, *args, **kwargs) -> Response: |
| 129 | + team = Team.get_item(int(parent_id)) |
| 130 | + if not team: |
| 131 | + raise TeamNotFound |
| 132 | + return await self.serialize(data=team) |
| 133 | + |
| 134 | + async def patch(self, parent_id: str, *args, **kwargs) -> Response: |
| 135 | + team = Team.get_item(int(parent_id)) |
| 136 | + if not team: |
| 137 | + raise TeamNotFound |
| 138 | + |
| 139 | + user_ids = await self.deserialize_ids() |
| 140 | + if not user_ids: |
| 141 | + users = [] |
| 142 | + else: |
| 143 | + users = [] |
| 144 | + for user_id in user_ids: |
| 145 | + user = User.get_item(int(user_id)) |
| 146 | + if not user: |
| 147 | + raise UserNotFound |
| 148 | + users.append(user) |
| 149 | + team.users = users |
| 150 | + team.save() |
| 151 | + return await self.serialize(data=team) |
| 152 | + |
| 153 | + async def post(self, parent_id: str, *args, **kwargs) -> Response: |
| 154 | + team = Team.get_item(int(parent_id)) |
| 155 | + if not team: |
| 156 | + raise TeamNotFound |
| 157 | + |
| 158 | + user_ids = await self.deserialize_ids() |
| 159 | + if not user_ids: |
| 160 | + users = [] |
| 161 | + else: |
| 162 | + users = team.users |
| 163 | + for user_id in user_ids: |
| 164 | + user = User.get_item(int(user_id)) |
| 165 | + if not user: |
| 166 | + raise UserNotFound |
| 167 | + users.append(user) |
| 168 | + team.users = users |
| 169 | + team.save() |
| 170 | + return await self.serialize(data=team) |
| 171 | + |
| 172 | + async def delete(self, parent_id: str, *args, **kwargs) -> Response: |
| 173 | + team = Team.get_item(int(parent_id)) |
| 174 | + if not team: |
| 175 | + raise TeamNotFound |
| 176 | + user_ids = await self.deserialize_ids() |
| 177 | + if not user_ids: |
| 178 | + user_ids = [] |
| 179 | + users = [] |
| 180 | + for user in team.users: |
| 181 | + if str(user.id) not in user_ids: |
| 182 | + users.append(user) |
| 183 | + team.users = users |
| 184 | + team.save() |
| 185 | + return await self.serialize(data=team) |
0 commit comments