Ejemplo n.º 1
0
    def migrate_bills(self, state):
        spec = {}
        if state:
            spec['state'] = state

        bills = self.billy_db.bills.find(spec, timeout=False)
        for bill in bills:

            ocdid = _hot_cache.get('{state}-{chamber}'.format(**bill))
            org_name = name_cache.get(ocdid)
            if not org_name or not ocdid:
                raise Exception("""Can't look up chamber this legislative
                                instrument was introduced into.""")

            b = Bill(name=bill['bill_id'],
                     organization=org_name,
                     organization_id=ocdid,
                     session=bill['session'],
                     title=bill['title'],
                     chamber=bill['chamber'],
                     type=bill['type'],
                     created_at=bill['created_at'],
                     updated_at=bill['updated_at'])

            b.identifiers = [{'scheme': 'openstates',
                             'identifier': bill['_id']}]
            b._openstates_id = bill['_id']

            blacklist = ["bill_id", "session", "title", "chamber", "type",
                         "created_at", "updated_at", "sponsors", "actions",
                         "versions", "sources", "state", "action_dates",
                         "documents", "level", "country", "alternate_titles",
                         "subjects", "_id", "type", "_type", "_term", "_all_ids",
                         "summary", "_current_term", "_current_session"]

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

            for subject in bill.get('subjects', []):
                b.add_subject(subject)

            if 'summary' in bill and bill['summary']:
                # OpenStates only has one at most. let's just convert it
                # blind.
                b.add_summary('summary', bill['summary'])

            if 'alternate_titles' in bill:
                b.other_titles = [{"title": x, "note": None}
                                  for x in bill['alternate_titles']]

            for source in bill['sources']:
                b.add_source(source['url'], note='OpenStates source')

            for document in bill['documents']:
                b.add_document_link(
                    mimetype=document.get('mimetype'),
                    name=document['name'],
                    url=document['url'],
                    document_id=document['doc_id'],
                    on_duplicate='ignore')  # Old docs are bad about this

            b.add_extra('action_dates', bill['action_dates'])

            for version in bill['versions']:
                kwargs = {}
                mime = version.get("mimetype", version.get("+mimetype"))
                if mime:
                    kwargs['mimetype'] = mime

                b.add_version_link(name=version['name'],
                                   url=version['url'],
                                   on_duplicate='ignore',
                                   document_id=version['doc_id'],
                                   # ^^^ Some old OS entries are not so
                                   # good about this.
                                   **kwargs)

            for subject in bill.get('subjects', []):
                b.add_subject(subject)

            for action in bill['actions']:
                related_entities = None
                related = action.get("related_entities")
                if related:
                    related_entities = []
                    for rentry in related:
                        type_ = {
                            "committee": "organizations",
                            "legislator": "people"
                        }[rentry['type']]

                        nid = rentry['id'] = lookup_entry_id(type_, rentry['id'])
                        if nid:
                            if "ocd" not in nid:
                                raise Exception("Non-OCD id")

                        rentry['_type'] = {
                            "committee": "organization",
                            "legislator": "person"
                        }[rentry.pop('type')]

                        related_entities.append(rentry)

                when = dt.datetime.strftime(action['date'], "%Y-%m-%d")

                translate = {"bill:introduced": "introduced",
                             "bill:reading:1": "reading:1",
                             "bill:reading:2": "reading:2",
                             "bill:reading:3": "reading:3"}

                type_ = [translate.get(x, None) for x in action['type']]

                b.add_action(description=action['action'],
                             actor=action['actor'],
                             date=when,
                             type=filter(lambda x: x is not None, type_),
                             related_entities=related_entities)

            for sponsor in bill['sponsors']:
                type_ = 'people'
                sponsor_id = sponsor.get('leg_id', None)

                if sponsor_id is None:
                    type_ = 'organizations'
                    sponsor_id = sponsor.get('committee_id', None)

                kwargs = {}
                if sponsor_id:
                    objid = lookup_entry_id(type_, sponsor_id)

                    if objid is not None:
                        kwargs['entity_id'] = objid

                etype = {"people": "person",
                         "organizations": "organization"}[type_]

                #if sponsor.get('official_type'):
                #    kwargs['official_type'] = sponsor['official_type']
                # Not needed???

                b.add_sponsor(
                    name=sponsor['name'],
                    sponsorship_type=sponsor['type'],
                    entity_type=etype,
                    primary=sponsor['type'] == 'primary',
                    chamber=sponsor.get('chamber', None),
                    **kwargs)

            self.save_object(b)