def handle_noargs(self, **options): for person in Person.objects.all(): legal_name = person.legal_name other_names = person.other_names normalized_legal_name = normalize_name(legal_name) normalized_other_names = normalize_name(other_names) if (legal_name != normalized_legal_name) or (other_names != normalized_other_names): print >> sys.stderr, "Found difference(s):" if legal_name != normalized_legal_name: print >> sys.stderr, " ", legal_name, "should be", normalized_legal_name if other_names != normalized_other_names: print >> sys.stderr, " ", other_names, "should be", normalized_other_names person.legal_name = normalized_legal_name person.other_names = normalized_other_names maybe_save(person, **options)
def handle_noargs(self, **options): for person in Person.objects.all(): legal_name = person.legal_name other_names = person.other_names normalized_legal_name = normalize_name(legal_name) normalized_other_names = normalize_name(other_names) if (legal_name != normalized_legal_name) or ( other_names != normalized_other_names): print >> sys.stderr, "Found difference(s):" if legal_name != normalized_legal_name: print >> sys.stderr, " ", legal_name, "should be", normalized_legal_name if other_names != normalized_other_names: print >> sys.stderr, " ", other_names, "should be", normalized_other_names person.legal_name = normalized_legal_name person.other_names = normalized_other_names maybe_save(person, **options)
def make_new_person(candidate, **options): legal_name = normalize_name((candidate.get('other_name', None) or '').title()) if legal_name: legal_name += ' ' legal_name += normalize_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 make_new_person(candidate, **options): legal_name = normalize_name((candidate.get('other_name', None) or '').title()) if legal_name: legal_name += ' ' legal_name += normalize_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 full_name(c): return normalize_name(c['other_name']) + " " + normalize_name(c['surname'])
def update_candidates_for_place(place_name, place_kind, parliamentary_session, title, race_type, candidates, same_person_checker, **options): all_updates_succeeded = True place = get_matching_place(place_name, place_kind, parliamentary_session) current_aspirants = Position.objects.filter(place=place, title=title).currently_active() print "%s %s %s %s %s" % (place_name, place_kind, parliamentary_session, title, race_type) def full_name(c): return normalize_name(c['other_name']) + " " + normalize_name(c['surname']) code_to_existing_aspirant = dict((a.external_id, a) for a in current_aspirants if a.external_id) code_to_current_candidates = dict((c['code'], c) for c in candidates) existing_aspirant_codes = set(code_to_existing_aspirant.keys()) current_candidate_codes = set(code_to_current_candidates.keys()) existing_aspirants_to_remove = existing_aspirant_codes - current_candidate_codes new_candidates_to_add = current_candidate_codes - existing_aspirant_codes matched_candidate_codes = current_candidate_codes & existing_aspirant_codes # Add each candidate that wasn't already present: for code in new_candidates_to_add: candidate = code_to_current_candidates[code] first_names = normalize_name(candidate['other_name'] or '') surname = normalize_name(candidate['surname'] or '') person = get_person_from_names(first_names, surname) # If that person was an existing current aspirant, they'll # just need to have the IEBC candidate code set: iebc_code_just_needed_setting = False for matching_existing_aspirant_position in current_aspirants.filter(person=person): if matching_existing_aspirant_position.external_id: if matching_existing_aspirant_position.external_id != code: print " original: '%s'" % (matching_existing_aspirant_position.external_id,) print " candidate: '%s'" % (code,) format_tuple = (matching_existing_aspirant_position.person, matching_existing_aspirant_position.external_id, person, code) message = "There was an existing candidate (%s - %s) with a name match for (%s - %s) but different codes" % format_tuple # raise Exception, message print message else: matching_existing_aspirant_position.external_id = code print >> sys.stderr, "* The IEBC code was missing for %s" % (matching_existing_aspirant_position,) maybe_save(matching_existing_aspirant_position, **options) update_parties(person, candidate['party'], **options) iebc_code_just_needed_setting = True if iebc_code_just_needed_setting: continue # If we have a person match, but they weren't a current # aspirant in this race, then be careful that we're not # mismatching using the SamePersonChecker: if person: same_person = same_person_checker.check_same_and_update(candidate, place, race_type, person) if same_person is None: all_updates_succeeded = False continue if not person: print >> sys.stderr, "* Creating a person because none matched %s %s" % (first_names, surname) person = make_new_person(candidate, **options) assert(person) update_parties(person, candidate['party'], **options) aspirant_position_properties = { 'organisation': Organisation.objects.get(name='REPUBLIC OF KENYA'), 'place': place, 'person': person, 'title': title, 'category': 'political'} # First see if that aspirant position already exists: existing_matching_aspirant_positions = Position.objects.filter(**aspirant_position_properties).currently_active() if existing_matching_aspirant_positions: # If it does, make sure that the IEBC code is set in # external_id, and that the end_date is 'future': for existing_matching_aspirant_position in existing_matching_aspirant_positions: if existing_matching_aspirant_position.external_id != code or existing_matching_aspirant_position.end_date != future_approximate_date: print >> sys.stderr, "* Need to set either the IEBC code or the end date (future) for %s" % (existing_matching_aspirant_position,) existing_matching_aspirant_position.external_id = code existing_matching_aspirant_position.end_date = future_approximate_date maybe_save(existing_matching_aspirant_position, **options) else: # Then we have to create a new position: new_position = Position(start_date=today_approximate_date, end_date=future_approximate_date, external_id=code, **aspirant_position_properties) print >> sys.stderr, "* Creating a missing position: %s" % (new_position,) maybe_save(new_position, **options) # For those aspirants that are no longer current, end their aspirant position: for code in existing_aspirants_to_remove: existing_aspirant_to_remove = code_to_existing_aspirant[code] print >> sys.stderr, "* Removing a no longer current aspirant: %s (position id: %s)" % (existing_aspirant_to_remove, existing_aspirant_to_remove.id,) existing_aspirant_to_remove.end_date = yesterday_approximate_date maybe_save(existing_aspirant_to_remove, **options) # For those aspirants that were already present, just make sure # their party assignments are correct: for code in matched_candidate_codes: # Make sure that the party assignments are correct: person = code_to_existing_aspirant[code].person candidate = code_to_current_candidates[code] update_parties(person, candidate['party'], **options) return all_updates_succeeded
def full_name(c): return normalize_name(c['other_name']) + " " + normalize_name( c['surname'])
def update_candidates_for_place(place_name, place_kind, parliamentary_session, title, race_type, candidates, same_person_checker, **options): all_updates_succeeded = True place = get_matching_place(place_name, place_kind, parliamentary_session) current_aspirants = Position.objects.filter( place=place, title=title).currently_active() print "%s %s %s %s %s" % (place_name, place_kind, parliamentary_session, title, race_type) def full_name(c): return normalize_name(c['other_name']) + " " + normalize_name( c['surname']) code_to_existing_aspirant = dict( (a.external_id, a) for a in current_aspirants if a.external_id) code_to_current_candidates = dict((c['code'], c) for c in candidates) existing_aspirant_codes = set(code_to_existing_aspirant.keys()) current_candidate_codes = set(code_to_current_candidates.keys()) existing_aspirants_to_remove = existing_aspirant_codes - current_candidate_codes new_candidates_to_add = current_candidate_codes - existing_aspirant_codes matched_candidate_codes = current_candidate_codes & existing_aspirant_codes # Add each candidate that wasn't already present: for code in new_candidates_to_add: candidate = code_to_current_candidates[code] first_names = normalize_name(candidate['other_name'] or '') surname = normalize_name(candidate['surname'] or '') person = get_person_from_names(first_names, surname) # If that person was an existing current aspirant, they'll # just need to have the IEBC candidate code set: iebc_code_just_needed_setting = False for matching_existing_aspirant_position in current_aspirants.filter( person=person): if matching_existing_aspirant_position.external_id: if matching_existing_aspirant_position.external_id != code: print " original: '%s'" % ( matching_existing_aspirant_position.external_id, ) print " candidate: '%s'" % (code, ) format_tuple = ( matching_existing_aspirant_position.person, matching_existing_aspirant_position.external_id, person, code) message = "There was an existing candidate (%s - %s) with a name match for (%s - %s) but different codes" % format_tuple # raise Exception, message print message else: matching_existing_aspirant_position.external_id = code print >> sys.stderr, "* The IEBC code was missing for %s" % ( matching_existing_aspirant_position, ) maybe_save(matching_existing_aspirant_position, **options) update_parties(person, candidate['party'], **options) iebc_code_just_needed_setting = True if iebc_code_just_needed_setting: continue # If we have a person match, but they weren't a current # aspirant in this race, then be careful that we're not # mismatching using the SamePersonChecker: if person: same_person = same_person_checker.check_same_and_update( candidate, place, race_type, person) if same_person is None: all_updates_succeeded = False continue if not person: print >> sys.stderr, "* Creating a person because none matched %s %s" % ( first_names, surname) person = make_new_person(candidate, **options) assert (person) update_parties(person, candidate['party'], **options) aspirant_position_properties = { 'organisation': Organisation.objects.get(name='REPUBLIC OF KENYA'), 'place': place, 'person': person, 'title': title, 'category': 'political' } # First see if that aspirant position already exists: existing_matching_aspirant_positions = Position.objects.filter( **aspirant_position_properties).currently_active() if existing_matching_aspirant_positions: # If it does, make sure that the IEBC code is set in # external_id, and that the end_date is 'future': for existing_matching_aspirant_position in existing_matching_aspirant_positions: if existing_matching_aspirant_position.external_id != code or existing_matching_aspirant_position.end_date != future_approximate_date: print >> sys.stderr, "* Need to set either the IEBC code or the end date (future) for %s" % ( existing_matching_aspirant_position, ) existing_matching_aspirant_position.external_id = code existing_matching_aspirant_position.end_date = future_approximate_date maybe_save(existing_matching_aspirant_position, **options) else: # Then we have to create a new position: new_position = Position(start_date=today_approximate_date, end_date=future_approximate_date, external_id=code, **aspirant_position_properties) print >> sys.stderr, "* Creating a missing position: %s" % ( new_position, ) maybe_save(new_position, **options) # For those aspirants that are no longer current, end their aspirant position: for code in existing_aspirants_to_remove: existing_aspirant_to_remove = code_to_existing_aspirant[code] print >> sys.stderr, "* Removing a no longer current aspirant: %s (position id: %s)" % ( existing_aspirant_to_remove, existing_aspirant_to_remove.id, ) existing_aspirant_to_remove.end_date = yesterday_approximate_date maybe_save(existing_aspirant_to_remove, **options) # For those aspirants that were already present, just make sure # their party assignments are correct: for code in matched_candidate_codes: # Make sure that the party assignments are correct: person = code_to_existing_aspirant[code].person candidate = code_to_current_candidates[code] update_parties(person, candidate['party'], **options) return all_updates_succeeded