Esempio n. 1
0
def do_site_search(q, allow_redirect=False):
    if q.strip() == "":
        return []

    results = []

    from events.models import Feed
    if "pass" in q or "fail" in q or "vote" in q:
        results.append({
            "title":
            "Tracking Federal Legislation",
            "href":
            "/start",
            "noun":
            "feeds",
            "results": [{
                "href": f.link,
                "label": f.title,
                "obj": f,
                "feed": f,
                "secondary": False
            } for f in (
                Feed.EnactedBillsFeed(),
                Feed.ActiveBillsExceptIntroductionsFeed(),
                Feed.ComingUpFeed(),
                Feed.AllVotesFeed(),
            )]
        })

    from haystack.query import SearchQuerySet
    from events.models import Feed

    results.append({
        "title":
        "Members of Congress, Presidents, and Vice Presidents",
        "href":
        "/congress/members/all",
        "qsarg":
        "name",
        "noun":
        "Members of Congress, Presidents, or Vice Presidents",
        "results": [{
            "href": p.object.get_absolute_url(),
            "label": p.object.name,
            "obj": p.object,
            "feed": Feed.PersonFeed(p.object),
            "secondary": p.object.get_current_role() == None
        } for p in SearchQuerySet().using("person").filter(
            indexed_model_name__in=["Person"], content=q).order_by(
                '-is_currently_serving', '-score')[0:9]]
    })

    # Skipping states for now because we might want to go to the district maps or to
    # the state's main page for state legislative information.
    #import us
    #results.append(("States", "/congress/members", "most_recent_role_state", "states",
    #    sorted([{"href": "/congress/members/%s" % s, "label": us.statenames[s] }
    #        for s in us.statenames
    #        if us.statenames[s].lower().startswith(q.lower())
    #        ], key=lambda p : p["label"])))

    from committee.models import Committee
    results.append({
        "title":
        "Congressional Committees",
        "href":
        "/congress/committees",
        "noun":
        "committees in Congress",
        "results":
        sorted([{
            "href": c.get_absolute_url(),
            "label": c.fullname,
            "feed": Feed.CommitteeFeed(c),
            "obj": c,
            "secondary": c.committee != None
        } for c in Committee.objects.filter(name__icontains=q, obsolete=False)
                ],
               key=lambda c: c["label"])
    })

    from settings import CURRENT_CONGRESS
    from bill.search import parse_bill_citation
    bill = parse_bill_citation(q)
    if not bill or not allow_redirect:
        from haystack.inputs import AutoQuery
        bills = [\
            {"href": b.object.get_absolute_url(),
             "label": b.object.title,
             "obj": b.object,
             "feed": Feed.BillFeed(b.object) if b.object.is_alive else None,
             "secondary": b.object.congress != CURRENT_CONGRESS }
            for b in SearchQuerySet().using("bill").filter(indexed_model_name__in=["Bill"], content=AutoQuery(q)).order_by('-current_status_date')[0:9]]
    else:
        #bills = [{"href": bill.get_absolute_url(), "label": bill.title, "obj": bill, "secondary": bill.congress != CURRENT_CONGRESS }]
        return HttpResponseRedirect(bill.get_absolute_url())
    results.append({
        "title": "Bills and Resolutions (Federal)",
        "href": "/congress/bills/browse",
        "qsarg": "congress=__ALL__&text",
        "noun": "federal bills or resolutions",
        "results": bills
    })

    results.append({
        "title":
        "State Legislation",
        "href":
        "/states/bills/browse",
        "qsarg":
        "text",
        "noun":
        "state legislation",
        "results": [{
            "href": p.object.get_absolute_url(),
            "label": p.object.short_display_title,
            "obj": p.object,
            "feed": Feed(feedname="states_bill:%d" % p.object.id),
            "secondary": True
        } for p in SearchQuerySet().using('states').filter(
            indexed_model_name__in=["StateBill"], content=q)[0:9]]
    })

    # subject terms, but exclude subject terms that look like committee names because
    # that is confusing to also see with committee results
    from bill.models import BillTerm, TermType
    results.append({
        "title":
        "Subject Areas (Federal Legislation)",
        "href":
        "/congress/bills",
        "noun":
        "subject areas",
        "results": [{
            "href": p.get_absolute_url(),
            "label": p.name,
            "obj": p,
            "feed": Feed.IssueFeed(p),
            "secondary": not p.is_top_term()
        } for p in BillTerm.objects.filter(
            name__icontains=q, term_type=TermType.new).exclude(
                name__contains=" Committee on ")[0:9]]
    })

    # in each group, make sure the secondary results are placed last, but otherwise preserve order
    for grp in results:
        for i, obj in enumerate(grp["results"]):
            obj["index"] = i
        grp["results"].sort(
            key=lambda o: (o.get("secondary", False), o["index"]))

    # sort categories first by whether all results are secondary results, then by number of matches (fewest first, if greater than zero)
    results.sort(key=lambda c: (len([
        d for d in c["results"] if d.get("secondary", False) == False
    ]) == 0, len(c["results"]) == 0, len(c["results"])))

    return results