aboutsummaryrefslogtreecommitdiff
path: root/jb/views/tasks.py
blob: 15857c3bb3ead3f3951ee60938ecb9e4466e96c8 (plain)
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
from datetime import datetime, timezone, timedelta

from fastapi import Request

from jb.decorators import AMTM, 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_if_exists(amt_hit_id=amt_hit_id)
    if not hit:
        raise ValueError(f"Hit {amt_hit_id} not found in DB")

    _ = check_hit_status(
        amtm=AMTM, 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