def context(self, http_client, request): try: sections = json.load(open(os.path.join(settings.GUIDANCE_DIR, '%s.json' % self.label_id))) t = select_template([ 'guidance/%s.html' % self.label_id, 'guidance/default.html', ]) return { 'subtemplate': t.render({ 'human_label_id': label_to_text( self.label_parts, include_marker=True), 'sections': sections, }), } except FileNotFoundError as e: pass except Exception as e: logger.error(e) t = get_template('guidance/blank.html') return { 'subtemplate': t.render({ 'human_label_id': label_to_text( self.label_parts, include_marker=True), }), }
def test_label_to_text(self): expectations = [ (['2323', '4'], '2323.4'), (['2323', '5', 'r', '3'], '2323.5(r)(3)'), (['23', '5', 'r', '3', 'i', 'p12', 'A'], '23.5(r)(3)(i)'), (['23', '5', 'p1', 'a'], '23.5'), (['2323', 'A'], 'Appendix A to Part 2323'), (['2323', 'A', '4'], 'Appendix A-4'), (['2323', 'A', '4', 'b', '2'], 'Appendix A-4(b)(2)'), (['2323', '5', 'Interp'], 'Supplement to 2323.5'), (['2323', '7', 'b', 'Interp', '1', 'v'], 'Supplement to 2323.7(b)-1.v'), (['2323', 'Z', 'Interp'], 'Supplement to Appendix Z to Part 2323'), (['204'], 'Regulation 204'), (['204', 'Interp'], 'Supplement I to Part 204'), (['204', 'Subpart', 'Interp'], 'Interpretations for Regulation Text of Part 204'), (['204', 'Subpart', 'C', 'Interp'], 'Interpretations for Subpart C of Part 204'), (['204', 'Appendices', 'Interp'], 'Interpretations for Appendices of Part 204'), (['204', 'Interp', 'h1'], 'This Section'), (['204', 'M2'], 'Appendix M2 to Part 204')] for label, expected_text in expectations: self.assertEqual(expected_text, node_types.label_to_text(label)) self.assertEqual('4', node_types.label_to_text(['2323', '4'], False)) self.assertEqual( '5(r)(3)', node_types.label_to_text(['2323', '5', 'r', '3'], False)) self.assertEqual(u'§ 2323.1', node_types.label_to_text(['2323', '1'], True, True)) self.assertEqual(u'§ 1', node_types.label_to_text(['2323', '1'], False, True))
def transform_context(self, context, builder): context['node'] = builder.tree context['formatted_label'] = label_to_text( builder.tree['label'], True, True) context['node']['section_id'] = '%s-%s' % ( builder.tree['label'][0], builder.tree['label'][1]) return context
def apply_layer(self, text_index): """Return a pair of field-name + interpretation if one applies.""" if text_index in self.layer and self.layer[text_index]: context = { "interps": [], "for_markup_id": text_index, "for_label": label_to_text(text_index.split("-"), include_section=False), } # Force caching of a few nodes up -- should prevent a request # per interpretation if caching is on generator.generator.get_tree_paragraph(self.root_interp_label, self.version) for layer_element in self.layer[text_index]: reference = layer_element["reference"] request = HttpRequest() request.method = "GET" response = self.partial_view(request, label_id=reference, version=self.version) response.render() interp = {"label_id": reference, "markup": response.content} ref_parts = reference.split("-") interp["section_id"] = self.section_url.interp(ref_parts, self.version) context["interps"].append(interp) return "interp", context
def apply_layer(self, text_index): """Return a pair of field-name + interpretation if one applies.""" if text_index in self.layer and self.layer[text_index]: context = { 'interps': [], 'for_markup_id': text_index, 'for_label': label_to_text(text_index.split('-'), include_section=False) } for layer_element in self.layer[text_index]: reference = layer_element['reference'] partial_view = views.partial_interp.PartialInterpView.as_view( inline=True) request = HttpRequest() request.GET['layers'] = 'terms,internal,keyterms,paragraph' request.method = 'GET' response = partial_view(request, label_id=reference, version=self.version) response.render() interp = { 'label_id': reference, 'markup': response.content, } ref_parts = reference.split('-') interp['section_id'] = self.section_url.interp( ref_parts, self.version) context['interps'].append(interp) return 'interp', context
def get_context_data(self, **kwargs): context = super(ParagraphSXSView, self).get_context_data(**kwargs) label_id = context['label_id'] notice_id = context['notice_id'] fr_page = context.get('fr_page') notice = generator.get_notice(notice_id) if not notice: raise error_handling.MissingContentException() notice = convert_to_python(notice) paragraph_sxs = generator.get_sxs(label_id, notice, fr_page) if paragraph_sxs is None: raise error_handling.MissingContentException() notices.add_depths(paragraph_sxs, 3) paragraph_sxs['children'] =\ notices.filter_labeled_children(paragraph_sxs) self.footnote_refs(paragraph_sxs) context['sxs'] = paragraph_sxs # Template assumes a single label context['sxs']['label'] = context['label_id'] context['sxs']['header'] = label_to_text(label_id.split('-'), include_marker=True) context['sxs']['all_footnotes'] = self.footnotes(notice, paragraph_sxs) context['notice'] = notice context['further_analyses'] = self.further_analyses( label_id, notice_id, paragraph_sxs['page'], context['version']) return context
def get_context_data(self, **kwargs): context = super(ChromeView, self).get_context_data(**kwargs) label_id = context['label_id'] version = context['version'] label_id_list = label_id.split('-') reg_part = label_id_list[0] context['q'] = self.request.GET.get('q', '') context['formatted_id'] = label_to_text(label_id_list, True, True) context['node_type'] = type_from_label(label_id_list) error_handling.check_regulation(reg_part) try: self.set_chrome_context(context, reg_part, version) self.check_tree(context) self.add_main_content(context) except (IndexError, TypeError): raise Http404 if self.has_sidebar: sidebar_view = SideBarView.as_view() response = sidebar_view(self.request, label_id=label_id, version=version) self._assert_good(response) response.render() context['sidebar_content'] = response.content return context
def apply_layer(self, text_index): """Return a pair of field-name + interpretation if one applies.""" if text_index in self.layer and self.layer[text_index]: context = {'interps': [], 'for_markup_id': text_index, 'for_label': label_to_text(text_index.split('-'), include_section=False)} # Force caching of a few nodes up -- should prevent a request # per interpretation if caching is on generator.generator.get_tree_paragraph( self.root_interp_label, self.version) for layer_element in self.layer[text_index]: reference = layer_element['reference'] request = HttpRequest() request.method = 'GET' response = self.partial_view(request, label_id=reference, version=self.version) response.render() interp = { 'label_id': reference, 'markup': response.content, } ref_parts = reference.split('-') interp['section_id'] = self.section_url.interp( ref_parts, self.version) context['interps'].append(interp) return 'interp', context
def transform_context(self, context, builder): context['node'] = builder.tree context['formatted_label'] = label_to_text(builder.tree['label'], True, True) context['node']['section_id'] = '%s-%s' % (builder.tree['label'][0], builder.tree['label'][1]) return context
def attach_metadata(self, node): """Return a pair of field-name + interpretation if one applies.""" text_index = node['label_id'] if text_index in self.layer and self.layer[text_index]: context = {'interps': [], 'for_markup_id': text_index, 'for_label': label_to_text(text_index.split('-'), include_section=False)} # Force caching of a few nodes up -- should prevent a request # per interpretation if caching is on generator.generator.get_tree_paragraph( self.root_interp_label, self.version) for layer_element in self.layer[text_index]: reference = layer_element['reference'] request = HttpRequest() request.method = 'GET' response = self.partial_view(request, label_id=reference, version=self.version) response.render() interp = { 'label_id': reference, 'markup': response.content, } ref_parts = reference.split('-') interp['section_id'] = self.section_url.interp( ref_parts, self.version) context['interps'].append(interp) node['interp'] = context
def apply_layer(self, text_index): """Return a pair of field-name + interpretation if one applies.""" if text_index in self.layer and self.layer[text_index]: context = {'interps': [], 'for_markup_id': text_index, 'for_label': label_to_text(text_index.split('-'), include_section=False)} for layer_element in self.layer[text_index]: reference = layer_element['reference'] partial_view = views.partial_interp.PartialInterpView.as_view( inline=True) request = HttpRequest() request.GET['layers'] = 'terms,internal,keyterms,paragraph' request.method = 'GET' response = partial_view(request, label_id=reference, version=self.version) response.render() interp = { 'label_id': reference, 'markup': response.content, } ref_parts = reference.split('-') interp['section_id'] = self.section_url.interp( ref_parts, self.version) context['interps'].append(interp) return 'interp', context
def get_context_data(self, **kwargs): context = super(ParagraphSXSView, self).get_context_data(**kwargs) label_id = context["label_id"] notice_id = context["notice_id"] fr_page = context.get("fr_page") notice = generator.get_notice(notice_id) if not notice: raise error_handling.MissingContentException() notice = convert_to_python(notice) paragraph_sxs = generator.get_sxs(label_id, notice, fr_page) if paragraph_sxs is None: raise error_handling.MissingContentException() notices.add_depths(paragraph_sxs, 3) paragraph_sxs["children"] = notices.filter_labeled_children(paragraph_sxs) self.footnote_refs(paragraph_sxs) context["sxs"] = paragraph_sxs # Template assumes a single label context["sxs"]["label"] = context["label_id"] context["sxs"]["header"] = label_to_text(label_id.split("-"), include_marker=True) context["sxs"]["all_footnotes"] = self.footnotes(notice, paragraph_sxs) context["notice"] = notice context["further_analyses"] = self.further_analyses( label_id, notice_id, paragraph_sxs["page"], context["version"] ) return context
def to_template_dict(self, key): """Take the reference associated with this SxS and turn it into data which can be used in the template""" # We care only about the latest doc_number, label_id = self.layer[key][-1]['reference'] return [{'doc_number': doc_number, 'label_id': label_id, 'text': label_to_text(label_id.split('-'), include_section=False, include_marker=True)}]
def template_context(self): return { 'doc_number': self.doc_number, 'label_id': self.label.id, 'text': label_to_text(self.label.parts, include_section=False, include_marker=True) }
def get_context_data(self, **kwargs): # We don't want to run the content data of PartialView -- it assumes # we will be applying layers context = super(PartialView, self).get_context_data(**kwargs) context['q'] = self.request.GET.get('q') context['version'] = self.request.GET.get('version') context['regulation'] = context['label_id'].split('-')[0] try: page = int(self.request.GET.get('page', '0')) except ValueError: page = 0 api_page = page // (API_PAGE_SIZE / PAGE_SIZE) context['warnings'] = [] if not context['q']: context['warnings'].append('Please provide a query.') if not context['version']: context['warnings'].append('Please provide a version.') if context['warnings']: results = {'results': [], 'total_hits': 0} else: results = api_reader.ApiReader().search(context['q'], context['version'], context['regulation'], api_page) self.reduce_results(results, page) section_url = SectionUrl() for result in results['results']: result['header'] = node_types.label_to_text(result['label']) if 'title' in result: result['header'] += ' ' + title(result['title']) result['section_id'] = section_url.view_label_id( result['label'], context['version']) result['url'] = section_url.fetch(result['label'], context['version'], sectional=True) context['results'] = results for version in fetch_grouped_history(context['regulation']): for notice in version['notices']: if notice['document_number'] == context['version']: context['version_by_date'] = notice['effective_on'] self.add_prev_next(page, context) self.final_context = context return context
def process_cfr_results(results, version): """Modify the results of a search over the CFR by adding a human-readable label, appropriate links, and version information""" section_url = SectionUrl() results = deepcopy(results) for result in results["results"]: result["header"] = node_types.label_to_text(result["label"]) if "title" in result: result["header"] += " " + title(result["title"]) result["section_id"] = section_url.view_label_id(result["label"], version) result["url"] = section_url.fetch(result["label"], version, sectional=True) return results
def get_context_data(self, **kwargs): context = super(SideBarView, self).get_context_data(**kwargs) label = context['label_id'].split('-') context['human_label_id'] = label_to_text(label, include_marker=True) client = api_reader.ApiReader() node_trees = self._get_node_trees(client, label, context['version']) self.add_sxs(client, node_trees, context) return context
def get_context_data(self, **kwargs): # We don't want to run the content data of PartialView -- it assumes # we will be applying layers context = super(PartialView, self).get_context_data(**kwargs) context['regulation'] = context['label_id'].split('-')[0] try: page = int(self.request.GET.get('page', '0')) except ValueError: page = 0 # API page size is API_PAGE_SIZE, but we show only PAGE_SIZE api_page = page // (API_PAGE_SIZE / PAGE_SIZE) page_idx = (page % (API_PAGE_SIZE / PAGE_SIZE)) * PAGE_SIZE results = api_reader.ApiReader().search(context['q'], context['version'], context['regulation'], api_page) # Ignore results found in the root (i.e. not a section), adjust # the number of results accordingly. original_count = len(results['results']) results['results'] = [ r for r in results['results'] if len(r['label']) > 1 ] num_results_ignored = original_count - len(results['results']) results['total_hits'] -= num_results_ignored results['results'] = results['results'][page_idx:page_idx + PAGE_SIZE] section_url = SectionUrl() for result in results['results']: result['header'] = label_to_text(result['label']) if 'title' in result: result['header'] += ' ' + title(result['title']) result['section_id'] = section_url.view_label_id( result['label'], context['version']) result['url'] = section_url.fetch(result['label'], context['version'], sectional=True) context['results'] = results for version in fetch_grouped_history(context['regulation']): for notice in version['notices']: if notice['document_number'] == context['version']: context['version_by_date'] = notice['effective_on'] self.add_prev_next(page, context) self.final_context = context return context
def get_context_data(self, **kwargs): # We don't want to run the content data of PartialView -- it assumes # we will be applying layers context = super(PartialView, self).get_context_data(**kwargs) context['q'] = self.request.GET.get('q') context['version'] = self.request.GET.get('version') context['regulation'] = context['label_id'].split('-')[0] try: page = int(self.request.GET.get('page', '0')) except ValueError: page = 0 api_page = page // (API_PAGE_SIZE / PAGE_SIZE) context['warnings'] = [] if not context['q']: context['warnings'].append('Please provide a query.') if not context['version']: context['warnings'].append('Please provide a version.') if context['warnings']: results = {'results': [], 'total_hits': 0} else: results = api_reader.ApiReader().search( context['q'], context['version'], context['regulation'], api_page) self.reduce_results(results, page) section_url = SectionUrl() for result in results['results']: result['header'] = node_types.label_to_text(result['label']) if 'title' in result: result['header'] += ' ' + title(result['title']) result['section_id'] = section_url.view_label_id( result['label'], context['version']) result['url'] = section_url.fetch( result['label'], context['version'], sectional=True) context['results'] = results for version in fetch_grouped_history(context['regulation']): for notice in version['notices']: if notice['document_number'] == context['version']: context['version_by_date'] = notice['effective_on'] self.add_prev_next(page, context) self.final_context = context return context
def add_cfr_headers(result): """We always want a title to click, even if the search result doesn't have one. We also want to prevent duplication, so we'll only show additional levels of headings if they differ.""" if result.get('title'): result['header'] = result['title'] else: result['header'] = node_types.label_to_text(result['label']) if result.get('match_title') and result['match_title'] != result['title']: result['subheader'] = result['match_title'] if (result.get('paragraph_title') and result['paragraph_title'] != result['match_title']): result['subsubheader'] = result['paragraph_title'] return result
def process_cfr_results(results, version): """Modify the results of a search over the CFR by adding a human-readable label, appropriate links, and version information""" section_url = SectionUrl() results = deepcopy(results) for result in results['results']: result['header'] = node_types.label_to_text(result['label']) if 'title' in result: result['header'] += ' ' + title(result['title']) result['section_id'] = section_url.view_label_id( result['label'], version) result['url'] = section_url.fetch(result['label'], version, sectional=True) return results
def to_template_dict(self, key): """Take the reference associated with this SxS and turn it into data which can be used in the template""" # We care only about the latest doc_number, label_id = self.layer[key][-1]['reference'] return [{ 'doc_number': doc_number, 'label_id': label_id, 'text': label_to_text(label_id.split('-'), include_section=False, include_marker=True) }]
def get_context_data(self, **kwargs): # We don't want to run the content data of PartialView -- it assumes # we will be applying layers context = super(PartialView, self).get_context_data(**kwargs) context['regulation'] = context['label_id'].split('-')[0] try: page = int(self.request.GET.get('page', '0')) except ValueError: page = 0 # API page size is API_PAGE_SIZE, but we show only PAGE_SIZE api_page = page // (API_PAGE_SIZE/PAGE_SIZE) page_idx = (page % (API_PAGE_SIZE/PAGE_SIZE)) * PAGE_SIZE results = api_reader.ApiReader().search( context['q'], context['version'], context['regulation'], api_page) # Ignore results found in the root (i.e. not a section), adjust # the number of results accordingly. original_count = len(results['results']) results['results'] = [r for r in results['results'] if len(r['label']) > 1] num_results_ignored = original_count - len(results['results']) results['total_hits'] -= num_results_ignored results['results'] = results['results'][page_idx:page_idx + PAGE_SIZE] for result in results['results']: result['header'] = label_to_text(result['label']) if 'title' in result: result['header'] += ' ' + title(result['title']) if 'Interp' in result['label']: result['section_id'] = '%s-%s' % (result['label'][0], 'Interp') else: result['section_id'] = '-'.join(result['label'][:2]) context['results'] = results for version in fetch_grouped_history(context['regulation']): for notice in version['notices']: if notice['document_number'] == context['version']: context['version_by_date'] = notice['effective_on'] self.add_prev_next(page, context) self.final_context = context return context
def get_context_data(self, **kwargs): # Skip ChromeView's implementation context = super(ChromeView, self).get_context_data(**kwargs) context["reg_part"] = context["label_id"].split("-")[0] context["version"] = self.request.GET.get("from_version") meta = api_reader.ApiReader().layer("meta", context["reg_part"], context["version"]) context["meta"] = meta[context["reg_part"]][0] context["formatted_id"] = label_to_text(context["label_id"].split("-")) content = self.content(context) if isinstance(content, HttpResponse): # error occurred return content context["partial_content"] = content utils.add_extras(context) return context
def get_context_data(self, **kwargs): # Skip ChromeView's implementation context = super(ChromeView, self).get_context_data(**kwargs) context['reg_part'] = context['label_id'].split('-')[0] context['version'] = self.request.GET.get('from_version') meta = api_reader.ApiReader().layer( 'meta', 'cfr', context['reg_part'], context['version']) context['meta'] = meta[context['reg_part']][0] context['formatted_id'] = label_to_text(context['label_id'].split('-')) content = self.content(context) if isinstance(content, HttpResponse): # error occurred return content context['partial_content'] = content return context
def context(self, http_client, request): analyses = [] data = self.fetch_data(http_client) if data: tree_labels = [Label(parts=t['label']) for t in self.fetch_relevant_trees(http_client)] analyses.extend( sxs for sxs in data if any(sxs.label in label for label in tree_labels)) analyses = [analysis.template_context() for analysis in sorted(analyses)] return { 'analyses': analyses, 'human_label_id': label_to_text( self.label_parts, include_marker=True), 'version': self.version }
def get_context_data(self, **kwargs): context = super(ChromeView, self).get_context_data(**kwargs) label_id = context['label_id'] version = context['version'] label_id_list = label_id.split('-') reg_part = label_id_list[0] context['q'] = self.request.GET.get('q', '') context['formatted_id'] = label_to_text(label_id_list, True, True) context['node_type'] = type_from_label(label_id_list) error_handling.check_regulation(reg_part) self.set_chrome_context(context, reg_part, version) self.check_tree(context) self.add_main_content(context) context['sidebar_content'] = self.sidebar(label_id, version) return context
def get_context_data(self, **kwargs): context = super(ParagraphSXSView, self).get_context_data(**kwargs) label_id = context['label_id'] part = label_id.split('-')[0] notice_id = context['notice_id'] fr_page = context.get('fr_page') version = context.get('version', notice_id) # Try first to get the notice and SxS with the version. try: notice, paragraph_sxs = generator.get_notice_and_sxs( part, version, label_id, fr_page) except TypeError: raise Http404 if notice is None or paragraph_sxs is None: # If that didn't work, try again with the notice_id notice, paragraph_sxs = generator.get_notice_and_sxs( part, notice_id, label_id, fr_page) if notice is None or paragraph_sxs is None: raise error_handling.MissingContentException() notices.add_depths(paragraph_sxs, 3) paragraph_sxs['children'] =\ notices.filter_labeled_children(paragraph_sxs) self.footnote_refs(paragraph_sxs) context['sxs'] = paragraph_sxs # Template assumes a single label context['sxs']['label'] = context['label_id'] context['sxs']['header'] = label_to_text(label_id.split('-'), include_marker=True) context['sxs']['all_footnotes'] = self.footnotes(notice, paragraph_sxs) context['notice'] = notice context['further_analyses'] = self.further_analyses( label_id, notice_id, context['version'], paragraph_sxs.get('page')) return context
def get_context_data(self, **kwargs): context = super(ParagraphSXSView, self).get_context_data(**kwargs) label_id = context['label_id'] part = label_id.split('-')[0] notice_id = context['notice_id'] fr_page = context.get('fr_page') version = context.get('version', notice_id) # Try first to get the notice and SxS with the version. notice, paragraph_sxs = generator.get_notice_and_sxs(part, version, label_id, fr_page) if notice is None or paragraph_sxs is None: # If that didn't work, try again with the notice_id notice, paragraph_sxs = generator.get_notice_and_sxs(part, notice_id, label_id, fr_page) if notice is None or paragraph_sxs is None: raise error_handling.MissingContentException() notices.add_depths(paragraph_sxs, 3) paragraph_sxs['children'] =\ notices.filter_labeled_children(paragraph_sxs) self.footnote_refs(paragraph_sxs) context['sxs'] = paragraph_sxs # Template assumes a single label context['sxs']['label'] = context['label_id'] context['sxs']['header'] = label_to_text(label_id.split('-'), include_marker=True) context['sxs']['all_footnotes'] = self.footnotes(notice, paragraph_sxs) context['notice'] = notice context['further_analyses'] = self.further_analyses( label_id, notice_id, context['version'], paragraph_sxs.get('page')) return context
def transform_context(self, context, builder): context["node"] = builder.tree context["formatted_label"] = label_to_text(builder.tree["label"], True, True) context["node"]["section_id"] = "%s-%s" % (builder.tree["label"][0], builder.tree["label"][1]) return context
def human_label(node): """Derive a human-readable description for this node. Override""" return node_types.label_to_text(node['label'], include_marker=True)
def template_context(self): return {'doc_number': self.doc_number, 'label_id': self.label.id, 'text': label_to_text(self.label.parts, include_section=False, include_marker=True)}