class InternalCitationLayer(): shorthand = 'internal' def __init__(self, layer): self.layer = layer self.sectional = False self.version = None self.rev_urls = SectionUrl() self.rendered = {} def render_url( self, label, text, template_name='regulations/layers/internal_citation.html'): key = (tuple(label), text, template_name) if key not in self.rendered: url = self.rev_urls.fetch(label, self.version, self.sectional) c = Context({'citation': { 'url': url, 'label': text, 'label_id': self.rev_urls.view_label_id(label, self.version)}}) template = loader.get_template(template_name) self.rendered[key] = template.render(c).strip('\n') return self.rendered[key] def apply_layer(self, text, text_index): if text_index in self.layer: layer_elements = self.layer[text_index] layer_pairs = [] for layer_element in layer_elements: for start, end in layer_element['offsets']: ot = text[int(start):int(end)] rt = self.render_url(layer_element['citation'], ot) layer_pairs.append((ot, rt, (start, end))) return layer_pairs
class InternalCitationLayer(): shorthand = 'internal' def __init__(self, layer): self.layer = layer self.sectional = False self.version = None self.rev_urls = SectionUrl() self.rendered = {} def render_url( self, label, text, template_name='regulations/layers/internal_citation.html'): key = (tuple(label), text, template_name) if key not in self.rendered: url = self.rev_urls.fetch(label, self.version, self.sectional) c = Context({'citation': {'url': url, 'label': text, 'label_id': self.rev_urls.view_label_id(label, self.version)}}) template = loader.get_template(template_name) self.rendered[key] = template.render(c).strip('\n') return self.rendered[key] def apply_layer(self, text, text_index): if text_index in self.layer: layer_elements = self.layer[text_index] layer_pairs = [] for layer_element in layer_elements: for start, end in layer_element['offsets']: ot = text[int(start):int(end)] rt = self.render_url(layer_element['citation'], ot) layer_pairs.append((ot, rt, (start, end))) return layer_pairs
class InternalCitationLayer(InlineLayer): shorthand = 'internal' data_source = 'internal-citations' def __init__(self, layer): self.layer = layer self.sectional = False self.version = None self.rev_urls = SectionUrl() self.rendered = {} def render_url(self, label, text, template_name='regulations/layers/internal_citation.html'): key = (tuple(label), text, template_name) if key not in self.rendered: url = self.rev_urls.fetch(label, self.version, self.sectional) c = Context({'citation': { 'url': url, 'label': text, 'label_id': self.rev_urls.view_label_id(label, self.version)}}) template = loader.get_template(template_name) self.rendered[key] = template.render(c).strip('\n') return self.rendered[key] def replacement_for(self, original, data): return self.render_url(data['citation'], original)
class InternalCitationLayer(InlineLayer): shorthand = 'internal' data_source = 'internal-citations' def __init__(self, layer): self.layer = layer self.sectional = False self.version = None self.rev_urls = SectionUrl() self.rendered = {} def render_url(self, label, text, template_name='regulations/layers/internal_citation.html'): key = (tuple(label), text, template_name) if key not in self.rendered: url = self.rev_urls.fetch(label, self.version, self.sectional) c = Context({ 'citation': { 'url': url, 'label': text, 'label_id': self.rev_urls.view_label_id(label, self.version) } }) template = loader.get_template(template_name) self.rendered[key] = template.render(c).strip('\n') return self.rendered[key] def replacement_for(self, original, data): return self.render_url(data['citation'], original)
def test_of(self): url = SectionUrl.of(['303', '1'], 'vvv', False) self.assertEquals('#303-1', url) url = SectionUrl.of(['303', '1', 'b'], 'vvv', False) self.assertEquals('#303-1-b', url) url = SectionUrl.of(['303'], 'vvv', False) self.assertEquals('#303', url) url = SectionUrl.of(['303', '1', 'b'], 'vvv', True) self.assertEquals('/303-1/vvv#303-1-b', url) self.assertTrue('999-88/verver#999-88-e' in SectionUrl.of(['999', '88', 'e'], 'verver', True)) self.assertEqual( '#999-88-e', SectionUrl.of(['999', '88', 'e'], 'verver', False)) self.assertEqual( '#999-Subpart-Interp', SectionUrl.of(['999', 'Subpart', 'Interp'], 'verver', False)) self.assertEqual( '#999-Subpart-A-Interp', SectionUrl.of(['999', 'Subpart', 'A', 'Interp'], 'verver', False)) self.assertEqual( '#999-Appendices-Interp', SectionUrl.of(['999', 'Appendices', 'Interp'], 'verver', False))
def test_of(self): url = SectionUrl.of(['303', '1'], 'vvv', False) self.assertEquals('#303-1', url) url = SectionUrl.of(['303', '1', 'b'], 'vvv', False) self.assertEquals('#303-1-b', url) url = SectionUrl.of(['303'], 'vvv', False) self.assertEquals('#303', url) url = SectionUrl.of(['303', '1', 'b'], 'vvv', True) self.assertEquals('/303-1/vvv#303-1-b', url) self.assertTrue('999-88/verver#999-88-e' in SectionUrl.of( ['999', '88', 'e'], 'verver', True)) self.assertEqual('#999-88-e', SectionUrl.of(['999', '88', 'e'], 'verver', False)) self.assertEqual( '#999-Subpart-Interp', SectionUrl.of(['999', 'Subpart', 'Interp'], 'verver', False)) self.assertEqual( '#999-Subpart-A-Interp', SectionUrl.of(['999', 'Subpart', 'A', 'Interp'], 'verver', False)) self.assertEqual( '#999-Appendices-Interp', SectionUrl.of(['999', 'Appendices', 'Interp'], 'verver', False))
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 __init__(self, layer): self.layer = layer self.template = loader.get_template( 'regulations/layers/definition_citation.html') self.sectional = False self.version = None self.rev_urls = SectionUrl() self.rendered = {} # precomputation for def_struct in self.layer['referenced'].values(): def_struct['reference_split'] = def_struct['reference'].split('-')
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 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']: add_cfr_headers(result) 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): # 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 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']: add_cfr_headers(result) result['section_id'] = section_url.view_label_id( result['label'], version) result['url'] = section_url.fetch(result['label'], version, sectional=True) return results
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 test_interp(self, fetch_toc): fetch_toc.return_value = [{ 'index': ['200', '1'], 'is_section': True }, { 'index': ['200', '2'], 'is_section': True }, { 'index': ['200', 'A'], 'is_appendix': True }] self.assertEqual('200-Subpart-Interp', SectionUrl().interp(['200', '1', 'Interp'], 'vvvv')) self.assertEqual('200-Subpart-Interp', SectionUrl().interp(['200', '2', 'Interp'], 'vvvv')) self.assertEqual('200-Appendices-Interp', SectionUrl().interp(['200', 'A', 'Interp'], 'vvvv')) fetch_toc.return_value = [{ 'index': ['200', 'Subpart', 'A'], 'is_subpart': True, 'sub_toc': [{ 'index': ['200', '1'], 'is_section': True }, { 'index': ['200', '2'], 'is_section': True }] }, { 'index': ['200', 'A'], 'is_appendix': True }, { 'index': ['200', 'Interp'], 'is_supplement': True, 'sub_toc': [{ 'index': ['200', 'Interp', 'h1'], 'section_id': '200-Interp-h1' }, { 'index': ['200', 'Subpart', 'A', 'Interp'], 'is_subterp': True }, { 'index': ['200', 'Appendices', 'Interp'], 'is_subterp': True }] }] self.assertEqual('200-Subpart-A-Interp', SectionUrl().interp(['200', '1', 'Interp'], 'vvvv')) self.assertEqual('200-Subpart-A-Interp', SectionUrl().interp(['200', '2', 'Interp'], 'vvvv')) self.assertEqual('200-Appendices-Interp', SectionUrl().interp(['200', 'A', 'Interp'], 'vvvv')) self.assertEqual('200-Interp-h1', SectionUrl().interp(['200', 'Interp', 'h1'], 'vvvv')) self.assertTrue( '200-Subpart-A', SectionUrl().interp(['200', '2', 'e', 'Interp', '1'], 'verver'))
class DefinitionsLayer(InlineLayer): shorthand = 'terms' data_source = 'terms' def __init__(self, layer): self.layer = layer self.template = loader.get_template( 'regulations/layers/definition_citation.html') self.sectional = False self.version = None self.rev_urls = SectionUrl() self.rendered = {} # precomputation for def_struct in self.layer['referenced'].values(): def_struct['reference_split'] = def_struct['reference'].split('-') def replacement_for(self, original, data): """ Create the link that takes you to the definition of the term. """ citation = data['ref'] # term = term w/o pluralization term = self.layer['referenced'][citation]['term'] citation = self.layer['referenced'][citation]['reference_split'] key = (original, tuple(citation)) if key not in self.rendered: context = {'citation': { 'url': self.rev_urls.fetch(citation, self.version, self.sectional), 'label': original, 'term': term, 'definition_reference': '-'.join(to_markup_id(citation))}} rendered = utils.render_template(self.template, context) self.rendered[key] = rendered return self.rendered[key]
class InterpretationsLayer(object): """Fetches the (rendered) interpretation for this node, if available""" shorthand = 'interp' def __init__(self, layer, version=None): self.layer = layer self.version = version self.section_url = SectionUrl() self.root_interp_label = None def preprocess_root(self, root): """Store the label for the root node ('s interpretation) for later use. We'll try to fetch it when we need a slide-down interp to cache it for future interps. Don't do this if the cache isn't set up or if we are processing an interp (which therefore has no interps).""" cache = get_cache('api_cache') if (root['node_type'] != 'interp' and not isinstance(cache, DummyCache)): self.root_interp_label = '-'.join(root['label'] + ['Interp']) 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 if self.root_interp_label: generator.generator.get_tree_paragraph(self.root_interp_label, self.version) 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
class InterpretationsLayer(LayerBase): """Fetches the (rendered) interpretation for this node, if available""" shorthand = 'interp' data_source = 'interpretations' layer_type = LayerBase.PARAGRAPH def __init__(self, layer, version=None): self.layer = layer self.version = version self.section_url = SectionUrl() self.root_interp_label = None self.partial_view = None def preprocess_root(self, root): """The root label will allow us to use a single set of layer appliers and grab all interp data at once.""" self.root_interp_label = '-'.join(root['label'] + ['Interp']) view_class = views.partial_interp.PartialInterpView self.partial_view = view_class.as_view(inline=True, appliers=view_class.mk_appliers( self.root_interp_label, self.version)) 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 get(self, request, *args, **kwargs): """Override this method so that we can grab the GET variables""" kwargs["version"] = request.GET.get("from_version") kwargs["back_url"] = SectionUrl.of(kwargs["label_id"].split("-"), kwargs["version"], sectional=True) kwargs["fr_page"] = request.GET.get("fr_page") if kwargs["fr_page"] and kwargs["fr_page"].isdigit(): kwargs["fr_page"] = int(kwargs["fr_page"]) return super(ParagraphSXSView, self).get(request, *args, **kwargs)
def set_chrome_context(self, context, reg_part, version): context['reg_part'] = reg_part context['history'] = fetch_grouped_history(reg_part) toc = fetch_toc(reg_part, version) for el in toc: el['url'] = SectionUrl().of(el['index'], version, self.partial_class.sectional_links) for sub in el.get('sub_toc', []): sub['url'] = SectionUrl().of( sub['index'], version, self.partial_class.sectional_links) context['TOC'] = toc context['meta'] = utils.regulation_meta(reg_part, version) context['version_switch_view'] = self.version_switch_view context['diff_redirect_label'] = self.diff_redirect_label( context['label_id'], toc)
class TableOfContentsLayer(ParagraphLayer): shorthand = 'toc' data_source = 'toc' def __init__(self, layer): self.layer = layer self.sectional = False self.version = None self.section_url = SectionUrl() def attach_metadata(self, node): text_index = node['label_id'] if text_index in self.layer: layer_elements = self.layer[text_index] toc_list = [] for data in layer_elements: if 'Subpart' in data['index']: toc_list.append(toc_subpart(data, toc_list, self.layer)) elif 'Interp' in data['index']: toc_list.append(toc_interp(data, toc_list, self.layer)) else: toc_list.append(toc_sect_appendix(data, toc_list)) for el in toc_list: el['url'] = self.section_url.fetch( el['index'], self.version, self.sectional) for sub in el.get('sub_toc', []): sub['url'] = self.section_url.fetch( sub['index'], self.version, self.sectional) node['TOC'] = toc_list @staticmethod def section(element, data): title_data = title_parsing.section(data) if title_data: element.update(title_data) @staticmethod def appendix_supplement(element, data, seen_appendix=False): as_data = title_parsing.appendix_supplement(data) if as_data: element.update(as_data) if element.get('is_appendix'): element['is_first_appendix'] = not seen_appendix
def _add_extra(el, version): """Add extra fields to a TOC element -- only added to elements we will use for prev/next""" if el.get('is_section'): el['markup_prefix'] = '§ ' elif el.get('is_subterp'): el['markup_prefix'] = 'Interpretations For ' el['url'] = SectionUrl.of(el['index'], version, sectional=True) return el
class InterpretationsLayer(object): """Fetches the (rendered) interpretation for this node, if available""" shorthand = 'interp' def __init__(self, layer, version=None): self.layer = layer self.version = version self.section_url = SectionUrl() self.root_interp_label = None def preprocess_root(self, root): """Store the label for the root node ('s interpretation) for later use. We'll try to fetch it when we need a slide-down interp to cache it for future interps. Don't do this if the cache isn't set up or if we are processing an interp (which therefore has no interps).""" cache = get_cache('api_cache') if (root['node_type'] != 'interp' and not isinstance(cache, DummyCache)): self.root_interp_label = '-'.join(root['label'] + ['Interp']) 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 if self.root_interp_label: generator.generator.get_tree_paragraph( self.root_interp_label, self.version) 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(self, request, *args, **kwargs): """Override this method so that we can grab the GET variables""" kwargs['version'] = request.GET.get('from_version') kwargs['back_url'] = SectionUrl.of(kwargs['label_id'].split('-'), kwargs['version'], sectional=True) kwargs['fr_page'] = request.GET.get('fr_page') if kwargs['fr_page'] and kwargs['fr_page'].isdigit(): kwargs['fr_page'] = int(kwargs['fr_page']) return super(ParagraphSXSView, self).get(request, *args, **kwargs)
def __init__(self, layer): self.layer = layer self.template = loader.get_template("regulations/layers/definition_citation.html") self.sectional = False self.version = None self.rev_urls = SectionUrl() self.rendered = {} # precomputation for def_struct in self.layer["referenced"].values(): def_struct["reference_split"] = def_struct["reference"].split("-")
def get(self, request, *args, **kwargs): """Override this method so that we can grab the GET variables""" kwargs['version'] = request.GET.get('from_version') kwargs['back_url'] = SectionUrl.of( kwargs['label_id'].split('-'), kwargs['version'], sectional=True) kwargs['fr_page'] = request.GET.get('fr_page') if kwargs['fr_page'] and kwargs['fr_page'].isdigit(): kwargs['fr_page'] = int(kwargs['fr_page']) return super(ParagraphSXSView, self).get(request, *args, **kwargs)
class InterpretationsLayer(ParagraphLayer): """Fetches the (rendered) interpretation for this node, if available""" shorthand = 'interp' data_source = 'interpretations' def __init__(self, layer, version=None): self.layer = layer self.version = version self.section_url = SectionUrl() self.root_interp_label = None self.partial_view = None def preprocess_root(self, root): """The root label will allow us to use a single set of layer appliers and grab all interp data at once.""" self.root_interp_label = '-'.join(root['label'] + ['Interp']) view_class = views.partial_interp.PartialInterpView self.partial_view = view_class.as_view( inline=True, layers=view_class.mk_layers( self.root_interp_label, self.version)) 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): if text_index in self.layer: layer_elements = self.layer[text_index] toc_list = [] for data in layer_elements: if 'Subpart' in data['index']: toc_list.append(toc_subpart(data, toc_list, self.layer)) elif 'Interp' in data['index']: toc_list.append(toc_interp(data, toc_list, self.layer)) else: toc_list.append(toc_sect_appendix(data, toc_list)) for el in toc_list: el['url'] = SectionUrl.of(el['index'], self.version, self.sectional) for sub in el.get('sub_toc', []): sub['url'] = SectionUrl.of(sub['index'], self.version, self.sectional) return ('TOC', toc_list)
class DefinitionsLayer(object): shorthand = 'terms' def __init__(self, layer): self.layer = layer self.template = loader.get_template( 'regulations/layers/definition_citation.html') self.sectional = False self.version = None self.rev_urls = SectionUrl() self.rendered = {} # precomputation for def_struct in self.layer['referenced'].values(): def_struct['reference_split'] = def_struct['reference'].split('-') def create_definition_link(self, original_text, citation, term): """ Create the link that takes you to the definition of the term. """ key = (original_text, tuple(citation)) if key not in self.rendered: context = { 'citation': { 'url': self.rev_urls.fetch(citation, self.version, self.sectional), 'label': original_text, 'term': term, 'definition_reference': '-'.join(to_markup_id(citation)) } } rendered = utils.render_template(self.template, context) self.rendered[key] = rendered return self.rendered[key] def apply_layer(self, text, text_index): """Catch all terms which are defined elsewhere and replace them with a link""" layer_pairs = [] if text_index in self.layer: layer_elements = self.layer[text_index] for layer_element in layer_elements: ref = layer_element['ref'] # term = term w/o pluralization term = self.layer['referenced'][ref]['term'] ref = self.layer['referenced'][ref]['reference_split'] for start, end in layer_element['offsets']: ot = text[start:end] rt = self.create_definition_link(ot, ref, term) layer_pairs.append((ot, rt, (start, end))) return layer_pairs
class InterpretationsLayer(LayerBase): """Fetches the (rendered) interpretation for this node, if available""" shorthand = "interp" data_source = "interpretations" layer_type = LayerBase.PARAGRAPH def __init__(self, layer, version=None): self.layer = layer self.version = version self.section_url = SectionUrl() self.root_interp_label = None self.partial_view = None def preprocess_root(self, root): """The root label will allow us to use a single set of layer appliers and grab all interp data at once.""" self.root_interp_label = "-".join(root["label"] + ["Interp"]) view_class = views.partial_interp.PartialInterpView self.partial_view = view_class.as_view( inline=True, appliers=view_class.mk_appliers(self.root_interp_label, self.version) ) 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 set_chrome_context(self, context, reg_part, version): context['reg_part'] = reg_part context['history'] = fetch_grouped_history(reg_part) toc = fetch_toc(reg_part, version) for el in toc: el['url'] = SectionUrl().of(el['index'], version, self.partial_class.sectional_links) for sub in el.get('sub_toc', []): sub['url'] = SectionUrl().of( sub['index'], version, self.partial_class.sectional_links) context['TOC'] = toc context['meta'] = utils.regulation_meta(reg_part, version) # Throw 404 if regulation doesn't exist if not context['meta']: raise error_handling.MissingContentException() context['version_span'] = version_span( context['history'], context['meta']['effective_date']) context['version_switch_view'] = self.version_switch_view context['diff_redirect_label'] = self.diff_redirect_label( context['label_id'], toc)
def get(self, request, *args, **kwargs): """Override this method so that we can grab the GET variables and properly handle any errors.""" kwargs['version'] = request.GET.get('from_version') kwargs['back_url'] = SectionUrl.of(kwargs['label_id'].split('-'), kwargs['version'], sectional=True) kwargs['fr_page'] = request.GET.get('fr_page') if kwargs['fr_page'] and kwargs['fr_page'].isdigit(): kwargs['fr_page'] = int(kwargs['fr_page']) try: return super(ParagraphSXSView, self).get(request, *args, **kwargs) except error_handling.MissingContentException as e: raise Http404
class DefinitionsLayer(object): shorthand = "terms" def __init__(self, layer): self.layer = layer self.template = loader.get_template("regulations/layers/definition_citation.html") self.sectional = False self.version = None self.rev_urls = SectionUrl() self.rendered = {} # precomputation for def_struct in self.layer["referenced"].values(): def_struct["reference_split"] = def_struct["reference"].split("-") def create_definition_link(self, original_text, citation, term): """ Create the link that takes you to the definition of the term. """ key = (original_text, tuple(citation)) if key not in self.rendered: context = { "citation": { "url": self.rev_urls.fetch(citation, self.version, self.sectional), "label": original_text, "term": term, "definition_reference": "-".join(to_markup_id(citation)), } } rendered = utils.render_template(self.template, context) self.rendered[key] = rendered return self.rendered[key] def apply_layer(self, text, text_index): """Catch all terms which are defined elsewhere and replace them with a link""" layer_pairs = [] if text_index in self.layer: layer_elements = self.layer[text_index] for layer_element in layer_elements: ref = layer_element["ref"] # term = term w/o pluralization term = self.layer["referenced"][ref]["term"] ref = self.layer["referenced"][ref]["reference_split"] for start, end in layer_element["offsets"]: ot = text[start:end] rt = self.create_definition_link(ot, ref, term) layer_pairs.append((ot, rt, (start, end))) return layer_pairs
class DefinitionsLayer(object): shorthand = 'terms' def __init__(self, layer): self.layer = layer self.template = loader.get_template( 'regulations/layers/definition_citation.html') self.sectional = False self.version = None self.rev_urls = SectionUrl() self.rendered = {} # precomputation for def_struct in self.layer['referenced'].values(): def_struct['reference_split'] = def_struct['reference'].split('-') def create_definition_link(self, original_text, citation, term): """ Create the link that takes you to the definition of the term. """ key = (original_text, tuple(citation)) if key not in self.rendered: context = { 'citation': { 'url': self.rev_urls.fetch(citation, self.version, self.sectional), 'label': original_text, 'term': term, 'definition_reference': '-'.join(to_markup_id(citation))}} rendered = utils.render_template(self.template, context) self.rendered[key] = rendered return self.rendered[key] def apply_layer(self, text, text_index): """Catch all terms which are defined elsewhere and replace them with a link""" layer_pairs = [] if text_index in self.layer: layer_elements = self.layer[text_index] for layer_element in layer_elements: ref = layer_element['ref'] # term = term w/o pluralization term = self.layer['referenced'][ref]['term'] ref = self.layer['referenced'][ref]['reference_split'] for start, end in layer_element['offsets']: ot = text[start:end] rt = self.create_definition_link(ot, ref, term) layer_pairs.append((ot, rt, (start, end))) return layer_pairs
class InterpretationsLayer(object): """Fetches the (rendered) interpretation for this node, if available""" shorthand = 'interp' def __init__(self, layer, version=None): self.layer = layer self.version = version self.section_url = SectionUrl() 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 add_diff_content(self, context): context['from_version'] = self.request.GET.get('from_version', context['version']) context['left_version'] = context['version'] context['right_version'] = \ context['main_content_context']['newer_version'] from_version = self.request.GET.get('from_version', context['version']) context['TOC'] = context['main_content_context']['TOC'] # Add reference to the first subterp, so we know how to redirect toc = fetch_toc(context['label_id'].split('-')[0], from_version) for entry in toc: if entry.get('is_supplement') and entry.get('sub_toc'): el = entry['sub_toc'][0] el['url'] = SectionUrl().of(el['index'], from_version, self.partial_class.sectional_links) context['first_subterp'] = el return context
class InterpretationsLayer(object): """Fetches the (rendered) interpretation for this node, if available""" shorthand = 'interp' def __init__(self, layer, version=None): self.layer = layer self.version = version self.section_url = SectionUrl() 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
class DefinitionsLayer(InlineLayer): shorthand = 'terms' data_source = 'terms' def __init__(self, layer): self.layer = layer self.template = loader.get_template( 'regulations/layers/definition_citation.html') self.sectional = False self.version = None self.rev_urls = SectionUrl() self.rendered = {} # precomputation for def_struct in self.layer['referenced'].values(): def_struct['reference_split'] = def_struct['reference'].split('-') def replacement_for(self, original, data): """ Create the link that takes you to the definition of the term. """ citation = data['ref'] # term = term w/o pluralization term = self.layer['referenced'][citation]['term'] citation = self.layer['referenced'][citation]['reference_split'] key = (original, tuple(citation)) if key not in self.rendered: context = { 'citation': { 'url': self.rev_urls.fetch(citation, self.version, self.sectional), 'label': original, 'term': term, 'definition_reference': '-'.join(to_markup_id(citation)) } } rendered = utils.render_template(self.template, context) self.rendered[key] = rendered return self.rendered[key]
def __init__(self, layer, version=None): self.layer = layer self.version = version self.section_url = SectionUrl()
def __init__(self, layer): self.layer = layer self.sectional = False self.version = None self.rev_urls = SectionUrl() self.rendered = {}
def __init__(self, layer, version=None): self.layer = layer self.version = version self.section_url = SectionUrl() self.root_interp_label = None self.partial_view = None
def __init__(self, layer): self.layer = layer self.sectional = False self.version = None self.section_url = SectionUrl()