def update_assignments(self, use_sandbox):
        self.stdout.write('{}'.format(use_sandbox))
        mturk_api = Manager_Projects.get_mturk_api(use_sandbox)
        print(mturk_api)

        count = 0
        for index, assignment_db in enumerate(
                Assignment.objects.filter(
                    hit__batch__use_sandbox=use_sandbox).filter(
                        Q(datetime_submit=None) | Q(datetime_accept=None))):
            try:
                assignment_mturk = mturk_api.get_assignment(
                    AssignmentId=assignment_db.id_assignment)['Assignment']
            except:
                continue
            assignment_db.datetime_accept = assignment_mturk['AcceptTime']
            assignment_db.datetime_submit = assignment_mturk['SubmitTime']

            status = assignment_mturk['AssignmentStatus']
            if status == 'Approved':
                assignment_db.status_external = assignments.STATUS_EXTERNAL.APPROVED
            elif status == 'Rejected':
                assignment_db.status_external = assignments.STATUS_EXTERNAL.REJECTED

            assignment_db.save()
            count += 1
            if index % 100 == 0:
                self.stdout.write('{}'.format(index))

        return count
示例#2
0
    def get_balance(use_sandbox: bool) -> dict:
        client = Manager_Projects.get_mturk_api(use_sandbox)
        try:
            balance = float(client.get_account_balance()['AvailableBalance'])
        except EndpointConnectionError:
            balance = None

        return {'balance': balance}
    def handle(self, *args, **options):
        client = Manager_Projects.get_mturk_api(False)

        for assignment in options['assignments']:
            result = client.approve_assignment(AssignmentId=assignment,
                                               OverrideRejection=True)

            self.stdout.write(self.style.SUCCESS(result))
示例#4
0
def create_batch(self, data, database_object_project=None, use_sandbox=True):
    from api.classes import Manager_Projects, ManagerTasks, Manager_Batches, Manager_Templates_Worker, Manager_Settings_Batch
    try:
        ManagerTasks.start(self.request.id)

        client = Manager_Projects.get_mturk_api(use_sandbox)
        dictionary_settings_batch = data['settings_batch']

        # inject blocking code into the template
        if dictionary_settings_batch['block_workers']:
            dictionary_settings_batch[
                'template_worker'].template = Manager_Batches.preprocess_template_request(
                    database_object_project,
                    dictionary_settings_batch['template_worker'].template)

        # generate batch name if not given
        try:
            name_batch = data['name'].upper()
        except KeyError:
            name_batch = uuid.uuid4().hex.upper()

        # create batch
        database_object_batch = Manager_Batches.create_batch(
            name_batch=name_batch,
            database_object_project=database_object_project,
            use_sandbox=use_sandbox,
        )

        Manager_Templates_Worker.clone_and_fix_template(
            dictionary_settings_batch['template_worker'])

        Manager_Settings_Batch.clone_and_fix_settings_batch(
            database_object_project=database_object_project,
            database_object_batch=database_object_batch,
            dictionary_settings_batch=dictionary_settings_batch,
        )

        title = dictionary_settings_batch['title']
        if dictionary_settings_batch['has_content_adult'] == True:
            title = 'Contains adult content! {}'.format(title)

        # return database_object_batch
        for index, dictionary_hit in enumerate(data['data_csv']):
            try:
                mturk_obj_hit = client.create_hit(
                    Keywords=','.join([
                        keyword['text']
                        for keyword in dictionary_settings_batch['keywords']
                    ]),
                    MaxAssignments=dictionary_settings_batch[
                        'count_assignments'],
                    LifetimeInSeconds=dictionary_settings_batch['lifetime'],
                    AssignmentDurationInSeconds=dictionary_settings_batch[
                        'duration'],
                    AutoApprovalDelayInSeconds=1209600,
                    Reward=Manager_Batches.cent_to_dollar(
                        dictionary_settings_batch['reward']),
                    Title=title,
                    Description=dictionary_settings_batch['description'],
                    Question=Manager_Batches.create_question(
                        dictionary_settings_batch['template_worker'].template,
                        dictionary_settings_batch['template_worker'].
                        height_frame, dictionary_hit),
                    QualificationRequirements=[]
                    # QualificationRequirements=Manager_Batches.get_qualifications(data)
                )
                pass
            except ClientError as e:
                print(e)
                # messages.error(request, '''
                #     An error occured
                #     <a href="#alert_1" data-toggle="collapse" class="alert-link">details</a>
                #     <p class="collapse mb-0" id="alert_1">
                #         {}
                #     </p>
                # '''.format(e))

                if index == 0:
                    database_object_batch.delete()

                break

            # db_obj_tag = m_Tag.objects.create(
            #     name=project.glob_prefix_name_tag_hit+mturk_obj_hit['HIT']['HITId'],
            #     key_corpus=database_object_project.name
            # )

            # print(mturk_obj_hit)
            db_obj_hit = HIT.objects.create(
                # id_hit=str(random.randint(0, 9999999)),
                id_hit=mturk_obj_hit['HIT']['HITId'].upper(),
                batch=database_object_batch,
                parameters=json.dumps(dictionary_hit),
                datetime_expiration=mturk_obj_hit['HIT']['Expiration'],
                datetime_creation=mturk_obj_hit['HIT']['CreationTime'],
            )

            self.update_state(state='PROGRESS',
                              meta={
                                  'current': index + 1,
                                  'total': len(data['data_csv'])
                              })

        ManagerTasks.delete_by_uid(self.request.id)

    except Exception as e:
        ManagerTasks.failed(self.request.id)
        print('{}'.format(e))