aboutsummaryrefslogtreecommitdiff
path: root/jb/views/tasks.py
diff options
context:
space:
mode:
authorMax Nanis2026-02-19 20:11:41 -0500
committerMax Nanis2026-02-19 20:11:41 -0500
commit8b31678c6e44400967d4934cd9f3c6c6ac0da721 (patch)
tree41c5f4479c353a16da1a8b6fa9088abd084ea388 /jb/views/tasks.py
parentf0f96f83c2630e890a2cbcab53f77fd4c37e1684 (diff)
downloadamt-jb-8b31678c6e44400967d4934cd9f3c6c6ac0da721.tar.gz
amt-jb-8b31678c6e44400967d4934cd9f3c6c6ac0da721.zip
Carer dir into project, some initial pytest, part of the flow tasks. License and Readme update
Diffstat (limited to 'jb/views/tasks.py')
-rw-r--r--jb/views/tasks.py70
1 files changed, 70 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