def make_mag_bk_judge(item, testing=False):
    """Takes the federal judge data <item> and associates it with a Judge
    object.  Returns a Judge object.  To be used for importing bankruptcy
    and magistrate judges.  Example data below:

    CL_ID: fjc-bk-12
    COURT: AR,E
    JUDGE_NAME: JONES, PHYLLIS M.
    POSITION: Bankruptcy
    NAME_LAST: Jones
    NAME_FIRST: Phyllis
    NAME_MIDDLE: M.
    NAME_SUFFIX:
    GENDER: Female
    START_DATE: 2015-01-07
    START_DATE_GRANULARITY: %Y-%m-%d
    SOURCE: https://www.uscourts.gov/judicial-milestones/phyllis-m-jones
    """

    name = "{CL_ID}: {NAME_FIRST} {NAME_LAST}".format(**item)
    print("Now processing: %s" % name)

    if Person.objects.filter(cl_id=item["CL_ID"]).exists():
        raise ValidationError(
            "CL_ID already exists for the record being imported: %s" % name)

    if Person.objects.filter(
            name_first=item["NAME_FIRST"],
            name_middle=item["NAME_MIDDLE"],
            name_last=item["NAME_LAST"],
    ).exists():
        raise ValidationError(
            "Name already exists for record being imported %s" % name)

    if not pd.isnull(item["NAME_MIDDLE"]):
        if len(item["NAME_MIDDLE"]) == 1:
            item["NAME_MIDDLE"] += "."

    if not pd.isnull(item["GENDER"]):
        gender = get_gender(item["GENDER"])

    if not item["NAME_SUFFIX"] == "":
        suffix = get_suffix(item["NAME_SUFFIX"])
    else:
        suffix = ""

    # Instantiate Judge object.
    person = Person(
        name_first=item["NAME_FIRST"],
        name_middle=item["NAME_MIDDLE"],
        name_last=item["NAME_LAST"],
        name_suffix=suffix,
        gender=gender,
        cl_id=item["CL_ID"],
    )

    if not testing:
        person.save()

    # Add position.
    if re.search("Bankruptcy", item["POSITION"]):
        position_type = Position.JUDGE
        if item["COURT"]:
            court = FJC_BANKRUPTCY_COURTS[item["COURT"]]
    else:
        position_type = Position.MAGISTRATE
        if item["COURT"]:
            court = FJC_DISTRICT_COURTS[item["COURT"]]

    date_start = process_date_string(item["START_DATE"])

    position = Position(
        person=person,
        position_type=position_type,
        court_id=court,
        date_start=date_start,
        date_granularity_start=item["START_DATE_GRANULARITY"],
    )

    if not testing:
        position.save()

    sources = Source(person=person,
                     url=item["SOURCE"],
                     date_accessed=str(date.today()))

    if not testing:
        sources.save()
def make_federal_judge(item, testing=False):
    """Takes the federal judge data <item> and associates it with a Judge object.
    Returns a Judge object.
    """

    date_dob, date_granularity_dob = process_date(item["Birth Year"],
                                                  item["Birth Month"],
                                                  item["Birth Day"])

    dob_city = item["Birth City"]
    dob_state = item["Birth State"]
    # if foreign-born, leave blank for now.
    if len(dob_state) > 2:
        dob_state = ""
    name = "%s: %s %s %s" % (
        item["cl_id"],
        item["First Name"],
        item["Last Name"],
        str(date_dob),
    )
    fjc_check = Person.objects.filter(fjc_id=item["jid"])
    if len(fjc_check) > 0:
        print("Warning: %s exists" % name)
        return

    pres_check = Person.objects.filter(
        name_first=item["First Name"],
        name_last=item["Last Name"],
        date_dob=date_dob,
    )

    if not testing:
        print("Now processing: %s" % name)
    if len(pres_check) > 0:
        print("%s is a president." % name)
        person = pres_check[0]
        person.fjc_id = item["jid"]

    else:
        date_dod, date_granularity_dod = process_date(item["Death Year"],
                                                      item["Death Month"],
                                                      item["Death Day"])

        dod_city = item["Death City"]
        dod_state = item["Death State"]
        # if foreign-dead, leave blank for now.
        if len(dod_state) > 2:
            dod_state = ""

        if not pd.isnull(item["Middle Name"]):
            if len(item["Middle Name"]) == 1:
                item["Middle Name"] += "."

        if not pd.isnull(item["Gender"]):
            gender = get_gender(item["Gender"])

        # Instantiate Judge object.
        person = Person(
            name_first=item["First Name"],
            name_middle=item["Middle Name"],
            name_last=item["Last Name"],
            name_suffix=get_suffix(item["Suffix"]),
            gender=gender,
            fjc_id=item["jid"],
            cl_id=item["cl_id"],
            date_dob=date_dob,
            date_granularity_dob=date_granularity_dob,
            dob_city=dob_city,
            dob_state=dob_state,
            date_dod=date_dod,
            date_granularity_dod=date_granularity_dod,
            dod_city=dod_city,
            dod_state=dod_state,
        )

    if not testing:
        person.save()

    listraces = get_races(item["Race or Ethnicity"])
    races = [Race.objects.get(race=r) for r in listraces]
    for r in races:
        if not testing:
            person.race.add(r)

    add_positions_from_row(item, person, testing)

    # add education items (up to 5 of them)
    for schoolnum in range(1, 6):
        school_str = " (%s)" % schoolnum
        schoolname = item["School" + school_str]

        if pd.isnull(schoolname):
            continue

        if pd.isnull(item["Degree" + school_str]):
            degs = [""]
        else:
            degs = [x.strip() for x in item["Degree" + school_str].split(";")]
        for degtype in degs:
            deg_level = get_degree_level(degtype)
            degyear = item["Degree Year" + school_str]
            try:
                int(degyear)
            except:
                degyear = None
            school = get_school(schoolname)
            if school is not None:
                degree = Education(
                    person=person,
                    school=school,
                    degree_detail=degtype,
                    degree_level=deg_level,
                    degree_year=degyear,
                )
                if not testing:
                    degree.save()

    # Non-judicial positions
    titles, locations, startyears, endyears = transform_employ(
        item["Professional Career"])
    # There is no field in the FJC data with variables commented out below
    # (i.e. "Bankruptcy and Magistrate service").  This is commented out
    # for reference as pulling out bankruptcy and magistrate service does
    # need to be implemented (yet).

    # titles2, locations2, startyears2, endyears2 = transform_bankruptcy(item['Bankruptcy and Magistrate service'])
    # titles = titles + titles2
    # locations = locations + locations2
    # startyears = startyears + startyears2
    # endyears = endyears + endyears2

    for i in range(len(titles)):
        job_title = titles[i]
        if (pd.isnull(job_title) or job_title == ""
                or job_title.startswith("Nominated")):
            continue
        location = locations[i]
        start_year = startyears[i]
        end_year = endyears[i]

        job_title = job_title.strip()

        if pd.isnull(start_year) or start_year == "":
            date_start = None
            date_start_granularity = ""
        else:
            try:
                start_year = int(start_year)
            except:
                continue
            date_start = date(start_year, 1, 1)
            date_start_granularity = GRANULARITY_YEAR
        if not pd.isnull(end_year) and end_year.isdigit():
            end_year = int(end_year)
            date_end = date(end_year, 1, 1)
            date_end_granularity = GRANULARITY_YEAR
        else:
            date_end = None
            date_end_granularity = ""

        if not pd.isnull(location):
            location = location.strip()
            if "," in location:
                city, state = [x.strip() for x in location.split(",")]
                org = ""
                if state in STATES_NORMALIZED.values():
                    pass
                elif state.lower() in STATES_NORMALIZED.keys():
                    state = STATES_NORMALIZED[state.lower()]
                else:
                    city, state = "", ""
                    org = location
            else:
                city, state = "", ""
                org = location
            # test for schools and courts
        else:
            city, state, org = "", "", ""

        position = Position(
            person=person,
            job_title=job_title,
            date_start=date_start,
            date_granularity_start=date_start_granularity,
            date_termination=date_end,
            date_granularity_termination=date_end_granularity,
            location_city=city,
            location_state=state,
            organization_name=org,
        )

        if not testing:
            try:
                position.save()
            except Exception:
                continue