diff options
Diffstat (limited to 'jb/views')
| -rw-r--r-- | jb/views/tasks.py | 70 | ||||
| -rw-r--r-- | jb/views/utils.py | 15 |
2 files changed, 85 insertions, 0 deletions
diff --git a/jb/views/tasks.py b/jb/views/tasks.py new file mode 100644 index 0000000..7176b29 --- /dev/null +++ b/jb/views/tasks.py @@ -0,0 +1,70 @@ +from datetime import datetime, timezone, timedelta + +from fastapi import Request + +from jb.decorators import AM, HM +from jb.flow.maintenance import check_hit_status +from jb.flow.monitoring import emit_assignment_event +from jb.models.assignment import AssignmentStub +from jb.models.definitions import AssignmentStatus + + +def process_request(request: Request) -> None: + """ + A worker has loaded the HIT (work) page and (probably) accepted the HIT. + AMT creates an assignment, tied to this hit and this worker. + Create it in the DB. + """ + amt_assignment_id = request.query_params.get("assignmentId", None) + if amt_assignment_id == "ASSIGNMENT_ID_NOT_AVAILABLE": + raise ValueError("shouldn't happen") + amt_hit_id = request.query_params.get("hitId", None) + amt_worker_id = request.query_params.get("workerId", None) + print(f"process_request: {amt_assignment_id=} {amt_worker_id=} {amt_hit_id=}") + assert amt_worker_id and amt_hit_id and amt_assignment_id + + # Check that the HIT is still valid + hit = HM.get_from_amt_id(amt_hit_id=amt_hit_id) + _ = check_hit_status(amt_hit_id=amt_hit_id, amt_hit_type_id=hit.amt_hit_type_id) + emit_assignment_event( + status=AssignmentStatus.Accepted, + amt_hit_type_id=hit.amt_hit_type_id, + ) + # I think it won't be assignable anymore? idk + # assert hit_status == HitStatus.Assignable, f"hit {amt_hit_id} {hit_status=}. Expected Assignable" + + # I would like to verify in the AMT API that this assignment is valid, but there + # is no way to do that (until the assignment is submitted) + + # # Make an offerwall to create a user account... + # # todo: GSS: Do we really need to do this??? + # client_ip = get_client_ip(request) + # url = f"{settings.fsb_host}{settings.product_id}/offerwall/45b7228a7/" + # _ = requests.get( + # url, + # {"bpuid": amt_worker_id, "ip": client_ip, "n_bins": 1, "format": "json"}, + # ).json() + + # This assignment shouldn't already exist. If it does, just make sure it + # is all the same. + assignment_stub = AM.get_stub_if_exists(amt_assignment_id=amt_assignment_id) + if assignment_stub: + print(f"{assignment_stub=}") + assert assignment_stub.amt_worker_id == amt_worker_id + assert assignment_stub.amt_assignment_id == amt_assignment_id + assert assignment_stub.created_at > ( + datetime.now(tz=timezone.utc) - timedelta(minutes=90) + ) + return None + + assignment_stub = AssignmentStub( + amt_hit_id=amt_hit_id, + amt_worker_id=amt_worker_id, + amt_assignment_id=amt_assignment_id, + status=AssignmentStatus.Accepted, + hit_id=hit.id, + ) + + AM.create_stub(stub=assignment_stub) + + return None diff --git a/jb/views/utils.py b/jb/views/utils.py new file mode 100644 index 0000000..39db5d2 --- /dev/null +++ b/jb/views/utils.py @@ -0,0 +1,15 @@ +from fastapi import Request + + +def get_client_ip(request: Request) -> str: + """ + Using a testclient, the ip returned is 'testclient'. If so, instead, grab + the ip from the headers + """ + ip = request.headers.get("X-Forwarded-For") + if not ip: + ip = request.client.host + elif ip == "testclient" or ip.startswith("10."): + forwarded = request.headers.get("X-Forwarded-For") + ip = forwarded.split(",")[0].strip() if forwarded else request.client.host + return ip |
