Ejemplo n.º 1
0
    def fully_populate_bill(cls, jsondata, xmldata, bill_num, bill_type):
        # idempotent function, will not corrupt if called even if bill is fully populated
        if jsondata["short_title"] is None:
            if jsondata["popular_title"] is None:
                title = jsondata["official_title"]
            else:
                title = jsondata["popular_title"]
        else:
            title = jsondata["short_title"]
        title = title if len(title) <= 256 else title[:256]

        cong = jsondata["congress"]
        num = jsondata["number"]
        active = jsondata["history"]["active"]
        sig = jsondata["history"]["awaiting_signature"]
        enact = jsondata["history"]["enacted"]
        veto = jsondata["history"]["vetoed"]
        try:
            comm = jsondata["committees"][0]["committee"]
        except IndexError:
            comm = None

        intro_date = jsondata["introduced_at"]
        intro_date = datetime.strptime(intro_date, "%Y-%m-%d").date()

        bills = Bill.query.filter((Bill.bill_type == bill_type)
                                  & (Bill.bill_num == num)
                                  & (Bill.congress == cong)).all()

        if bills:
            # if the bill has been instantiated,
            # check if bill has been fully populated
            bill = bills[0]
            populated = bool(bill.title)
            if not populated:
                # if not populated, instantiate key identifying info (title, origin date)
                bill.congress = cong
                bill.title = title
                bill.introduced_date = intro_date
            # overwrite with most recent status info
            bill.active = active
            bill.awaiting_sig = sig
            bill.enacted = enact
            bill.vetoed = veto
            bill.committee = comm
        else:
            # if bill has NOT been instantiated,
            # create instantiation and add to db
            populated = False
            bill = Bill(
                title=title,
                congress=cong,
                bill_type=bill_type,
                bill_num=num,
                introduced_date=intro_date,
                committee=comm,
                active=active,
                awaiting_sig=sig,
                enacted=enact,
                vetoed=veto,
            )
            db.session.add(bill)

        # delete old statuses
        bill.statuses = []  # clears all actions attached to this bill
        statuses = BillStatus.query.filter(BillStatus.bill_id == bill.id).all()
        for bs in statuses:
            db.session.delete(bs)
        # bill statuses and actions
        actions = jsondata["actions"]
        for act in actions:
            stat = BillStatus()
            d = act["acted_at"][:10]
            d = datetime.strptime(d, "%Y-%m-%d").date()
            text = act["text"]
            text = text if len(text) < 128 else text[:128]
            act_type = act["type"]
            stat.date = d
            stat.text = text
            stat.action_type = act_type
            bill.statuses.append(stat)

        if not populated:
            # set and link legislative subjects
            subjects = jsondata["subjects"]
            for subj in subjects:
                subj_q = LegislativeSubjects.query.filter(
                    func.lower(LegislativeSubjects.subject) == subj.lower())
                loaded_subjects = subj_q.all()
                if loaded_subjects:
                    for sub in loaded_subjects:
                        bill.leg_subjects.append(sub)
                else:
                    new_sub = LegislativeSubjects()
                    new_sub.subject = subj
                    bill.leg_subjects.append(new_sub)

            # sponsors
            spon = xmldata[0].findall("sponsors")
            spon = spon[0]
            bio_id = spon[0].find("bioguideId").text
            lname = spon[0].find("lastName").text
            fname = spon[0].find("firstName").text
            state = spon[0].find("state").text
            party = spon[0].find("party").text

            if bill_type < 5:
                # Bill originated in the House of Representatives
                # Search for reps using bioguide_id
                rep_q = Representative.query.filter(
                    Representative.bioguide_id == bio_id)
            else:
                # Bill originated in the Senate
                # Search for reps using state + party lastname
                rep_q = (Representative.query.filter(
                    Representative.state == state).filter(
                        Representative.party == party).filter(
                            func.lower(Representative.lname) == lname.lower()))
            reps = rep_q.all()

            if reps:
                # representative exists in the database
                # add them as a sponsor to this bill.
                rep = reps[0]
            else:
                rep = Representative()
            rep.bioguide_id = bio_id
            rep.fname = fname.title()
            rep.lname = lname.title()
            mname = spon[0].find("middleName").text
            if mname is not None:
                rep.mname = mname.title()
            rep.state = state
            rep.party = party
            rep.active = True
            bill.sponsor = rep
            # end of not-populated clause

        # cosponsors
        bill.cosponsors = []  # clears all actions attached to this bill
        cospon = xmldata[0].findall("cosponsors")
        cospon = cospon[0]
        for c in cospon:
            bio_id = c.find("bioguideId").text
            lname = c.find("lastName").text
            fname = c.find("firstName").text
            state = c.find("state").text
            party = c.find("party").text

            if bill_type < 5:
                # Bill originated in the House of Representatives
                # Search for reps using bioguide_id
                rep_q = Representative.query.filter(
                    Representative.bioguide_id == bio_id)
            else:
                # Bill originated in the Senate
                # Search for reps using state + party lastname
                rep_q = Representative.query.filter(
                    (Representative.state == state)
                    & (Representative.party == party)
                    & (func.lower(Representative.lname) == lname.lower()))
            reps = rep_q.all()
            if reps:
                # representative exists in the database
                # add them as a cosponsor to this bill.
                rep = reps[0]
            else:
                rep = Representative()
            rep.bioguide_id = bio_id
            rep.fname = fname.title()
            rep.lname = lname.title()
            mname = c.find("middleName").text
            if mname is not None:
                rep.mname = mname.title()
            rep.state = state
            rep.party = party
            rep.active = True
            bill.cosponsors.append(rep)
            # end of cosponsor iteration
        db.session.commit()