def make_new_person(candidate, **options): legal_name = (candidate.get('other_name', None) or '').title() if legal_name: legal_name += ' ' legal_name += (candidate.get('surname', None) or '').title() slug_to_use = slugify(legal_name) suffix = 2 while True: try: Person.objects.get(slug=slug_to_use) except Person.DoesNotExist: # Then this slug_to_use is fine, so just break: break slug_to_use = re.sub('-?\d*$', '', slug_to_use) + '-' + str(suffix) suffix += 1 new_person = Person(legal_name=legal_name, slug=slug_to_use) maybe_save(new_person, **options) return new_person
def setUp(self): # create a load of test people in the database names = [ u'Adam Ant', u'Bobby Smith', u'Fred Jones', u'Joe Bloggs', u'Joe Smith', u'Josepth Smyth', ] for name in names: Person( slug=slugify(name), legal_name=name, gender='m', ).save() # Haystack indexes are not touched when fixtures are dumped. Run this # so that other changes cannot affect these tests. Have added a note to # a HayStack issue regarding this: # https://github.com/toastdriven/django-haystack/issues/226 call_command('rebuild_index', interactive=False, verbosity=0)