def test_can_add_subs_to_duplicate_set(self): dup_set = models.DuplicateSubmissionSet() dup_set.save() subs = [factories.FormSubmissionFactory.create() for i in range(2)] dup_set.submissions.add(*subs) for sub in subs: self.assertEqual(sub.duplicate_set, dup_set) self.assertTrue(sub.duplicate_set.id)
def handle(self, *args, **options): dup_sets = SubmissionsService.find_duplicates( models.FormSubmission.objects.all()) self.stdout.write("Found {} duplicate sets".format(len(dup_sets))) existing_dup_sets = models.DuplicateSubmissionSet.objects.all() self.stdout.write("{} duplicate sets already exist".format( existing_dup_sets.count())) count_already_existed = 0 dup_set_extensions = {} new_dup_sets = [] existing_dup_set_lookups = { frozenset(dup_set.submissions.all()): dup_set for dup_set in existing_dup_sets } for dup_set in dup_sets: found_existing = False for lookup_set, existing in existing_dup_set_lookups.items(): if dup_set == lookup_set: found_existing = True count_already_existed += 1 break elif dup_set & lookup_set: dup_set_extensions[existing] = dup_set found_existing = True break if not found_existing: new_dup_sets.append(dup_set) for existing, new_dups in dup_set_extensions: existing.submissions.add(*new_dups) self.stdout.write("{} found duplicate sets were existing".format( count_already_existed)) self.stdout.write("Extended {} existing duplicate sets".format( len(dup_set_extensions))) for new_set in new_dup_sets: new_dup_set_object = models.DuplicateSubmissionSet() new_dup_set_object.save() new_dup_set_object.submissions.add(*new_set) self.stdout.write("Created {} new duplicate sets".format( len(new_dup_sets)))
def link_with_any_duplicates(submission, applicant_id): """Links submission with any duplicates from the same applicant If duplicates are found, returns the DuplicateSubmissionSet id If no duplicates are found, returns False """ duplicates = check_for_existing_duplicates(submission, applicant_id) if duplicates: dup_set_id = None unadded_duplicates = [] for dup in duplicates: if dup.duplicate_set_id: dup_set_id = dup.duplicate_set_id else: unadded_duplicates.append(dup) if not dup_set_id: new_dup_set = models.DuplicateSubmissionSet() new_dup_set.save() new_dup_set.submissions.add(*unadded_duplicates) dup_set_id = new_dup_set.id submission.duplicate_set_id = dup_set_id submission.save() return dup_set_id return False