def grant_crowd_qualification(self, qualification_name: str, value: int = 1) -> None: """ Grant a qualification by the given name to this worker. Check the local MTurk db to find the matching MTurk qualification to grant, and pass that. If no qualification exists, try to create one. In creating a new qualification, Mephisto resolves the ambiguity over which requester to associate that qualification with by using the FIRST requester of the given account type (either `mturk` or `mturk_sandbox`) """ mturk_qual_details = self.datastore.get_qualification_mapping( qualification_name) if mturk_qual_details is not None: requester = Requester(self.db, mturk_qual_details["requester_id"]) qualification_id = mturk_qual_details["mturk_qualification_id"] else: target_type = ("mturk_sandbox" if qualification_name.endswith("sandbox") else "mturk") requester = self.db.find_requesters(provider_type=target_type)[0] assert isinstance( requester, MTurkRequester ), "find_requesters must return mturk requester for given provider types" qualification_id = requester._create_new_mturk_qualification( qualification_name) assert isinstance( requester, MTurkRequester), "Must be an MTurk requester for MTurk quals" client = self._get_client(requester._requester_name) give_worker_qualification(client, self.get_mturk_worker_id(), qualification_id, value) return None
def direct_soft_block_mturk_workers( db: "MephistoDB", worker_list: List[str], soft_block_qual_name: str, requester_name: Optional[str] = None, ): """ Directly assign the soft blocking MTurk qualification that Mephisto associates with soft_block_qual_name to all of the MTurk worker ids in worker_list. If requester_name is not provided, it will use the most recently registered mturk requester in the database. """ reqs = db.find_requesters(requester_name=requester_name, provider_type="mturk") requester = reqs[-1] mturk_qual_details = requester.datastore.get_qualification_mapping( soft_block_qual_name ) if mturk_qual_details is not None: # Overrule the requester, as this qualification already exists requester = Requester(db, mturk_qual_details["requester_id"]) qualification_id = mturk_qual_details["mturk_qualification_id"] else: qualification_id = requester._create_new_mturk_qualification( soft_block_qual_name ) mturk_client = requester._get_client(requester._requester_name) for idx, worker_id in enumerate(worker_list): if idx % 50 == 0: print(f'Blocked {idx + 1} workers so far.') try: give_worker_qualification(mturk_client, worker_id, qualification_id, value=1) except Exception as e: print(f'Failed to give worker with ID: \"{worker_id}\" qualification with error: {e}. Skipping.')