-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.py
More file actions
626 lines (495 loc) · 21.8 KB
/
handler.py
File metadata and controls
626 lines (495 loc) · 21.8 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
import json
import os
import boto3
import decimal
import requests
from boto3.dynamodb.conditions import Key, Attr
from botocore.exceptions import ClientError
dynamodb = boto3.resource('dynamodb')
projects_table = os.environ['PROJECTS_TABLE']
#=========================================#
#======= Helper Functions ========#
#=========================================#
def create_response(statusCode, message):
"""Returns a given status code."""
return {
'statusCode': statusCode,
'headers': {
# Required for CORS support to work
'Access-Control-Allow-Origin': '*',
# Required for cookies, authorization headers with HTTPS
'Access-Control-Allow-Credentials': 'true',
},
'body': message
}
class DecimalEncoder(json.JSONEncoder):
"""Helper class to convert a DynamoDB item to JSON."""
def default(self, o):
if isinstance(o, set):
return list(o)
if isinstance(o, decimal.Decimal):
if o % 1 != 0:
return float(o)
else:
return int(o)
return super(DecimalEncoder, self).default(o)
def removeProjectFromCalendarEvents(list_of_event_ids):
"""Removes a project from associated reservations in the calendar.
Requests are posted to the calendar table at AWS associated with
the current development stage.
Args:
list_of_event_ids (list): Ids of calendar events we want to modify.
"""
# The development stage is used in some URLs. The production URL for the
# calendar is '...org/calendar...', so check first if the stage is 'prod'.
if os.environ['STAGE'] == 'prod':
stage = 'calendar'
else:
stage = os.environ['STAGE']
calendarURL = f"https://calendar.photonranch.org/{stage}/remove-project-from-events"
requestBody = json.dumps({
"events": list_of_event_ids
})
requests.post(calendarURL, requestBody)
#=========================================#
#======= Core Methods ========#
#=========================================#
def modify_project(project_name: str, created_at: str, project_changes: dict):
"""Modifies the details of an exising project.
Args:
project_name (str): Name of the existing project we want to modify.
created_at (str): UTC ISO datetime of project creation, used to id
the existing project we want to modify.
project_changes (dict): These are the changes we want to apply. The
format of this dict should be the same as if we were adding a new
project.
Returns:
dict: contains the following keys:
is_successful (bool): whether or not the update worked.
description (str): optional text to display to the user.
updated_project (str): the state of the project after the update.
"""
table = dynamodb.Table(projects_table)
old_project = get_project(project_name, created_at)
# If the project specified by project_name and created_at is not found:
if not old_project["project_exists"]:
return {
"is_successful": False,
"description": "The requested project does not exist.",
"updated_project": []
}
# Initialize the dict that will overwrite the existing project in dynamodb.
updated_project = old_project["project"]
# Apply the new project changes we want to make
updated_project["project_constraints"] = project_changes["project_constraints"]
updated_project["project_name"] = project_changes["project_name"]
updated_project["project_note"] = project_changes["project_note"]
updated_project["project_targets"] = project_changes["project_targets"]
updated_project["project_sites"] = project_changes["project_sites"]
updated_project["scheduled_with_events"] = project_changes["scheduled_with_events"]
updated_project["project_priority"] = project_changes["project_priority"]
# A tricky detail is how to keep track of existing project data for
# exposure requests that have been modified.
# Note: this treats identical exposure requests with different image counts
# as different requests. In other words, editing a project by increasing the
# number of images for some exposure will start from scratch, ignoring
# any previously gathered data.
# Initialize a new array to store identifiers for completed project data
updated_project_data = [[] for x in range(len(project_changes["exposures"]))]
updated_remaining_data = [exposure["count"] for exposure in project_changes["exposures"]]
# For each exposure request, try to match it with an existing exposure
# request. If they match, then 'import' the associated data into the
# updated_project_data array.
for new_index, new_exposure in enumerate(project_changes["exposures"]):
for old_index, old_exposure in enumerate(old_project["project"]["exposures"]):
if new_exposure == old_exposure:
updated_project_data[new_index] = old_project["project"]["project_data"][old_index]
updated_remaining_data[new_index] = old_project["project"]["remaining"][old_index]
break
# Finally, add the updated_project_data array to the udpated_project dict.
updated_project["project_data"] = updated_project_data
updated_project["remaining"] = updated_remaining_data
updated_project["exposures"] = project_changes["exposures"]
# Delete the existing project from the table
table.delete_item(
Key={
"project_name": project_name,
"created_at": created_at
},
)
# Add the updated project back
dynamodb_entry = json.loads(json.dumps(updated_project, cls=DecimalEncoder), parse_float=decimal.Decimal)
table_response = table.put_item(Item=dynamodb_entry)
return {
"is_successful": True,
"description": "Project has been updated.",
"updated_project": table_response,
}
def get_project(project_name, created_at):
"""Retrieves details of a specified project from the DynamoDB table.
Args:
project_name (str): Name of the project we want to retrieve.
created_at (str): UTC datetime string of project creation.
Returns:
List of project details, if it exists.
Otherwise, an empty list.
"""
table = dynamodb.Table(projects_table)
response = table.get_item(
Key={
"project_name": project_name,
"created_at": created_at,
}
)
if 'Item' in response:
return {
"project_exists": True,
"project": response['Item']
}
else:
return {
"project_exists": False,
"project": []
}
#=========================================#
#======= Handlers ========#
#=========================================#
def addNewProject(event, context):
"""Adds a new project to the projects DynamoDB database.
Args:
event.body.project_name (str): Name of the project we want to add.
event.body.user_id (str): Auth0 user 'sub'.
event.body.created_at (str): UTC datetime string of project creation.
Returns:
200 status code with project details if successful.
400 status code if project missing required keys.
"""
event_body = json.loads(event.get("body", ""))
table = dynamodb.Table(projects_table)
print("event_body:")
print(event_body)
# Check that all required keys are present.
required_keys = ['project_name', 'user_id', 'created_at']
actual_keys = event_body.keys()
for key in required_keys:
if key not in actual_keys:
msg = f"Error: missing required key {key}"
print(msg)
return create_response(400, msg)
# Convert floats into decimals for dynamodb
dynamodb_entry = json.loads(json.dumps(event_body), parse_float=decimal.Decimal)
table_response = table.put_item(Item=dynamodb_entry)
message = json.dumps({
'table_response': table_response,
'new_project': event_body,
})
return create_response(200, message)
def modify_project_handler(event, context):
"""Handler method to create a response code after modifying a project.
Args:
event.body.project_name (str): Name of the project we want to modify.
event.body.created_at (str): UTC datetime string of project creation.
event.body.project_changes (dict): Project changes to apply.
Returns:
200 status code with modified project details if successful.
400 status code if unsuccessful.
"""
try:
event_body = json.loads(event.get("body", ""))
print(event_body)
project_name = event_body['project_name']
created_at = event_body['created_at']
project_changes = event_body['project_changes']
response = modify_project(project_name, created_at, project_changes)
return create_response(200, json.dumps(response, cls=DecimalEncoder))
# Something else went wrong, return a Bad Request status code.
except Exception as e:
print(f"Exception: {e}")
return create_response(400, json.dumps(e))
def get_project_handler(event, context):
"""Handler method to retrieve the details of a project.
Args:
event.body.project_name (str): Name of the existing project to modify.
event.body.created_at (str): UTC datetime string of project creation.
Returns:
200 status code with project details if successful.
Otherwise, 404 status code if project does not exist.
"""
event_body = json.loads(event.get("body", ""))
print("event_body:")
print(event_body)
project_name = event_body['project_name']
created_at = event_body['created_at']
project = get_project(project_name, created_at)
if project["project_exists"]:
project_json = json.dumps(project["project"], cls=DecimalEncoder)
return create_response(200, project_json)
else:
return create_response(404, "Project not found.")
def getAllProjects(event, context):
"""Retrieves all existing projects and details from the DynamoDB table.
Returns:
200 status code with JSON of all project data.
Example Python code using this endpoint:
import requests
url = "https://projects.photonranch.org/dev/get-all-projects"
all_projects = requests.post(url).json()
"""
table = dynamodb.Table(projects_table)
response = table.scan()
data = response['Items']
while 'LastEvaluatedKey' in response:
response = table.scan(ExclusiveStartKey=response['LastEvaluatedKey'])
data.extend(response['Items'])
return create_response(200, json.dumps(data, cls=DecimalEncoder))
def getUserProjects(event, context):
"""Retrieves the details of all projects created by a specified user.
Args:
event.body.user_id (str): Auth0 user 'sub'.
Returns:
200 status code with JSON of user project details.
400 status code if the required key 'user_id' is missing.
"""
event_body = json.loads(event.get("body", ""))
table = dynamodb.Table(projects_table)
print("event_body:")
print(event_body)
# Check that all required keys are present.
required_keys = ['user_id']
actual_keys = event_body.keys()
for key in required_keys:
if key not in actual_keys:
msg = f"Error: missing required key {key}"
print(msg)
return create_response(400, json.dumps(msg))
response = table.query(
IndexName="userid-createdat-index",
KeyConditionExpression=Key('user_id').eq(event_body['user_id'])
)
print(response)
user_projects = json.dumps(response['Items'], cls=DecimalEncoder)
return create_response(200, user_projects)
def addProjectEvent(event, context):
"""Adds an associated calendar event to a project's list of events.
Projects keep a list of events that they are scheduled with.
This way, if a project is deleted, it can be removed from any
associated events.
We could use a set in DynamoDB to keep track of associated events. But that
gets complicated because JSON does not support sets, so we would need lots
of custom modifier code throughout the pipeline.
Instead we'll keep it simple with a list. Get the list, check if already
contains our event, and add it if not, then update DynamoDB.
Args:
event.body.project_name (str): Name of the project to add events to.
event.body.created_at (str): UTC datetime string of project creation.
event.body.event_id (str): id of the associated calendar event to add.
Returns:
200 status code if calendar event already exists in project's details.
200 status code if successful adding event ids to the project.
"""
request_body = json.loads(event.get("body", ""))
table = dynamodb.Table(projects_table)
print("event_body:")
print(request_body)
project_name = request_body["project_name"]
created_at = request_body["created_at"]
event_id = request_body["event_id"] # ID of the calendar event
response = table.get_item(
Key={
"project_name": project_name,
"created_at": created_at
},
)
events_list = response['Item']['scheduled_with_events']
# Don't add a duplicate
if event_id in events_list:
return create_response(200, 'Event already associated with this project')
# Add the event to the list and then update the project in dynamodb
else:
events_list.append(event_id)
print(f"events_list: {events_list}")
update_response = table.update_item(
Key={
"project_name": project_name,
"created_at": created_at,
},
UpdateExpression="SET scheduled_with_events = :swe",
ExpressionAttributeValues={
":swe": events_list
}
)
return create_response(200, 'Successfully associated event with project.')
def addProjectData(event, context):
"""Updates a project with images taken to track the completion progress.
When an observatory captures and uploads an image requested in a project,
it should use this endpoint to update the project's completion status.
Args:
event.body.project_name (str): Name of the existing project to modify.
event.body.created_at (str): UTC datetime string of project creation.
event.body.exposure_index (int):
Index of the most recently completed exposure.
event.body.base_filename (str):
New filename to add to the project's data.
Returns:
200 status code if project succesfully updates with image data.
Otherwise, 500 status code if project does not unsuccessfully update.
"""
event_body = json.loads(event.get("body", ""))
table = dynamodb.Table(projects_table)
print("event")
print(json.dumps(event))
# Unique project identifier
project_name = event_body["project_name"]
created_at = event_body["created_at"]
# Indices for where to save the new data in project_data
exposure_index = event_body["exposure_index"]
# Data to save
base_filename = event_body["base_filename"]
# First, get the 'project_data' and 'remaining' arrays we want to update.
# 'project_data[exposure_index]' stores filenames of completed exposures.
# 'remaining[exposure_index]' is the number of exposures remaining.
resp1 = table.get_item(
Key={
"project_name": project_name,
"created_at": created_at,
}
)
project_data = resp1["Item"]["project_data"]
remaining = resp1["Item"]["remaining"]
# Next, add our new information
project_data[exposure_index].append(base_filename)
remaining[exposure_index] = int(remaining[exposure_index]) - 1
print("updated values: ")
print(project_data)
print(remaining)
# Finally, update the DynamoDB project entry with
# the revised 'project_data' and 'remaining'
resp2 = table.update_item(
Key={
"project_name": project_name,
"created_at": created_at,
},
UpdateExpression="SET #project_data = :project_data_updated, #remaining = :remaining_updated",
ExpressionAttributeNames={
"#project_data": "project_data",
"#remaining": "remaining",
},
ExpressionAttributeValues={
":project_data_updated": project_data,
":remaining_updated": remaining,
}
)
if resp2["ResponseMetadata"]["HTTPStatusCode"] == 200:
return create_response(200, json.dumps({"message": "success"}))
else:
return create_response(500, json.dumps({"message": "failed to update project in dynamodb"}))
def deleteProject(event, context):
"""Deletes a project from the DynamoDB table.
A user can only delete their own projects. Only admins can delete
the projects of other users, so the user's role must be checked first.
Args:
event.body.project_name (str): name of the project we want to delete.
event.body.created_at (str): UTC datetime string of project creation.
context.requestContext.authorizor.principalID (str):
Auth0 user 'sub' token (eg. 'google-oauth2|xxxxxxxxxxxxx').
context.requestContext.authorizer.userRoles (str):
Global user account type (eg. 'admin') of the requesting user.
Returns:
200 status code with successful projection deletion.
Otherwise, 403 status code if requesting user is unauthorized.
"""
request_body = json.loads(event.get("body", ""))
table = dynamodb.Table(projects_table)
print("event")
print(json.dumps(event))
# Get the user's roles provided by the lambda authorizer
userMakingThisRequest = event["requestContext"]["authorizer"]["principalId"]
print(f"userMakingThisRequest: {userMakingThisRequest}")
userRoles = json.loads(event["requestContext"]["authorizer"]["userRoles"])
print(f"userRoles: {userRoles}")
# Check if the requester is an admin
requesterIsAdmin = "false"
if 'admin' in userRoles:
requesterIsAdmin="true"
print(f"requesterIsAdmin: {requesterIsAdmin}")
# Specify the event with our pk (project_name) and sk (created_at)
project_name = request_body['project_name']
created_at = request_body['created_at']
# Get the project we want to delete so we can remove it from all its
# scheduled calendar events.
event_response = table.get_item(
Key={
"project_name": project_name,
"created_at": created_at
},
)
associated_events = event_response['Item']['scheduled_with_events']
# Don't remove project from calendar events if the user is not authorized
if requesterIsAdmin or userMakingThisRequest==event_response['Item']['user_id']:
print("removing projects from calendar events: ")
print(associated_events)
removeProjectFromCalendarEvents(associated_events)
try:
# Now we can delete the item
response = table.delete_item(
Key={
"project_name": project_name,
"created_at": created_at
},
ConditionExpression=":requesterIsAdmin = :true OR user_id = :requester_id",
ExpressionAttributeValues = {
":requester_id": userMakingThisRequest,
":requesterIsAdmin": requesterIsAdmin,
":true": "true"
}
)
except ClientError as e:
print(f"error deleting project: {e}")
if e.response['Error']['Code'] == "ConditionalCheckFailedException":
print(e.response['Error']['Message'])
return create_response(403, "You may only delete your own projects.")
return create_response(403, e.response['Error']['Message'])
message = json.dumps(response, indent=4, cls=DecimalEncoder)
print(f"success deleting project; message: {message}")
return create_response(200, message)
def deleteSchedulerProjects(event, context):
"""Special delete method: intended only for clearing expired scheduler outputs
Args:
event.body.project_ids (list or str): IDs for all projects to delete.
Each ID is formatted {project_name}#{created_at}.
Returns:
200 status code with successful project deletion and the following:
successful_delete_count: number of projects deleted successfully
failed_delete_count: number of projects that failed to delete
failed_ids: list of IDs for projects that failed to delete
"""
request_body = json.loads(event.get("body", "{}"))
table = dynamodb.Table(projects_table)
ids_to_delete = request_body.get("project_ids", [])
failed_to_delete = []
for id in ids_to_delete:
project_name, created_at = id.split("#", 1)
try:
table.delete_item(
Key={
"project_name": project_name,
"created_at": created_at
},
ConditionExpression="origin = :scheduler_origin",
ExpressionAttributeValues={
":scheduler_origin": "LCO"
}
)
except ClientError as e:
error_code = e.response["Error"]["Code"]
print(f"Error deleting project {id}: {e}")
if error_code == "ConditionalCheckFailedException":
print(f"Failed to delete project {id} because origin is not LCO.")
failed_to_delete.append(id)
response_message = {
"successful_delete_count": len(ids_to_delete) - len(failed_to_delete),
"failed_delete_count": len(failed_to_delete),
"failed_ids": failed_to_delete,
"message": "Delete finished"
}
return create_response(200, json.dumps(response_message))