def get_form(self, request, obj=None, **kwargs): defaults = {} if obj is None: defaults['form'] = HITCreationForm if not messages.get_messages(request): hits = mturk.list_hits()['HITs'] hitCounts = ((name, sum(1 for hit in hits if hit['HITStatus'] == 'Unassignable' and hit['Description'] == desc)) for name, desc in hitDescriptions.items()) hitCountHuman = [ f"{str(count)} {name}" for name, count in hitCounts if count ] if hitCountHuman: if len(hitCountHuman) == 1: hitsHuman = hitCountHuman[0] elif len(hitCountHuman) == 2: hitsHuman = f"{hitCountHuman[0]} and {hitCountHuman[1]}" else: hitsHuman = f"{', '.join(hitCountHuman[:-1])}, and {hitCountHuman[-1]}" self.message_user( request, f"There are {hitsHuman} HITs that are still in progress. Only proceed if you want more.", messages.WARNING) else: self.fieldsets = ((None, {'fields': ('assignment_id', 'data')}), ) self.readonly_fields = ('assignment_id', ) # 'hitID', 'workerID' defaults.update(kwargs) return super().get_form(request, obj, **defaults)
def changelist_view(self, request, extra_context=None): # Update assignment statuses of any new assignments try: self.assignments = { assignment['AssignmentId']: assignment for hit in mturk.list_hits()['HITs'] for assignment in mturk.list_assignments_for_hit(HITId=hit['HITId']).get('Assignments', []) } for obj in HIT.objects.filter(Q(data__has_key='hitId') &~ Q(data__has_key='status')): obj.data['status'] = self.assignments.get(obj.assignment_id, {}).get('AssignmentStatus', '') obj.save() except Exception as e: self.message_user(request, f'Unable to retrieve assignment statuses. {e}', messages.ERROR) return super().changelist_view(request, extra_context=None)
def print_assignment(hit_id): if hit_id == 'all': a = [] for hit in mturk.list_hits()['HITs']: try: a.extend( mturk.list_assignments_for_hit(HITId=hit['HITId']).get( 'Assignments', [])) except Exception as e: print(e) pprint(a) else: pprint( mturk.list_assignments_for_hit(HITId=hit_id).get( 'Assignments', []))
def delete_hit(phase): # Delete all HITs for now for item in mturk.list_hits()['HITs']: hit_id = item['HITId'] print('HITId:', hit_id) # GET the HIT status status = mturk.get_hit(HITId=hit_id)['HIT']['HITStatus'] print('HITStatus: ', status) description = mturk.get_hit(HITId=hit_id)['HIT']['Description'] # delete phase01a if phase == 'phase01a' and description == hitDescriptions['phase01a']: # If HIT is active then set it to expire immediately if status == 'Assignable': response = mturk.update_expiration_for_hit(HITId=hit_id, ExpireAt=datetime( 2015, 1, 1)) if status == 'Unassignable': try: response = mturk.update_expiration_for_hit( HITId=hit_id, ExpireAt=datetime(2015, 1, 1)) except Exception as e: print(e) # Delete the HIT try: mturk.delete_hit(HITId=hit_id) except Exception as e: # print(e) print('Not deleted') else: print('Deleted') elif phase == 'phase01b' and description == hitDescriptions['phase01b']: # If HIT is active then set it to expire immediately if status == 'Assignable': response = mturk.update_expiration_for_hit(HITId=hit_id, ExpireAt=datetime( 2015, 1, 1)) print("I found for phase1a") # Delete the HIT try: mturk.delete_hit(HITId=hit_id) except: print('Not deleted') else: print('Deleted') elif phase == 'phase03' and description == hitDescriptions['phase03']: # If HIT is active then set it to expire immediately if status == 'Assignable': response = mturk.update_expiration_for_hit(HITId=hit_id, ExpireAt=datetime( 2015, 1, 1)) print("I found for phase1a") # Delete the HIT try: mturk.delete_hit(HITId=hit_id) except: print('Not deleted') else: print('Deleted')
def print_hit(): pprint(mturk.list_hits()['HITs'])
def delete_hit(phase): # Delete all HITs for now for item in mturk.list_hits()['HITs']: hit_id=item['HITId'] print('HITId:', hit_id) # GET the HIT status status = mturk.get_hit(HITId=hit_id)['HIT']['HITStatus'] print('HITStatus: ', status) description = mturk.get_hit(HITId=hit_id)['HIT']['Description'] # delete phase01a if phase == 'phase01a' and description == 'The phase01a for generating the QA pairs from the given image about a common single object from machine learning image classification dataset': # If HIT is active then set it to expire immediately if status=='Assignable': response = mturk.update_expiration_for_hit( HITId=hit_id, ExpireAt=datetime(2015, 1, 1) ) print("I found for phase1a") # Delete the HIT try: mturk.delete_hit(HITId=hit_id) except: print('Not deleted') else: print('Deleted') elif phase == 'phase01b' and description == 'The phase01b for crowdsourcing game, given an image of single object from ML image dataset, answer the questions provided': # If HIT is active then set it to expire immediately if status=='Assignable': response = mturk.update_expiration_for_hit( HITId=hit_id, ExpireAt=datetime(2015, 1, 1) ) print("I found for phase1a") # Delete the HIT try: mturk.delete_hit(HITId=hit_id) except: print('Not deleted') else: print('Deleted') elif phase == 'phase03' and description == 'The phase03 for crowdsourcing game, given an image of single object from ML image dataset, answer the questions provided': # If HIT is active then set it to expire immediately if status=='Assignable': response = mturk.update_expiration_for_hit( HITId=hit_id, ExpireAt=datetime(2015, 1, 1) ) print("I found for phase1a") # Delete the HIT try: mturk.delete_hit(HITId=hit_id) except: print('Not deleted') else: print('Deleted')