def create_events(self, prev_role, next_role): now = datetime.datetime.now().date() from events.models import Feed, Event with Event.update(self) as E: f = Feed.PersonFeed(self.person_id) if not prev_role or not self.continues_from(prev_role): E.add("termstart", self.startdate, f) if not next_role or not next_role.continues_from(self): if self.enddate <= now: # because we're not sure of end date until it happens E.add("termend", self.enddate, f)
def build_info(): if re.match(r"\d", pk): person = get_object_or_404(Person, pk=pk) else: # support bioguide IDs for me person = get_object_or_404(Person, bioguideid=pk) # current role role = person.get_current_role() if role: active_role = True else: active_role = False try: role = person.roles.order_by('-enddate')[0] except IndexError: role = None # photo photo_path = 'data/photos/%d-100px.jpeg' % person.pk photo_credit = None if os.path.exists(photo_path): photo = '/' + photo_path with open(photo_path.replace("-100px.jpeg", "-credit.txt"), "r") as f: photo_credit = f.read().strip().split(" ", 1) else: photo = None analysis_data = analysis.load_data(person) links = [] if person.osid: links.append(("OpenSecrets", "http://www.opensecrets.org/politicians/summary.php?cid=" + person.osid)) if person.pvsid: links.append(("VoteSmart", "http://votesmart.org/candidate/" + person.pvsid)) if person.bioguideid: links.append(("Bioguide", "http://bioguide.congress.gov/scripts/biodisplay.pl?index=" + person.bioguideid)) if person.cspanid: links.append(("C-SPAN Video", "http://www.c-spanvideo.org/person/" + str(person.cspanid))) return {'person': person, 'role': role, 'active_role': active_role, 'active_congressional_role': active_role and role.role_type in (RoleType.senator, RoleType.representative), 'photo': photo, 'photo_credit': photo_credit, 'links': links, 'analysis_data': analysis_data, 'recent_bills': person.sponsored_bills.all().order_by('-introduced_date')[0:7], 'committeeassignments': get_committee_assignments(person), 'feed': Feed.PersonFeed(person.id), 'cities': get_district_cities("%s-%02d" % (role.state.lower(), role.district)) if role and role.district else None, }
def build_info(): if re.match(r"\d", pk): person = get_object_or_404(Person, pk=pk) else: # support bioguide IDs for me person = get_object_or_404(Person, bioguideid=pk) # current role role = person.get_current_role() if role: active_role = True else: active_role = False try: role = person.roles.order_by('-enddate')[0] except IndexError: role = None # photo photo_url, photo_credit = person.get_photo() # analysis analysis_data = analysis.load_data(person) has_session_stats = False if role: try: has_session_stats = role.get_most_recent_session_stats() except: pass links = [] if role.website: links.append( ("%s's Official Website" % person.lastname, role.website)) if person.twitterid: links.append(("@" + person.twitterid, "http://twitter.com/" + person.twitterid)) if person.osid: links.append( ("OpenSecrets", "http://www.opensecrets.org/politicians/summary.php?cid=" + person.osid)) if person.pvsid: links.append(("VoteSmart", "http://votesmart.org/candidate/" + person.pvsid)) if person.bioguideid: links.append( ("Bioguide", "http://bioguide.congress.gov/scripts/biodisplay.pl?index=" + person.bioguideid)) if person.cspanid: links.append( ("C-SPAN", "http://www.c-spanvideo.org/person/" + str(person.cspanid))) return { 'person': person, 'role': role, 'active_role': active_role, 'active_congressional_role': active_role and role.role_type in (RoleType.senator, RoleType.representative), 'photo': photo_url, 'photo_credit': photo_credit, 'links': links, 'analysis_data': analysis_data, 'recent_bills': person.sponsored_bills.all().order_by('-introduced_date')[0:7], 'committeeassignments': get_committee_assignments(person), 'feed': Feed.PersonFeed(person.id), 'cities': get_district_cities("%s-%02d" % (role.state.lower(), role.district)) if role and role.district else None, 'has_session_stats': has_session_stats, }
def person_details_user_view(request, pk): person = get_object_or_404(Person, pk=pk) return render_subscribe_inline(request, Feed.PersonFeed(person.id))
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