示例#1
0
    def create(self, validated_data):
        print('validated_data')
        print(validated_data)
        print('validated_data')

        project = Manager_Settings_Batch.create(data=validated_data)

        return project
示例#2
0
    def import_batches(database_object_project: Project, request: Request, use_sandbox: bool):
        from api.classes import Manager_Settings_Batch, Manager_Templates_Worker

        for parsed_csv in request.data.get('parsedCSVs', []):
            name_batch = request.data['nameBatch']
            if name_batch is None:
                name_batch = uuid.uuid4().hex.upper()
            else:
                name_batch = name_batch.upper()

            # create batch
            database_object_batch = Manager_Batches.create_batch(
                name_batch=name_batch,
                database_object_project=database_object_project,
                use_sandbox=use_sandbox,
                datetime_creation=datetime.datetime.strptime(sorted(map(lambda x: x['CreationTime'], parsed_csv))[0], '%a %b %d %H:%M:%S %Z %Y'),
            )

            database_object_template_worker = Manager_Templates_Worker.get(request.data.get('templateWorker'))

            template_worker = Manager_Templates_Worker.clone_and_fix_template(database_object_template_worker)

            count_assignments_estimated = collections.Counter(map(lambda x: x['HITId'], parsed_csv)).most_common(1)[0][1]

            if 'name_settings_batch' in request.data:
                Manager_Settings_Batch.create(data={
                    'database_object_project': database_object_project,
                    'name': request.data['name_settings_batch'],
                    'title': parsed_csv[0]['Title'],
                    'reward': mturk_reward_to_database_reward(parsed_csv[0]['Reward']),
                    'count_assignments': count_assignments_estimated,
                    'description': parsed_csv[0]['Description'],
                    'lifetime': 604800,
                    'duration': int(parsed_csv[0]['AssignmentDurationInSeconds']),
                    'template_worker': template_worker,

                    'block_workers': False,
                    'has_content_adult': False,
                    'keywords': [],
                })

            Manager_Settings_Batch.clone_and_fix_settings_batch(
                database_object_project=database_object_project,
                database_object_batch=database_object_batch,
                dictionary_settings_batch={
                    'title': parsed_csv[0]['Title'],
                    'reward': mturk_reward_to_database_reward(parsed_csv[0]['Reward']),
                    'count_assignments': count_assignments_estimated,
                    'description': parsed_csv[0]['Description'],
                    'lifetime': 604800,
                    # 'lifetime': int(parsed_csv[0]['LifetimeInSeconds']),
                    'duration': int(parsed_csv[0]['AssignmentDurationInSeconds']),
                    # 'keywords': parsed_csv[0]['Keywords']
                    'template_worker': template_worker,
                },
            )

            dict_hits = {}
            dictionary_workers_available = {worker.id_worker: worker for worker in Worker.objects.all()}

            for assignment in parsed_csv:
                print('###################')
                try:
                    database_object_hit = dict_hits[assignment['HITId']]
                except KeyError:
                    database_object_hit = HIT.objects.create(
                        id_hit=assignment['HITId'].upper(),
                        batch=database_object_batch,
                        parameters=json.dumps({name_input: assignment['Input.{}'.format(name_input)] for name_input in [key.replace('Input.', '') for key in assignment if key.startswith('Input.')]}),
                        datetime_expiration=datetime.datetime.strptime(assignment['Expiration'], '%a %b %d %H:%M:%S %Z %Y'),
                        datetime_creation=datetime.datetime.strptime(assignment['CreationTime'], '%a %b %d %H:%M:%S %Z %Y'),
                    )
                    dict_hits[assignment['HITId']] = database_object_hit

                try:
                    database_object_worker = dictionary_workers_available[assignment['WorkerId']]
                except KeyError:
                    # otherwise create the new worker and add it to the dictionary
                    database_object_worker = Worker.objects.get_or_create(
                        id_worker=assignment['WorkerId'],
                    )[0]
                    dictionary_workers_available[assignment['WorkerId']] = database_object_worker

                Assignment.objects.create(
                    id_assignment=assignment['AssignmentId'],
                    hit=database_object_hit,
                    worker=database_object_worker,
                    status_external=mturk_status_to_database_status(assignment['AssignmentStatus']),
                    answer=json.dumps({name_input: assignment['Answer.{}'.format(name_input)] for name_input in [key.replace('Answer.', '') for key in assignment if key.startswith('Answer.')]}),
                    datetime_submit=datetime.datetime.strptime(assignment['SubmitTime'], '%a %b %d %H:%M:%S %Z %Y'),
                    datetime_accept=datetime.datetime.strptime(assignment['AcceptTime'], '%a %b %d %H:%M:%S %Z %Y'),
                )
                # break

        return {
            'name_batch': name_batch
        }