def compensate_worker(self, worker_id, email, dollars, notify=True): """Pay a worker by means of a special HIT that only they can see. """ qualification = self.mturkservice.create_qualification_type( name="Dallinger Compensation Qualification - {}".format( generate_random_id()), description=( "You have received a qualification to allow you to complete a " "compensation HIT from Dallinger for ${}.".format(dollars)), ) qid = qualification["id"] self.mturkservice.assign_qualification(qid, worker_id, 1, notify=notify) hit_request = { "experiment_id": "(compensation only)", "max_assignments": 1, "title": "Dallinger Compensation HIT", "description": "For compenation only; no task required.", "keywords": [], "reward": float(dollars), "duration_hours": 1, "lifetime_days": 3, "question": MTurkQuestions.compensation(sandbox=self.is_sandbox), "qualifications": [MTurkQualificationRequirements.must_have(qid)], "do_subscribe": False, } hit_info = self.mturkservice.create_hit(**hit_request) if email is not None: message = { "subject": "Dallinger Compensation HIT", "sender": self.config.get("dallinger_email_address"), "recipients": [email], "body": ("A special compenstation HIT is available for you to complete on MTurk.\n\n" "Title: {title}\n" "Reward: ${reward:.2f}\n" "URL: {worker_url}").format(**hit_info), } self.mailer.send(**message) else: message = {} return { "hit": hit_info, "qualification": qualification, "email": message }
def test_create_hit_lifecycle(self, with_cleanup, qtype, worker_id): result = with_cleanup.get_qualification_type_by_name(qtype["name"]) assert qtype == result with_cleanup.assign_qualification(qtype["id"], worker_id, score=2) workers = with_cleanup.get_workers_with_qualification(qtype["id"]) assert worker_id in [w["id"] for w in workers] result = with_cleanup.increment_qualification_score(qtype["name"], worker_id) assert result["score"] == 3 qualifications = (MTurkQualificationRequirements.must_have(qtype["id"]),) config = standard_hit_config( max_assignments=2, annotation="test-annotation", qualifications=qualifications, ) hit = with_cleanup.create_hit(**config) assert hit["status"] == "Assignable" assert hit["max_assignments"] == 2 assert hit["annotation"] == "test-annotation" # There is a lag before extension is possible sleep_secs = 2 max_wait = 60 time.sleep(sleep_secs) start = time.time() updated = None while not updated and time.time() - start < max_wait: try: updated = with_cleanup.extend_hit( hit["id"], number=1, duration_hours=0.25 ) except MTurkServiceException: time.sleep(sleep_secs) if updated is None: pytest.fail("HIT was never updated") else: assert updated["max_assignments"] == 3 assert with_cleanup.disable_hit( hit_id=hit["id"], experiment_id=config["experiment_id"] )