def delete(self, request, slug_project, database_object_project, use_sandbox, format=None): Manager_Projects.delete(database_object_project) return Response(status=status.HTTP_204_NO_CONTENT)
def update(self, instance, validated_data): # print('validated_data') # print(validated_data) # print('validated_data') project = Manager_Projects.update(instance=instance, validated_data=validated_data) # for key, value in validated_data.items(): # if key == 'keywords': # print(value) # instance.keywords.clear() # for keyword in value: # try: # instance.keywords.add(keyword['id']) # except KeyError: # keyword_new = Keyword.objects.get_or_create(text=keyword['text'])[0] # instance.keywords.add(keyword_new) # elif key == 'count_assignments_max_per_worker': # response = Manager_Global_DB.set_count_assignments_max_per_worker(instance.slug, value) # else: # print('key') # print(key) # print(value) # setattr(instance, key, value) # instance.save() return project
def get(self, request, slug_project, database_object_project, use_sandbox, format=None): project = Manager_Projects.get(database_object_project) serializer = Serializer_Project( project, ) # serializer = Serializer_Project(database_object_project, context={'request': request}) return Response(serializer.data)
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
def ping(request, slug_project, database_object_project, use_sandbox, format=None): dictionary_data = Manager_Projects.ping(database_object_project) return Response(dictionary_data)
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 create(self, validated_data): print('validated_data') print(validated_data) print('validated_data') project = Manager_Projects.create(data=validated_data) return project
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))
def get_count_assignments_max_per_worker(request, slug_project, database_object_project, use_sandbox, format=None): dictionary_data = Manager_Projects.get_count_assignments_max_per_worker( database_object_project) # dictionary_data = {} # return Response(True) return Response(dictionary_data)
def handle(self, *args, **options): use_sandbox = options['use_sandbox'] project_name = options['project'] queryset = Manager_Projects.get_all() if project_name is not None: queryset = queryset.filter(name=project_name) for project in queryset: self.stdout.write( self.style.SUCCESS('Syncing project {} with {} HITs'.format( project.name, HIT.objects.filter(batch__project=project).count()))) Manager_Batches.sync_mturk(project, use_sandbox)
def get(self, request, format=None): # list_projects = [ # # 'real-money-web-page-segmentation-01', # # 'real-money-web-page-segmentation-02', # # 'real-money-web-page-segmentation-02-rest', # # 'real-money-web-page-segmentation-03', # ] # # for name_project in list_projects: # migrate_project(name_project) queryset_projects = Manager_Projects.get_all() serializer = Serializer_Project(queryset_projects, many=True, context={ 'request': request, }) return Response(serializer.data)
def get(self, request, format=None): # list_projects = [ # # 'real-money-web-page-segmentation-01', # # 'real-money-web-page-segmentation-02', # # 'real-money-web-page-segmentation-02-rest', # # 'real-money-web-page-segmentation-03', # ] # # for name_project in list_projects: # migrate_project(name_project) list_fields = request.query_params.getlist('fields[]') if len(list_fields) == 0: list_fields = None queryset_projects = Manager_Projects.get_all(fields=list_fields) serializer = Serializer_Project( queryset_projects, many=True, context={ 'request': request, 'fields': list_fields, }) return Response(serializer.data)
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))
def projects_check_uniqueness(request, name_project): is_unique = Manager_Projects.check_uniqueness(name_project) # import time # time.sleep(1) return Response(is_unique)