Ejemplo n.º 1
0
    def test_fetch_grouped_history(self, reader):
        client = reader.ApiReader.return_value

        future = datetime.today() + timedelta(days=5)
        future = future.strftime('%Y-%m-%d')
        client.regversions.return_value = {'versions': [
            {'by_date': '2001-01-01', 'version': 'v1'},
            {'versions': 'v2'},
            {'by_date': '2003-03-03', 'version': 'v3'},
            {'versions': 'v4'},
            {'versions': 'v5'},
            {'by_date': future, 'version': 'v6'},
        ]}

        n1 = {'effective_on': '2001-01-01', 'publication_date': '2000-10-10'}
        n2 = {'effective_on': '2003-03-03', 'publication_date': '2001-01-01'}
        n3 = {'effective_on': '2003-03-03', 'publication_date': '2002-02-02'}
        n4 = {'effective_on': future, 'publication_date': '2005-05-05'}
        n5 = {'effective_on': future, 'publication_date': '2005-06-05'}
        n6 = {'effective_on': future, 'publication_date': '2005-07-05'}
        client.notices.return_value = {'results': [n1, n2, n3, n5, n4, n6]}

        history = versions.fetch_grouped_history('111')
        self.assertEqual(3, len(history))
        v1, v2, v3 = history

        self.assertEqual('future', v1['timeline'])
        self.assertEqual('current', v2['timeline'])
        self.assertEqual('past', v3['timeline'])

        self.assertEqual(versions.convert_to_python([n6, n5, n4]),
                         v1['notices'])
        self.assertEqual(versions.convert_to_python([n3, n2]), v2['notices'])
        self.assertEqual(versions.convert_to_python([n1]), v3['notices'])
Ejemplo n.º 2
0
    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
Ejemplo n.º 3
0
    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 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
Ejemplo n.º 5
0
def get_versions(label_id):
    """ Get the current and next version of the regulation. """
    history = fetch_grouped_history(label_id)
    if history:
        future = [h for h in history if h['timeline'] == 'future']
        if len(future) > 0:
            next_version = future[-1]
        else:
            next_version = None

        current = [h for h in history if h['timeline'] == 'current']
        current_version = current[0]
        return (current_version, next_version)
Ejemplo n.º 6
0
def get_versions(label_id):
    """ Get the current and next version of the regulation. """
    history = fetch_grouped_history(label_id)
    if history:
        future = [h for h in history if h["timeline"] == "future"]
        if len(future) > 0:
            next_version = future[-1]
        else:
            next_version = None

        current = [h for h in history if h["timeline"] == "current"]
        current_version = current[0]
        return (current_version, next_version)
    def get_context_data(self, doc_type, **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['doc_type'] = doc_type

        context['regulation'] = context['label_id'].split('-')[0]
        context['url_rule'] = url_rules[doc_type]

        try:
            page = int(self.request.GET.get('page', '0'))
        except ValueError:
            page = 0

        context['warnings'] = []
        if not context['q']:
            context['warnings'].append('Please provide a query.')
        if doc_type == 'cfr' and 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['doc_type'],
                version=context['version'],
                regulation=context['regulation'],
                page=page,
                page_size=PAGE_SIZE,
                is_root='false',
                is_subpart='false',
            )

        if doc_type == 'cfr':
            context['results'] = process_cfr_results(results,
                                                     context['version'])
            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']
        else:
            context['results'] = process_preamble_results(results)

        self.add_prev_next(page, context)

        return context
Ejemplo n.º 8
0
    def get_context_data(self, doc_type, **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["doc_type"] = doc_type

        context["regulation"] = context["label_id"].split("-")[0]
        context["url_rule"] = url_rules[doc_type]

        try:
            page = int(self.request.GET.get("page", "0"))
        except ValueError:
            page = 0

        context["warnings"] = []
        if not context["q"]:
            context["warnings"].append("Please provide a query.")
        if doc_type == "cfr" and 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["doc_type"],
                context["version"],
                context["regulation"],
                page=page,
                is_root="false",
                is_subpart="false",
            )

        if doc_type == "cfr":
            context["results"] = process_cfr_results(results, context["version"])
            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"]
        else:
            context["results"] = process_preamble_results(results)

        self.add_prev_next(page, context)

        return context
Ejemplo n.º 9
0
def order_diff_versions(label_id, version, new_version):
    # Re-order if needed - History is sorted in reverse chronological order
    for major_version in fetch_grouped_history(label_id.split('-')[0]):
        for notice in major_version['notices']:
            # Hit the "old" version first, meaning it's not actually the old
            # version
            if notice['document_number'] == version:
                return redirect('chrome_section_diff_view', label_id,
                                new_version, version)
            # Hit the new version first -- sort is correct
            elif notice['document_number'] == new_version:
                return redirect('chrome_section_diff_view', label_id, version,
                                new_version)

    # Didn't find the versions in question. Assume this was intentional
    return redirect('chrome_section_diff_view', label_id, version, new_version)
Ejemplo n.º 10
0
    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
Ejemplo n.º 11
0
    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)
Ejemplo n.º 12
0
def order_diff_versions(label_id, version, new_version):
    # Re-order if needed - History is sorted in reverse chronological order
    for major_version in fetch_grouped_history(label_id.split('-')[0]):
        for notice in major_version['notices']:
            # Hit the "old" version first, meaning it's not actually the old
            # version
            if notice['document_number'] == version:
                return redirect('chrome_section_diff_view', label_id,
                                new_version, version)
            # Hit the new version first -- sort is correct
            elif notice['document_number'] == new_version:
                return redirect('chrome_section_diff_view', label_id,
                                version, new_version)

    # Didn't find the versions in question. Assume this was intentional
    return redirect('chrome_section_diff_view', label_id, version,
                    new_version)
Ejemplo n.º 13
0
    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)
Ejemplo n.º 14
0
    def get_context_data(self, doc_type, **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['doc_type'] = doc_type

        context['regulation'] = context['label_id'].split('-')[0]
        context['url_rule'] = url_rules[doc_type]

        try:
            page = int(self.request.GET.get('page', '0'))
        except ValueError:
            page = 0

        context['warnings'] = []
        if not context['q']:
            context['warnings'].append('Please provide a query.')
        if doc_type == 'cfr' and 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['doc_type'],
                version=context['version'], regulation=context['regulation'],
                page=page, page_size=PAGE_SIZE,
                is_root='false', is_subpart='false',
            )

        if doc_type == 'cfr':
            context['results'] = process_cfr_results(results,
                                                     context['version'])
            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']
        else:
            context['results'] = process_preamble_results(results)

        self.add_prev_next(page, context)

        return context
Ejemplo n.º 15
0
    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)
Ejemplo n.º 16
0
    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)