-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
183 lines (152 loc) · 5.61 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
from chalice import BadRequestError, Chalice, NotFoundError, Response
from marshmallow import ValidationError
from models import Event, Registration, User
from schemas import EventRegistrationSchema, EventSchema, UserSchema
app = Chalice(app_name="events-manager")
@app.route("/users", methods=["POST"])
def create_user():
request = app.current_request
try:
user: User = UserSchema().load(request.json_body)
return Response(
body=UserSchema().dump(user),
status_code=200,
headers={"Content-Type": "text/json"}
)
except ValidationError as err:
raise BadRequestError(err.messages_dict)
@app.route("/users/{pk}", methods=["GET", "DELETE", "PUT"])
def user_detail(pk):
request = app.current_request
full_key: str = User.prepare_key(pk)
try:
user: User = User.get(full_key, full_key)
if request.method == "DELETE":
User.delete_user(user)
elif request.method == "PUT":
user = UserSchema(
context={"instance": user}
).load(request.json_body)
return Response(
body=UserSchema().dump(user),
status_code=200,
headers={"Content-Type": "text/json"}
)
except User.DoesNotExist:
raise NotFoundError("User not found")
except ValidationError as err:
raise BadRequestError(err.messages_dict)
@app.route("/events", methods=["POST"])
def create_event():
request = app.current_request
try:
event: Event = EventSchema().load(request.json_body)
return Response(
body=EventSchema().dump(event),
status_code=200,
headers={"Content-Type": "text/json"}
)
except ValidationError as err:
raise BadRequestError(err.messages_dict)
@app.route("/events/{pk}", methods=["GET", "DELETE", "PUT"])
def event_detail(pk):
request = app.current_request
full_key: str = Event.prepare_key(pk)
try:
event: Event = Event.get(full_key, full_key)
if request.method == "DELETE":
event.delete()
elif request.method == "PUT":
event = EventSchema(
context={"instance": event}
).load(request.json_body)
return Response(
body=EventSchema().dump(event),
status_code=200,
headers={"Content-Type": "text/json"}
)
except Event.DoesNotExist:
raise NotFoundError("Event not found")
except ValidationError as err:
raise BadRequestError(err.messages_dict)
@app.route("/users/{id}/events", methods=["GET"])
def get_events_by_user(id):
full_key = User.prepare_key(id)
events = Event.gsi1.query(full_key, Event.gsi1SK.startswith("EVENT#"))
return Response(
body=EventSchema().dump(events, many=True),
status_code=200,
headers={"Content-Type": "text/json"}
)
@app.route("/events/{id}/registrations", methods=["POST"])
def register_for_event(id):
request = app.current_request
full_key: str = Event.prepare_key(id)
try:
event: Event = Event.get(full_key, full_key)
registration: Registration = EventRegistrationSchema().load(
{**request.json_body, "event": event.ID}
)
return Response(
body=EventRegistrationSchema().dump(registration),
status_code=200,
headers={"Content-Type": "text/json"}
)
except Event.DoesNotExist:
raise NotFoundError("Event not found")
except ValidationError as err:
raise BadRequestError(err.messages_dict)
@app.route("/events/{id}/registrations/{user_id}", methods=["GET", "DELETE"])
def change_registration(id, user_id):
request = app.current_request
try:
registration: Registration = Registration.get(
Event.prepare_key(id), User.prepare_key(user_id)
)
if request.method == "DELETE":
registration.delete()
return Response(
body=EventRegistrationSchema().dump(registration),
status_code=200,
headers={"Content-Type": "text/json"}
)
except Registration.DoesNotExist:
raise NotFoundError("Registration not found")
except ValidationError as err:
raise BadRequestError(err.messages_dict)
@app.route("/events/{id}/registrations", methods=["GET"])
def get_event_registrations(id):
registrations = Registration.query(
Event.prepare_key(id), Registration.SK.startswith("USER#")
)
return Response(
body=EventRegistrationSchema().dump(registrations, many=True),
status_code=200,
headers={"Content-Type": "text/json"}
)
@app.route("/users/{id}/registrations", methods=["GET"])
def get_user_registrations(id):
registrations = Registration.gsi1.query(
User.prepare_key(id), Registration.gsi1SK.startswith("REGISTRATION#")
)
return Response(
body=EventRegistrationSchema().dump(registrations, many=True),
status_code=200,
headers={"Content-Type": "text/json"}
)
@app.route("/events", methods=["GET"])
def get_events_for_city():
query_params = app.current_request.query_params
if not query_params or "city" not in query_params:
raise BadRequestError({"city": "`city` query param must be provided"})
city: str = query_params.get("city")
zip_code: str = query_params.get("zip_code")
if zip_code:
events = Event.gsi2.query(city, Event.gsi2SK == zip_code)
else:
events = Event.gsi2.query(city)
return Response(
body=EventSchema().dump(events, many=True),
status_code=200,
headers={"Content-Type": "text/json"}
)