1import itertools
2
3from _init_environment import SetHostMTurkConnection
4from _init_environment import config_environment
5
6def description_filter(substring):
7	return lambda hit: substring in hit.Title
8
9def disable_hit(hit):
10	return conn.disable_hit(hit.HITId)
11
12def dispose_hit(hit):
13	# assignments must be first approved or rejected
14	for assignment in conn.get_assignments(hit.HITId):
15		if assignment.AssignmentStatus == 'Submitted':
16			conn.approve_assignment(assignment.AssignmentId)
17	return conn.dispose_hit(hit.HITId)
18
19def cleanup():
20	"""Remove any boto test related HIT's"""
21        config_environment()
22
23	global conn
24
25	conn = SetHostMTurkConnection()
26
27
28	is_boto = description_filter('Boto')
29	print 'getting hits...'
30	all_hits = list(conn.get_all_hits())
31	is_reviewable = lambda hit: hit.HITStatus == 'Reviewable'
32	is_not_reviewable = lambda hit: not is_reviewable(hit)
33	hits_to_process = filter(is_boto, all_hits)
34	hits_to_disable = filter(is_not_reviewable, hits_to_process)
35	hits_to_dispose = filter(is_reviewable, hits_to_process)
36	print 'disabling/disposing %d/%d hits' % (len(hits_to_disable), len(hits_to_dispose))
37	map(disable_hit, hits_to_disable)
38	map(dispose_hit, hits_to_dispose)
39
40	total_hits = len(all_hits)
41	hits_processed = len(hits_to_process)
42	skipped = total_hits - hits_processed
43	fmt = 'Processed: %(total_hits)d HITs, disabled/disposed: %(hits_processed)d, skipped: %(skipped)d'
44	print fmt % vars()
45
46if __name__ == '__main__':
47	cleanup()
48