Exemplo n.º 1
0
def getMemberVotesAssemble(icpsr=0, qtext="", skip=0):
    icpsr = defaultValue(bottle.request.params.icpsr, 0)
    qtext = defaultValue(bottle.request.params.qtext, "")
    skip = defaultValue(bottle.request.params.skip, 0)

    if not icpsr:
        output = bottle.template("views/error",
                                 errorMessage="No member specified.")
        bottle.response.headers["nextId"] = 0
        return (output)

    person = memberLookup({"icpsr": icpsr})
    if not "error" in person:
        person = person["results"][0]
    else:
        output = bottle.template("views/error",
                                 errorMessage=person["errormessage"])
        bottle.response.headers["nextId"] = 0
        return (output)

    votes = []

    if qtext:
        qtext = qtext + " AND (voter: " + str(person["icpsr"]) + ")"
    else:
        qtext = "voter: " + str(person["icpsr"])

    if skip:
        voteQuery = query(qtext,
                          rowLimit=25,
                          jsapi=1,
                          sortSkip=skip,
                          request=bottle.request)
    else:
        voteQuery = query(qtext, rowLimit=25, jsapi=1, request=bottle.request)

    # Outsourced the vote assembly to a model for future API buildout.
    votes = prepVotes(voteQuery, person)
    output = bottle.template("views/member_votes",
                             person=person,
                             votes=votes,
                             skip=skip,
                             nextId=voteQuery["nextId"])

    bottle.response.headers["nextId"] = voteQuery["nextId"]
    return (output)
Exemplo n.º 2
0
def search():
    q = defaultValue(bottle.request.params.q)
    startdate = defaultValue(bottle.request.params.startdate)
    enddate = defaultValue(bottle.request.params.enddate)
    chamber = defaultValue(bottle.request.params.chamber)
    icpsr = defaultValue(bottle.request.params.icpsr)
    rapi = defaultValue(bottle.request.params.rapi,0)
    res = query(q,startdate,enddate,chamber, icpsr=icpsr, rapi=rapi)
    return(res)
Exemplo n.º 3
0
def getMemberVotesAssemble(icpsr=0, qtext="", skip=0):
    icpsr = defaultValue(bottle.request.params.icpsr, 0)
    qtext = defaultValue(bottle.request.params.qtext, "")
    skip = defaultValue(bottle.request.params.skip, 0)

    if not icpsr:
        output = bottle.template(
            "views/error", errorMessage="No member specified.")
        bottle.response.headers["nextId"] = 0
        return(output)

    person = memberLookup({"icpsr": icpsr})
    if not "error" in person:
        person = person["results"][0]
    else:
        output = bottle.template(
            "views/error", errorMessage=person["errormessage"])
        bottle.response.headers["nextId"] = 0
        return(output)

    votes = []

    if qtext:
        qtext = qtext + " AND (voter: " + str(person["icpsr"]) + ")"
    else:
        qtext = "voter: " + str(person["icpsr"])

    if skip:
        voteQuery = query(qtext, rowLimit=25, jsapi=1,
                          sortSkip=skip, request=bottle.request)
    else:
        voteQuery = query(qtext, rowLimit=25, jsapi=1, request=bottle.request)

    # Outsourced the vote assembly to a model for future API buildout.
    votes = prepVotes(voteQuery, person)
    output = bottle.template("views/member_votes", person=person,
                             votes=votes, skip=skip, nextId=voteQuery["nextId"])

    bottle.response.headers["nextId"] = voteQuery["nextId"]
    return(output)
Exemplo n.º 4
0
def search():
    q = defaultValue(bottle.request.params.q)
    startdate = defaultValue(bottle.request.params.startdate)
    enddate = defaultValue(bottle.request.params.enddate)
    chamber = defaultValue(bottle.request.params.chamber)
    icpsr = defaultValue(bottle.request.params.icpsr)
    rapi = defaultValue(bottle.request.params.rapi, 0)
    res = query(q,
                startdate,
                enddate,
                chamber,
                icpsr=icpsr,
                rapi=rapi,
                request=bottle.request)
    return (res)
Exemplo n.º 5
0
def searchAssemble():
    q = defaultValue(bottle.request.params.q)
    nextId = defaultValue(bottle.request.params.nextId,0)

    #Party search
    resultParties = []
    if q is not None and not nextId and not ":" in q and len(q.split())<4 and len(q):
        try:
            testQ = int(q)
            if testQ>0 and testQ<10000:
                partySearch = partyLookup({"id": q}, api="Web_FP_Search")
            else:
                partySearch = {}
        except:
            partySearch = partyLookup({"name": q}, api="Web_FP_Search")
        if "results" in partySearch:
            for party in partySearch["results"]:
                party["scoreMatch"] = fuzz.token_set_ratio(party["fullName"].lower().replace(" party",""), q.lower().replace(" party",""))
		if party["count"] > 1000:
			party["scoreMatch"] += 25
		elif party["count"] > 100:
			party["scoreMatch"] += 10
                resultParties.append(party)
	resultParties.sort(key=lambda x: (-x["scoreMatch"], -x["maxCongress"]))

    # Member search
    resultMembers = []
    needScore=1
    redirFlag=0
    expandResults=0
    if q is not None and not nextId and not ":" in q and len(q.split())<5 and len(q):
        try:
            if len(q.split())==1 and (q.upper().startswith("MH") or q.upper().startswith("MS")):
                memberSearch = memberLookup({"id": q}, 8, distinct=1, api="Web_FP_Search")
            elif q.strip().lower() in ["speaker of the house","speakers of the house","speaker: 1", "speaker:1","house speaker"]:
                memberSearch = memberLookup({"speaker": 1, "chamber": "house"}, 60, distinct=1, api="Web_FP_Search")
                needScore=0
                expandResults=1
            elif q.strip().lower() in ["potus", "president of the united states", "president", "the president", "president:1", "president: 1","presidents","presidents of the united states","presidents of the united states of america","president of the united states of america"]:
                memberSearch = memberLookup({"chamber": "President"}, 50, distinct=1, api="Web_FP_Search")
                needScore=0
                expandResults=1
            elif len(q.split())==1 and int(q):
                memberSearch = memberLookup({"icpsr": int(q)}, 5, distinct=1, api="Web_FP_Search")
                redirFlag=1
            else:
                memberSearch = memberLookup({"name": q}, 40, distinct=1, api="Web_FP_Search")
        except:
                memberSearch = memberLookup({"name": q}, 40, distinct=1, api="Web_FP_Search")
        if "results" in memberSearch:
            for member in memberSearch["results"]:
                memName = ""
                if "bioname" in member and member["bioname"] is not None:
                    memName = member["bioname"]
                elif "fname" in member and member["fname"] is not None:
                    memName = member["fname"]
                else:
                    try:
                        memName = member["name"]
                    except:
                        memName = "Error, Invalid Name."

                try:
                    memName = memName.replace(",","").lower()
                except:
                    memName = memName.lower()

                searchNameToScore = q.replace(",","").lower()
                scoreBasic = fuzz.token_set_ratio(memName, q.replace(",","").lower()) # Score base search
                scoreNick = fuzz.token_set_ratio(nicknameHelper(memName, searchNameToScore), nicknameHelper(searchNameToScore)) # Come up with a best nickname match
                member["scoreMatch"] = max(scoreBasic, scoreNick)
		member["bonusMatch"] = 0
		print q, "/", memName, "/", scoreBasic, scoreNick
                if member["congress"]>=100:
                    member["bonusMatch"] += 10
                if member["chamber"]=="President":
                    member["bonusMatch"] += 25
                if member["chamber"]=="Senate":
                    member["bonusMatch"] += 10
                if "congresses" in member:
                    duration = 0
                    for cong in member["congresses"]:
                        duration = duration+(cong[1]-cong[0])
                    if duration>=5:
                        member["bonusMatch"] += 7

                if not os.path.isfile("static/img/bios/"+str(member["icpsr"]).zfill(6)+".jpg"):
                    member["bioImg"] = "silhouette.png"
                else:
                    member["bioImg"] = str(member["icpsr"]).zfill(6)+".jpg"
                member["minElected"] = congressToYear(member["congresses"][0][0], 0)

                resultMembers.append(member)

    #return(resultMembers)
    if needScore:
        print "results before truncation"
        print resultMembers
        print "end ====="
        if len(resultMembers) and resultMembers[0]["scoreMatch"]>=100:
            resultMembers = [x for x in resultMembers if x["scoreMatch"]>=100]
        resultMembers.sort(key=lambda x: -(x["scoreMatch"] + x["bonusMatch"]))
    else:
        resultMembers.sort(key=lambda x: -x["congress"])
    if len(resultMembers)>8 and not expandResults:
        resultMembers=resultMembers[0:8]

    # Date facet
    startdate = defaultValue(bottle.request.params.fromDate)
    enddate = defaultValue(bottle.request.params.toDate)

    # Chamber facet
    try:
        chamber = bottle.request.params.getall("chamber")
        if len(chamber)>1:
            chamber = None
        elif type(chamber)==type([]):
            chamber = chamber[0]
    except:
        chamber = None

    # Congress facet
    try:
        fromCongress = int(defaultValue(bottle.request.params["fromCongress"],0))
        toCongress = int(defaultValue(bottle.request.params["toCongress"],0))
        if (q is None or q=="") and (fromCongress or toCongress):
            q = ""

        if fromCongress or toCongress:
            if fromCongress == toCongress:
                q = q + " congress:"+str(fromCongress)
            elif fromCongress and not toCongress:
                q = q + " congress:["+str(fromCongress)+" to ]"
            elif toCongress and not fromCongress:
                q = q + " congress:[ to "+str(toCongress)+"]"
            else:
                q = q + " congress:["+str(fromCongress)+" to "+str(toCongress)+"]"
    except:
        pass

    # Support facet
    try:
        support = bottle.request.params["support"]
        if (q is None or q=="") and (support):
            q = ""

        if "," in support:
            try:
                valMin, valMax = [int(x) for x in support.split(",")]
                if valMin!=0 or valMax!=100:
                    q = q + " support:["+str(valMin)+" to "+str(valMax)+"]"
            except:
                pass
        else:
            try:
                support = int(support)
                q = q + " support:["+str(support-1)+" to "+str(support+1)+"]"
            except:
                pass
    except:
        pass

    # Code facet
    try:
        clausen = bottle.request.params.getall("clausen")
    except:
        clausen = []

    try:
	keyvote = bottle.request.params.getall("keyvote")
	if len(keyvote):
		if q is None or q=="":
			q = "keyvote: 1"
		else:
			q += " keyvote: 1"
	else:
		pass
    except:
	pass

    try:
        peltzman = bottle.request.params.getall("peltzman")
    except:
        peltzman = []
    codeString = ""
    if len(clausen):
        for cCode in clausen:
            codeString += "codes.Clausen: "+cCode+" OR "
    if len(peltzman):
        for pCode in peltzman:
            codeString += "codes.Peltzman: "+pCode+" OR "
    if len(codeString):
        codeString = codeString[0:-4]
        if q is None or q=="":
            q = codeString
        else:
            q += " ("+codeString+")"

    # Sort facet
    sortD = int(defaultValue(bottle.request.params.sortD,-1))
    try:
        if sortD!=-1 and sortD!=1:
            sortD = -1
    except:
        sortD = -1

    sortScore = int(defaultValue(bottle.request.params.sortScore,1))
    icpsr = defaultValue(bottle.request.params.icpsr)
    jsapi = 1
    rowLimit = 50
    res = query(q, startdate, enddate, chamber, icpsr=icpsr, rowLimit=rowLimit, jsapi=jsapi, sortDir=sortD, sortSkip=nextId, sortScore=sortScore)

    if "errormessage" in res:
        bottle.response.headers["rollcall_number"] = -999
        bottle.response.headers["member_number"] = 0
        bottle.response.headers["nextId"] = 0
        out = bottle.template("views/search_results", rollcalls = [], errormessage=res["errormessage"], resultMembers=resultMembers, resultParties=resultParties)
    else:
        if "fulltextSearch" in res:
            highlighter = res["fulltextSearch"]
        else:
            highlighter = ""

        if redirFlag==1 and len(resultParties)==0 and len(resultMembers)==1 and (not "rollcalls" in res or len(res["rollcalls"])==0):
            bottle.response.headers["redirect_url"] = "/person/"+str(resultMembers[0]["icpsr"])
        bottle.response.headers["rollcall_number"] = res["recordcountTotal"]
        bottle.response.headers["member_number"] = len(resultMembers)
	bottle.response.headers["party_number"] = len(resultParties)
        bottle.response.headers["nextId"] = res["nextId"]
        bottle.response.headers["need_score"] = res["needScore"]
        if not "rollcalls" in res:
            out = bottle.template("views/search_results", rollcalls = [], errormessage="", resultMembers=resultMembers, resultParties=resultParties)
        else:
            #print(res['rollcalls'])
            out = bottle.template("views/search_results", rollcalls = res["rollcalls"], highlighter=highlighter, errormessage="", resultMembers=resultMembers, resultParties=resultParties) 
    return(out)
Exemplo n.º 6
0
def person(icpsr=0):
    clearTime()
    timeIt("begin")
    if not icpsr:
        icpsr = defaultValue(bottle.request.params.icpsr,0)

    skip = 0

    # Easter Egg
    keith = defaultValue(bottle.request.params.keith, 0)

    # Pull by ICPSR
    person = memberLookup({"icpsr": icpsr}, 1)
    timeIt("memberLookup")

    # If we have no error, follow through
    if not "errormessage" in person:
        person = person["results"][0]
	if not "bioname" in person:
		person["bioname"] = "ERROR NO NAME IN DATABASE PLEASE FIX."
        votes = []
        # Look up votes

        timeIt("nameFunc")

        # Check if bio image exists
        bioFound = 0
        if not os.path.isfile("static/img/bios/"+str(person["icpsr"]).zfill(6)+".jpg"):
            # If not, use the default silhouette
            if not keith:
                person["bioImg"] = "silhouette.png"
            else:
                person["bioImg"] = "keith.png"
        else:
            person["bioImg"] = str(person["icpsr"]).zfill(6)+".jpg"
            bioFound = 1

        timeIt("bioImg")

        # Get years of service
        person["yearsOfService"] = yearsOfService(person["icpsr"],"")
	person["yearsOfServiceSenate"] = yearsOfService(person["icpsr"],"Senate")
	person["yearsOfServiceHouse"] = yearsOfService(person["icpsr"],"House")
        person["congressesOfService"] = congressesOfService(person["icpsr"],"")
        person["congressLabels"] = {}
        for congressChunk in person["congressesOfService"]:
            for cong in range(congressChunk[0], congressChunk[1]+1):
                person["congressLabels"][cong] = str(cong)+"th Congress ("+str(congressToYear(cong,0))+"-"+str(congressToYear(cong,1))+")"

        timeIt("congressLabels")

        # Find out if we have any other ICPSRs that are this person for another party
        altICPSRs = checkForPartySwitch(person)
        if "results" in altICPSRs:
            person["altPeople"] = []
            # Iterate through them
            for alt in altICPSRs["results"]:
                # Look up this one
                altPerson = memberLookup({"icpsr": alt}, 1)["results"][0]
                if not "errormessage" in altPerson:
                    # Get their years of service
                    altPerson["yearsOfService"] = yearsOfService(altPerson["icpsr"])
                    # If we don't have a bio image for main guy, borrow from his previous/subsequent incarnations
                    if not bioFound and os.path.isfile("static/img/bios/"+str(altPerson["icpsr"]).zfill(6)+".jpg"):
                        person["bioImg"] = str(altPerson["icpsr"]).zfill(6)+".jpg"
                        bioFound = 1
                    person["altPeople"].append(altPerson)


        timeIt("partySwitches")
        voteQuery = query(qtext="voter: "+str(person["icpsr"]), rowLimit=25, jsapi=1)
        timeIt("gotVotes")

	votes = prepVotes(voteQuery, person) # Outsourced the vote assembly to a model for future API buildout.

        if "biography" in person:
            person["biography"] = person["biography"].replace("a Representative","Representative")

        timeIt("readyOut")
        # Go to the template.
        output = bottle.template("views/person",person=person, votes=votes, timeSet=zipTimes(), skip=0, nextId=voteQuery["nextId"], voteQuery=voteQuery)
        return(output)

    # If we have an error, return an error page
    else:
        output = bottle.template("views/error", errorMessage=person["errormessage"])
        return(output)