def bill_widget(request, congress, type_slug, number): bill = load_bill_from_url(congress, type_slug, number) from person.name import get_person_name if bill.sponsor: bill.sponsor.role = bill.sponsor_role # for rending name sponsor_name = None if not bill.sponsor else \ get_person_name(bill.sponsor, firstname_position='before', show_suffix=True) def get_text_info(): from billtext import load_bill_text try: return load_bill_text(bill, None, mods_only=True) except IOError: return None return { "SITE_ROOT_URL": settings.SITE_ROOT_URL, "bill": bill, "congressdates": get_congress_dates(bill.congress), "subtitle": get_secondary_bill_title(bill, bill.titles), "sponsor_name": sponsor_name, "current": bill.congress == CURRENT_CONGRESS, "dead": bill.congress != CURRENT_CONGRESS and bill.current_status not in BillStatus.final_status_obvious, "text": get_text_info, }
def bill_details(request, congress, type_slug, number): # pre-load info bill = load_bill_from_url(congress, type_slug, number) text_info = bill.get_text_info(with_citations=True) from stakeholder.models import BillPosition stakeholder_posts = bill.stakeholder_positions\ .filter(post__stakeholder__verified=True)\ .select_related("post", "post__stakeholder")\ .order_by('-created') def add_position_return_post(bp): bp.post.position = bp.position; return bp.post stakeholder_posts = [add_position_return_post(bp) for bp in stakeholder_posts] # context return { 'bill': bill, "congressdates": get_congress_dates(bill.congress), "subtitle": get_secondary_bill_title(bill, bill.titles), "current": bill.congress == CURRENT_CONGRESS, "dead": bill.congress != CURRENT_CONGRESS and bill.current_status not in BillStatus.final_status_obvious, "feed": bill.get_feed(), "prognosis": bill.get_prognosis_with_details(), "text_info": text_info, "text_incorporation": fixup_text_incorporation(bill.text_incorporation), "show_media_bar": not bill.original_intent_replaced and bill.sponsor and bill.sponsor.has_photo() and text_info and text_info.get("has_thumbnail"), "stakeholder_posts": stakeholder_posts, }
def bill_details(request, congress, type_slug, number): bill = load_bill_from_url(congress, type_slug, number) return { 'bill': bill, "congressdates": get_congress_dates(bill.congress), "subtitle": get_secondary_bill_title(bill, bill.titles), "current": bill.congress == CURRENT_CONGRESS, "dead": bill.congress != CURRENT_CONGRESS and bill.current_status not in BillStatus.final_status_obvious, "feed": bill.get_feed(), "text_info": get_text_info(bill), }
def test_title_calculation(self): bill = BillMockup(congress=112, bill_type=BillType.house_bill, number=525) # Simple test titles = (('short', 'abc', 'title1'),) self.assertEqual('H.R. 525: title1', get_primary_bill_title(bill, titles)) # Should be first title amoung titles with as="abc" titles = (('short', 'abc', 'title1'), ('short', 'abc', 'title2')) self.assertEqual('H.R. 525: title1', get_primary_bill_title(bill, titles)) # Should be title with short type titles = (('popular', 'abc', 'title1'), ('short', 'abc', 'title2')) self.assertEqual('H.R. 525: title2', get_primary_bill_title(bill, titles)) # Should be title with short type titles = (('popular', 'abc', 'title1'), ('short', 'abc', 'title2'), ('short', 'abc', 'title3')) self.assertEqual('H.R. 525: title2', get_primary_bill_title(bill, titles)) # Should be first title in group of title with as=abc2 titles = (('short', 'abc', 'title1'), ('short', 'abc', 'title2'), ('short', 'abc2', 'title3'), ('short', 'abc2', 'title4')) self.assertEqual('H.R. 525: title3', get_primary_bill_title(bill, titles)) # Should be title with official type titles = (('short', 'abc', 'title1'), ('official', 'abc', 'title2')) self.assertEqual('title2', get_secondary_bill_title(bill, titles)) # Should be None titles = (('official', 'abc', 'title2'),) self.assertEqual(None, get_secondary_bill_title(bill, titles))
def bill_details(request, congress, type_slug, number): if type_slug.isdigit(): bill_type = type_slug else: try: bill_type = BillType.by_slug(type_slug) except BillType.NotFound: raise Http404("Invalid bill type: " + type_slug) bill = get_object_or_404(Bill, congress=congress, bill_type=bill_type, number=number) from person.name import get_person_name sponsor_name = None if not bill.sponsor else \ get_person_name(bill.sponsor, role_date=bill.introduced_date, firstname_position='before', show_suffix=True) def get_reintroductions(): reintro_prev = None reintro_next = None for reintro in bill.find_reintroductions(): if reintro.congress < bill.congress: reintro_prev = reintro if reintro.congress > bill.congress and not reintro_next: reintro_next = reintro return reintro_prev, reintro_next def get_text_info(): from billtext import load_bill_text try: return load_bill_text(bill, None, mods_only=True) except IOError: return None return { 'bill': bill, "congressdates": get_congress_dates(bill.congress), "subtitle": get_secondary_bill_title(bill, bill.titles), "sponsor_name": sponsor_name, "reintros": get_reintroductions, # defer so we can use template caching "current": bill.congress == CURRENT_CONGRESS, "dead": bill.congress != CURRENT_CONGRESS and bill.current_status not in BillStatus.final_status_obvious, "feed": Feed.BillFeed(bill), "text": get_text_info, }
def bill_details(request, congress, type_slug, number): bill = load_bill_from_url(congress, type_slug, number) # get related bills related_bills = [] reintro_prev = None reintro_next = None for reintro in bill.find_reintroductions(): if reintro.congress < bill.congress: reintro_prev = reintro if reintro.congress > bill.congress and not reintro_next: reintro_next = reintro if reintro_prev: related_bills.append({ "bill": reintro_prev, "note": "was a previous version of this bill.", "show_title": False }) if reintro_next: related_bills.append({ "bill": reintro_next, "note": "was a re-introduction of this bill in a later Congress.", "show_title": False }) for rb in bill.get_related_bills(): if rb.relation in ("identical", "rule"): related_bills.append({ "bill": rb.related_bill, "note": "(%s)" % rb.relation, "show_title": False }) elif rb.relation == "ruled-by": related_bills.append({ "bill": rb.related_bill, "prenote": "Debate on", "note": " is governed by these rules.", "show_title": False }) else: related_bills.append({ "bill": rb.related_bill, "note": ("(%s)" % (rb.relation.title() if rb.relation != "unknown" else "Related")), "show_title": True }) # bill text info and areas of law affected from billtext import load_bill_text try: text_info = load_bill_text(bill, None, mods_only=True, with_citations=True) except IOError: text_info = None return { 'bill': bill, "congressdates": get_congress_dates(bill.congress), "subtitle": get_secondary_bill_title(bill, bill.titles), "current": bill.congress == CURRENT_CONGRESS, "dead": bill.congress != CURRENT_CONGRESS and bill.current_status not in BillStatus.final_status_obvious, "feed": bill.get_feed(), "text_info": text_info, "related": related_bills, }
def get_secondary_bill_title_2(bill): t = get_secondary_bill_title(bill, bill.titles) if t is None: return "" return t
def bill_details(request, congress, type_slug, number): if type_slug.isdigit(): bill_type = type_slug else: try: bill_type = BillType.by_slug(type_slug) except BillType.NotFound: raise Http404("Invalid bill type: " + type_slug) bill = get_object_or_404(Bill, congress=congress, bill_type=bill_type, number=number) from person.name import get_person_name sponsor_name = None if not bill.sponsor else \ get_person_name(bill.sponsor, role_date=bill.introduced_date, firstname_position='before', show_suffix=True) def get_reintroductions(): reintro_prev = None reintro_next = None for reintro in bill.find_reintroductions(): if reintro.congress < bill.congress: reintro_prev = reintro if reintro.congress > bill.congress and not reintro_next: reintro_next = reintro return reintro_prev, reintro_next def get_text_info(): from models import USCSection from billtext import load_bill_text from search import parse_slip_law_number import re try: metadata = load_bill_text(bill, None, mods_only=True) # do interesting stuff with citations if "citations" in metadata: slip_laws = [] statutes = [] usc = { } other = [] usc_other = USCSection(name="Other Citations", ordering=99999) for cite in metadata["citations"]: if cite["type"] == "slip_law": slip_laws.append(cite) cite["bill"] = parse_slip_law_number(cite["text"]) elif cite["type"] == "statutes_at_large": statutes.append(cite) elif cite["type"] == "usc": # build a normalized citation and a link to LII cite_norm = "usc/" + cite["title"] cite_link = "http://www.law.cornell.edu/uscode/text/" + cite["title"] if cite["section"]: cite_link += "/" + cite["section"] cite_norm += "/" + cite["section"] if cite["paragraph"]: cite_link += "#" + "_".join(re.findall(r"\(([^)]+)\)", cite["paragraph"])) # Build a tree of title-chapter-...-section nodes so we can # display the citations in context. try: sec_obj = USCSection.objects.get(citation=cite_norm) except: # USCSection.DoesNotExist and MultipleObjectsReturned both possible # the 'id' field is set to make these objects properly hashable sec_obj = USCSection(id=cite["text"], name=cite["text"], parent_section=usc_other) sec_obj.link = cite_link if "range_to_section" in cite: sec_obj.range_to_section = cite["range_to_section"] # recursively go up to the title path = [sec_obj] while sec_obj.parent_section: sec_obj = sec_obj.parent_section path.append(sec_obj) # now pop off from the path to put the node at the right point in a tree container = usc while path: p = path.pop(-1) if p not in container: container[p] = { } container = container[p] else: other.append(cite) slip_laws.sort(key = lambda x : (x["congress"], x["number"])) # restructure data format def ucfirst(s): return s[0].upper() + s[1:] def rebuild_usc_sec(seclist, indent=0): ret = [] seclist = sorted(seclist.items(), key=lambda x : x[0].ordering) for sec, subparts in seclist: ret.append({ "text": (ucfirst(sec.level_type + ((" " + sec.number) if sec.number else "") + (": " if sec.name else "")) if sec.level_type else "") + (sec.name if sec.name else ""), "link": getattr(sec, "link", None), "range_to_section": getattr(sec, "range_to_section", None), "indent": indent, }) ret.extend(rebuild_usc_sec(subparts, indent=indent+1)) return ret usc = rebuild_usc_sec(usc) metadata["citations"] = { "slip_laws": slip_laws, "statutes": statutes, "usc": usc, "other": other, "count": len(slip_laws)+len(statutes)+len(usc)+len(other) } return metadata except IOError: return None return { 'bill': bill, "congressdates": get_congress_dates(bill.congress), "subtitle": get_secondary_bill_title(bill, bill.titles), "sponsor_name": sponsor_name, "reintros": get_reintroductions, # defer so we can use template caching "current": bill.congress == CURRENT_CONGRESS, "dead": bill.congress != CURRENT_CONGRESS and bill.current_status not in BillStatus.final_status_obvious, "feed": Feed.BillFeed(bill), "text": get_text_info, }
def bill_details(request, congress, type_slug, number): bill = load_bill_from_url(congress, type_slug, number) from person.name import get_person_name sponsor_name = None if not bill.sponsor else \ get_person_name(bill.sponsor, role_date=bill.introduced_date, firstname_position='before', show_suffix=True) def get_reintroductions(): reintro_prev = None reintro_next = None for reintro in bill.find_reintroductions(): if reintro.congress < bill.congress: reintro_prev = reintro if reintro.congress > bill.congress and not reintro_next: reintro_next = reintro return reintro_prev, reintro_next def get_text_info(): from models import USCSection from billtext import load_bill_text from search import parse_slip_law_number import re try: metadata = load_bill_text(bill, None, mods_only=True) # do interesting stuff with citations if "citations" in metadata and not settings.DEBUG: slip_laws = [] statutes = [] usc = { } other = [] usc_other = USCSection(name="Other Citations", ordering=99999) for cite in metadata["citations"]: if cite["type"] == "slip_law": slip_laws.append(cite) cite["bill"] = parse_slip_law_number(cite["text"]) elif cite["type"] == "statutes_at_large": statutes.append(cite) elif cite["type"] in ("usc-section", "usc-chapter"): # Build a tree of title-chapter-...-section nodes so we can # display the citations in context. try: sec_obj = USCSection.objects.get(citation=cite["key"]) except: # USCSection.DoesNotExist and MultipleObjectsReturned both possible # create a fake entry for the sake of output # the 'id' field is set to make these objects properly hashable sec_obj = USCSection(id=cite["text"], name=cite["text"], parent_section=usc_other) if "range_to_section" in cite: sec_obj.range_to_section = cite["range_to_section"] # recursively go up to the title path = [sec_obj] so = sec_obj while so.parent_section: so = so.parent_section path.append(so) # build a link to LII if cite["type"] == "usc-section": cite_link = "http://www.law.cornell.edu/uscode/text/" + cite["title"] if cite["section"]: cite_link += "/" + cite["section"] if cite["paragraph"]: cite_link += "#" + "_".join(re.findall(r"\(([^)]+)\)", cite["paragraph"])) elif cite["type"] == "usc-chapter": cite_link = "http://www.law.cornell.edu/uscode/text/" + cite["title"] + "/" + "/".join( (so.level_type + "-" + so.number) for so in reversed(path[:-1]) ) sec_obj.link = cite_link # now pop off from the path to put the node at the right point in a tree container = usc while path: p = path.pop(-1) if p not in container: container[p] = { } container = container[p] else: other.append(cite) slip_laws.sort(key = lambda x : (x["congress"], x["number"])) # restructure data format def ucfirst(s): return s[0].upper() + s[1:] def rebuild_usc_sec(seclist, indent=0): ret = [] seclist = sorted(seclist.items(), key=lambda x : x[0].ordering) for sec, subparts in seclist: ret.append({ "text": (ucfirst(sec.level_type + ((" " + sec.number) if sec.number else "") + (": " if sec.name else "")) if sec.level_type else "") + (sec.name_recased if sec.name else ""), "link": getattr(sec, "link", None), "range_to_section": getattr(sec, "range_to_section", None), "indent": indent, }) ret.extend(rebuild_usc_sec(subparts, indent=indent+1)) return ret usc = rebuild_usc_sec(usc) metadata["citations"] = { "slip_laws": slip_laws, "statutes": statutes, "usc": usc, "other": other, "count": len(slip_laws)+len(statutes)+len(usc)+len(other) } return metadata except IOError: return None return { 'bill': bill, "congressdates": get_congress_dates(bill.congress), "subtitle": get_secondary_bill_title(bill, bill.titles), "sponsor_name": sponsor_name, "reintros": get_reintroductions, # defer so we can use template caching "current": bill.congress == CURRENT_CONGRESS, "dead": bill.congress != CURRENT_CONGRESS and bill.current_status not in BillStatus.final_status_obvious, "feed": Feed.BillFeed(bill), "text": get_text_info, "care2_category_id": { 5816: '793', # Agriculture and Food=>Health 5840: '789', # Animals=>Animal Welfare 5996: '794', # Civil Rights=>Human Rights 5991: '791', # Education=>Education 6021: '792', # Energy=>Environment & Wildlife 6038: '792', # Environmental Protection=>Environment & Wildlife 6053: '793', # Families=>Health 6130: '793', # Health=>Health 6206: '794', # Immigration=>Human Rights 6279: '792', # Public Lands/Natural Resources=>Environment & Wildlife 6321: '791', # Social Sciences=>Education 6328: '793', # Social Welfare => Health }.get(bill.get_top_term_id(), '795') # fall back to Politics category }