def update(self, mturk_assignment=None, hit=None): """Update self with Mechanical Turk API data If mturk_assignment is given to this function, it should be a Boto assignment object that represents a Mechanical Turk Assignment instance. Otherwise, Amazon Mechanical Turk is contacted. This instance's attributes are updated. """ if mturk_assignment is None: hit = self.connection.get_hit(self.hit.mturk_id)[0] for a in self.connection.get_assignments(hit.HITId): # While we have the query, we may as well update if a.AssignmentId == self.mturk_id: # That's this record. Hold onto so we can update below assignment = a else: other_assignment = Assignment.objects.get( mturk_id=a.AssignmentId) other_assignment.update(a) else: assert isinstance(mturk_assignment, boto.mturk.connection.Assignment) assignment = mturk_assignment self.status = self.reverse_status_lookup[assignment.AssignmentStatus] self.worker_id = assignment.WorkerId self.submit_time = amazon_string_to_datetime(assignment.SubmitTime) self.accept_time = amazon_string_to_datetime(assignment.AcceptTime) self.auto_approval_time = amazon_string_to_datetime( assignment.AutoApprovalTime) self.submit_time = amazon_string_to_datetime(assignment.SubmitTime) # Different response groups for query if hasattr(assignment, 'RejectionTime'): self.rejection_time = amazon_string_to_datetime( assignment.RejectionTime) if hasattr(assignment, 'ApprovalTime'): self.approval_time = amazon_string_to_datetime( assignment.ApprovalTime) self.save() # Update any Key-Value Pairs that were associated with this # assignment for result_set in assignment.answers: for question in result_set: for key, value in question.fields: kv = KeyValue.objects.get_or_create(key=key, assignment=self)[0] if kv.value != value: kv.value = value kv.save()
def update(self, mturk_hit=None, do_update_assignments=False): """Update self with Mechanical Turk API data If mturk_hit is given to this function, it should be a Boto hit object that represents a Mechanical Turk HIT instance. Otherwise, Amazon Mechanical Turk is contacted to get additional information. This instance's attributes are updated. """ if mturk_hit is None or not hasattr(mturk_hit, "HITStatus"): hit = self.connection.get_hit(self.mturk_id)[0] else: assert isinstance(mturk_hit, boto.mturk.connection.HIT) hit = mturk_hit self.status = HIT.reverse_status_lookup[hit.HITStatus] self.reward = hit.Amount self.assignment_duration_in_seconds = hit.AssignmentDurationInSeconds self.auto_approval_delay_in_seconds = hit.AutoApprovalDelayInSeconds self.max_assignments = hit.MaxAssignments self.creation_time = amazon_string_to_datetime(hit.CreationTime) self.description = hit.Description self.title = hit.Title self.hit_type_id = hit.HITTypeId self.keywords = hit.Keywords if hasattr(self, 'NumberOfAssignmentsCompleted'): self.number_of_assignments_completed =\ hit.NumberOfAssignmentsCompleted if hasattr(self, 'NumberOfAssignmentsAvailable'): self.number_of_assignments_available =\ hit.NumberOfAssignmentsAvailable if hasattr(self, 'NumberOfAssignmentsPending'): self.number_of_assignments_pending =\ hit.NumberOfAssignmentsPending #'CurrencyCode', 'Reward', 'Expiration', 'expired'] self.save() if do_update_assignments: self.update_assignments()
def test_amazon_string_to_datetime(self): sample_date = '2012-04-04T22:31:03Z' self.assertEqual( amazon_string_to_datetime(sample_date), datetime.datetime(2012, 4, 4, 22, 31, 3))