-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.py
84 lines (72 loc) · 3.55 KB
/
status.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
#
# This software is Copyright ©️ 2020 The University of Southern California. All Rights Reserved.
# Permission to use, copy, modify, and distribute this software and its documentation for educational, research and non-profit purposes, without fee, and without a written agreement is hereby granted, provided that the above copyright notice and subject to the full license file found in the root of this software deliverable. Permission to make commercial use of this software may be obtained by contacting: USC Stevens Center for Innovation University of Southern California 1150 S. Olive Street, Suite 2300, Los Angeles, CA 90115, USA Email: [email protected]
#
# The full terms of this copyright and license should always be found in the root directory of this software deliverable as "license.txt" and if these terms are not found with this software, please contact the USC Stevens Center for the full license.
#
import json
import boto3
import os
from module.api import user_can_edit_mentor
from module.ttl_cache import TTLCache
from module.utils import load_sentry, create_json_response, require_env
from module.logger import get_logger
from train import get_auth_headers
load_sentry()
log = get_logger("status")
JOBS_TABLE_NAME = require_env("JOBS_TABLE_NAME")
log.info(f"using table {JOBS_TABLE_NAME}")
aws_region = os.environ.get("REGION", "us-east-1")
dynamodb = boto3.resource("dynamodb", region_name=aws_region)
job_table = dynamodb.Table(JOBS_TABLE_NAME)
def get_requesting_user_id(event):
token = json.loads(event["requestContext"]["authorizer"]["token"])
return token["id"]
cached_user_can_edit_mentor = TTLCache()
def get_user_can_edit_mentor(mentor, user_id, auth_headers):
user_can_edit_mentor_result = cached_user_can_edit_mentor.get((mentor, user_id))
if not user_can_edit_mentor_result:
user_can_edit_mentor_result = user_can_edit_mentor(mentor, auth_headers)
cached_user_can_edit_mentor.set((mentor, user_id), user_can_edit_mentor_result)
return user_can_edit_mentor_result
def handler(event, context):
log.debug(json.dumps(event))
status_id = event["pathParameters"]["id"]
auth_headers = get_auth_headers(event)
db_item = job_table.get_item(Key={"id": status_id})
log.debug(db_item)
if "Item" in db_item:
item = db_item["Item"]
user_id = get_requesting_user_id(event)
if not user_id:
return create_json_response(401, {"error": "not authorized, no user id", "message": "not authorized, no user id"}, event)
user_can_edit_mentor_result = get_user_can_edit_mentor(item["mentor"], user_id, auth_headers)
if not user_can_edit_mentor_result:
status = 401
data = {
"error": "not authorized",
"message": "not authorized",
}
else:
status = 200
data = {
"id": item["id"],
"status": item["status"],
"state": item["status"],
"mentor": item["mentor"],
# only added after trainjob runs
**({"updated": item["updated"]} if "updated" in item else {}),
"statusUrl": f"/status/{status_id}",
}
else:
data = {
"error": "not found",
"message": f"{status_id} not found",
}
status = 400
return create_json_response(status, data, event)
# # for local debugging:
# if __name__ == '__main__':
# with open('__events__/status-event.json.dist') as f:
# event = json.loads(f.read())
# handler(event, {})