示例#1
0
        def attach_members(committee, org):
            term = get_current_term(obj_to_jid(org))
            for member in committee['members']:
                osid = member.get('leg_id', None)
                person_id = lookup_entry_id('people', osid)
                if person_id:
                    m = Membership(person_id, org._id,
                                   role=member['role'],
                                   chamber=org.chamber,
                                   # term=term['name'],
                                   start_date=str(term['start_year']))
                    m.add_extra('term', term['name'])
                    # We can assume there's no end_year because it's a current
                    # member of the committee. If they left the committee, we don't
                    # know about it yet :)
                    self.save_object(m)

                    if m.role != 'member':
                        # In addition to being the (chair|vice-chair),
                        # they should also be noted as a member.
                        m = Membership(person_id, org._id,
                                       role='member',
                                       chamber=org.chamber,
                                       start_date=str(term['start_year']))
                        m.add_extra('term', term['name'])
                        self.save_object(m)
示例#2
0
    def migrate_people(self, state):
        spec = {}
        if state:
            spec["state"] = state
        for entry in self.billy_db.legislators.find(spec, timeout=False):
            jurisdiction_id = openstates_to_jid(entry['_id'])

            who = Person(entry['full_name'])
            who.identifiers = [{'scheme': 'openstates',
                               'identifier': entry['_id']}]
            who._openstates_id = entry['_id']
            who.created_at = entry['created_at']
            who.updated_at = entry['updated_at']
            if who.name != entry['_scraped_name']:
                who.add_name(entry['_scraped_name'])

            for k, v in {
                "photo_url": "image",
                "chamber": "chamber",
                "district": "district",
            }.items():
                if entry.get(k, None):
                    setattr(who, v, entry[k])

            who.sources = entry['sources']

            home = entry.get('url', None)
            if home:
                who.add_link(home, "Homepage")

            vsid = entry.get('votesmart_id')
            if vsid:
                who.add_identifier(vsid, 'votesmart-id')

            tdid = entry.get('transparencydata_id')
            if tdid:
                who.add_identifier(tdid, 'transparencydata-id')

            blacklist = ["photo_url", "chamber", "district", "url",
                         "roles", "offices", "party", "state", "sources",
                         "active", "old_roles", "_locked_fields", "created_at",
                         "updated_at", "transparencydata_id", "votesmart_id",
                         "leg_id", "email", "phone", "fax", "_scraped_name",
                         "_id", "_all_ids", "full_name", "country", "level",
                         "office_address", "suffixes", "_type"]
            # The above is a list of keys we move over by hand later.

            for key, value in entry.items():
                if key in blacklist or not value:  # or key.startswith("_"):
                    continue
                who.extras[key] = value

            who.add_meta('locked_fields', entry.get('_locked_fields', []))

            chamber = entry.get('chamber')
            if chamber == "joint":
                continue

            legislature = None
            if chamber:
                legislature = _hot_cache.get('{state}-{chamber}'.format(
                    state=entry['state'],
                    chamber=chamber,
                ))

                if legislature is None:
                    print(chamber, entry['state'], entry['_id'])
                    raise Exception("Someone's in the void.")

            self.save_object(who)  # gives who an id, btw.

            party = entry.get('party', None)
            db.memberships.remove({"person_id": who._id}, safe=True)

            if party:
                m = Membership(who._id, self.create_or_get_party(entry['party']))
                self.save_object(m)

            if legislature:
                term = get_current_term(jurisdiction_id)
                m = Membership(who._id, legislature,
                               chamber=chamber,
                               start_date=str(term['start_year']))
                m.add_extra('term', term['name'])

                chamber, district = (entry.get(x, None)
                                     for x in ['chamber', 'district'])

                if chamber:
                    m.chamber = chamber

                if district:
                    m.post_id = district

                for key in ['email', 'fax', 'phone']:
                    if key in entry and entry[key]:
                        m.add_contact_detail(type=key,
                                             value=entry[key],
                                             note=key)

                if entry.get("office_address"):
                    m.add_contact_detail(type='office',
                                         value=entry['office_address'],
                                         note='Office Address')

                for office in entry.get('offices', []):
                    note = office['name']
                    for key, value in office.items():
                        if not value or key in ["name", "type"]:
                            continue
                        m.add_contact_detail(type=key, value=value, note=note)

                self.save_object(m)

            for session in entry.get('old_roles', []):
                roles = entry['old_roles'][session]
                meta = get_metadata(obj_to_jid(who))
                term = None

                for role in roles:
                    term = role['term']
                    to = None
                    for to in meta['terms']:
                        if term == to['name']:
                            term = to
                            break
                    else:
                        raise Exception("No term found?")

                    start_year = term['start_year']
                    end_year = term['end_year']

                    if 'committee' in role:
                        cid = role.get('committee_id')
                        if cid:
                            jid = _hot_cache.get(cid)
                            if jid:
                                m = Membership(who._id, jid,
                                               role='member',
                                               start_date=str(start_year),
                                               end_date=str(end_year),
                                               chamber=role['chamber'])
                                m.add_extra('term', term['name'])

                                if "position" in role:
                                    m.role = role['position']

                                self.save_object(m)

                                if m.role != 'member':
                                    # In addition to being the (chair|vice-chair),
                                    # they should also be noted as a member.
                                    m = Membership(who._id,
                                                   jid,
                                                   role='member',
                                                   start_date=str(start_year),
                                                   end_date=str(end_year),
                                                   chamber=role['chamber'])
                                    m.add_extra('term', term['name'])
                                    self.save_object(m)

                    if 'district' in role:
                        oid = "{state}-{chamber}".format(**role)
                        leg_ocdid = _hot_cache.get(oid)
                        if leg_ocdid:
                            m = Membership(who._id, leg_ocdid,
                                           start_date=str(start_year),
                                           end_date=str(end_year),
                                           post_id=role['district'],
                                           chamber=role['chamber'])
                            m.add_extra('term', term['name'])
                            self.save_object(m)