Ejemplo n.º 1
0
 def current_role(self):
     if not getattr(self, "_current_role", None):
         self._current_role = get_current_role(self)
         try:
             self._current_role["district"] = int(
                 self._current_role["district"])
         except ValueError:
             pass
     return self._current_role
Ejemplo n.º 2
0
def test_get_current_role():
    populate_db()
    p = Person.objects.get(name="Amanda Adams")
    role = get_current_role(p)
    assert role == {
        "party": "Republican",
        "chamber": "lower",
        "district": "1",
        "division_id": "ocd-division/country:us/state:Alaska/district:1",
        "role": "",
        "state": "ak"
    }
Ejemplo n.º 3
0
def convert_legislator(leg):
    if leg.given_name and leg.family_name:
        first_name = leg.given_name
        last_name = leg.family_name
        suffixes = ""
    else:
        _, first_name, last_name, suffixes = name_tools.split(leg.name)

    legacy_ids = [
        oid.identifier for oid in leg.identifiers.all()
        if oid.scheme == "legacy_openstates"
    ]

    if not legacy_ids:
        legacy_ids = ["~not available~"]

    party = None
    chamber = None
    district = None
    state = None

    cr = get_current_role(leg)
    party = cr["party"]
    chamber = cr["chamber"]
    district = cr["district"]
    state = cr["state"]

    email = None
    offices = defaultdict(dict)
    for cd in leg.contact_details.all():
        offices[cd.note][cd.type] = cd.value
        if cd.type == "email" and not email:
            email = cd.value

    active = bool(chamber and district)

    try:
        url = leg.links.all()[0].url
    except IndexError:
        url = ""

    return {
        "id":
        legacy_ids[0],
        "leg_id":
        legacy_ids[0],
        "all_ids":
        legacy_ids,
        "full_name":
        leg.name,
        "first_name":
        first_name,
        "last_name":
        last_name,
        "suffix":
        suffixes,
        "photo_url":
        leg.image,
        "url":
        url,
        "email":
        email,
        "party":
        party,
        "chamber":
        chamber,
        "district":
        district,
        "state":
        state,
        "sources": [{
            "url": s.url
        } for s in leg.sources.all()],
        "active":
        active,
        "roles": [{
            "term": static.TERMS[state][-1]["name"],
            "district": district,
            "chamber": chamber,
            "state": state,
            "party": party,
            "type": "member",
            "start_date": None,
            "end_date": None,
        }] if active else [],
        "offices": [{
            "name":
            label,
            "fax":
            details.get("fax"),
            "phone":
            details.get("voice"),
            "email":
            details.get("email"),
            "address":
            details.get("address"),
            "type":
            "capitol" if "capitol" in label.lower() else "district",
        } for label, details in offices.items()],
        "old_roles": {},
        "middle_name":
        "",
        "country":
        "us",
        "level":
        "state",
        "created_at":
        leg.created_at.strftime(DATE_FORMAT),
        "updated_at":
        leg.updated_at.strftime(DATE_FORMAT),
    }
Ejemplo n.º 4
0
def convert_legislator(leg):
    if leg.given_name and leg.family_name:
        first_name = leg.given_name
        last_name = leg.family_name
        suffixes = ''
    else:
        _, first_name, last_name, suffixes = name_tools.split(leg.name)

    legacy_ids = [oid.identifier for oid in leg.identifiers.all()
                  if oid.scheme == 'legacy_openstates']

    if not legacy_ids:
        legacy_ids = ['~not available~']

    party = None
    chamber = None
    district = None
    state = None

    cr = get_current_role(leg)
    party = cr['party']
    chamber = cr['chamber']
    district = cr['district']
    state = cr['state']

    email = None
    offices = defaultdict(dict)
    for cd in leg.contact_details.all():
        offices[cd.note][cd.type] = cd.value
        if cd.type == 'email' and not email:
            email = cd.value

    active = bool(chamber and district)

    try:
        url = leg.links.all()[0].url
    except IndexError:
        url = ""

    return {
        'id': legacy_ids[0],
        'leg_id': legacy_ids[0],
        'all_ids': legacy_ids,
        'full_name': leg.name,
        'first_name': first_name,
        'last_name': last_name,
        'suffix': suffixes,
        'photo_url': leg.image,
        'url': url,
        'email': email,
        'party': party,
        'chamber': chamber,
        'district': district,
        'state': state,
        'sources': [{'url': s.url} for s in leg.sources.all()],
        'active': active,
        'roles': [{
            "term": static.TERMS[state][-1]['name'],
            "district": district,
            "chamber": chamber,
            "state": state,
            "party": party,
            "type": "member",
            "start_date": None,
            "end_date": None,
        }] if active else [],
        'offices': [
            {
                'name': label,
                'fax': details.get('fax'),
                'phone': details.get('voice'),
                'email': details.get('email'),
                'address': details.get('address'),
                'type': 'capitol' if 'capitol' in label.lower() else 'district',
            }
            for label, details in offices.items()
        ],

        'old_roles': {},
        'middle_name': '',
        'country': 'us',
        'level': 'state',
        'created_at': leg.created_at.strftime(DATE_FORMAT),
        'updated_at': leg.updated_at.strftime(DATE_FORMAT),
    }