Ejemplo n.º 1
0
def redirect_by_date(request, label_id, year, month, day):
    """If a user requests a date as the version, find the version which was
    current as of that date"""
    date_versions = []
    client = ApiReader()
    for struct in client.regversions(label_id.split('-')[0])['versions']:
        if 'by_date' in struct:
            date_versions.append((struct['by_date'], struct['version']))

    date_versions = sorted(date_versions)
    last_version = None
    date_str = '%s-%s-%s' % (year, month, day)
    while date_versions and date_versions[0][0] <= date_str:
        last_version = date_versions[0][1]
        date_versions = date_versions[1:]

    label_parts = label_id.split('-')
    if last_version and len(label_parts) == 2:
        return redirect('chrome_section_view', label_id, last_version)
    elif last_version and label_parts[-1] == 'Interp':
        return redirect('chrome_section_view', label_id, last_version)
    elif last_version and len(label_parts) == 1:
        return redirect('chrome_regulation_view', label_id, last_version)
    elif last_version:
        return redirect('chrome_paragraph_view', label_id, last_version)
    else:
        return handle_generic_404(request)
Ejemplo n.º 2
0
def fetch_toc(reg_part, version, flatten=False):
    """Fetch the toc, transform it into a list usable by navigation, etc."""
    api = ApiReader()
    toc = api.layer('toc', reg_part, version)

    toc_list = []
    if reg_part in toc:
        for data in toc[reg_part]:
            if 'Subpart' in data['index']:
                toc_list.append(toc_subpart(data, toc_list, toc))
            elif 'Subjgrp' in data['index']:
                toc_list.append(toc_subjgrp(data, toc_list, toc))
            elif 'Interp' in data['index']:
                toc_list.append(toc_interp(data, toc_list, toc))
            else:
                toc_list.append(toc_sect_appendix(data, toc_list))
    if flatten:
        flattened = []
        for el in toc_list:
            if 'sub_toc' in el:
                flattened.extend(el['sub_toc'])
            else:
                flattened.append(el)
        return flattened
    return toc_list
    def test_layer(self, api_client):
        to_return = {'example': 1}
        api_client.ApiClient.return_value.get.return_value = to_return
        reader = ApiReader()
        self.assertEqual(
            to_return,
            reader.layer("layer-here", "label-here", "date-here"))
        get = api_client.ApiClient.return_value.get
        self.assertEqual(1, get.call_count)
        param = api_client.ApiClient.return_value.get.call_args[0][0]
        self.assertTrue('layer-here' in param)
        self.assertTrue('label' in param)   # grabs the root
        self.assertTrue('date-here' in param)

        #   Cache
        self.assertEqual(
            to_return,
            reader.layer("layer-here", "label-abc", "date-here"))
        self.assertEqual(1, get.call_count)

        self.assertEqual(
            to_return,
            reader.layer("layer-here", "lablab", "date-here"))
        self.assertEqual(2, get.call_count)
        param = get.call_args[0][0]
        self.assertTrue('layer-here' in param)
        self.assertTrue('lablab' in param)
        self.assertTrue('date-here' in param)
Ejemplo n.º 4
0
def redirect_by_date(request, label_id, year, month, day):
    """If a user requests a date as the version, find the version which was
    current as of that date"""
    date_versions = []
    client = ApiReader()
    for struct in client.regversions(label_id.split('-')[0])['versions']:
        if 'by_date' in struct:
            date_versions.append((struct['by_date'], struct['version']))

    date_versions = sorted(date_versions)
    last_version = None
    date_str = '%s-%s-%s' % (year, month, day)
    while date_versions and date_versions[0][0] <= date_str:
        last_version = date_versions[0][1]
        date_versions = date_versions[1:]

    label_parts = label_id.split('-')
    if last_version and len(label_parts) == 2:
        return redirect('chrome_section_view', label_id, last_version)
    elif last_version and label_parts[-1] == 'Interp':
        return redirect('chrome_section_view', label_id, last_version)
    elif last_version and len(label_parts) == 1:
        return redirect('chrome_regulation_view', label_id, last_version)
    elif last_version:
        return redirect('chrome_paragraph_view', label_id, last_version)
    else:
        return handle_generic_404(request)
Ejemplo n.º 5
0
    def test_layer(self, fetch):
        to_return = {'example': 1}
        fetch.return_value = to_return
        reader = ApiReader()
        self.assertEqual(
            to_return,
            reader.layer("layer-here", "cfr", "label-here", "version-here"))
        self.assertEqual(1, fetch.call_count)
        param = fetch.call_args[0][0]
        self.assertIn('layer-here/cfr/version-here/label', param)
        self.assertNotIn('label-here', param)   # only grabs the root, "label"

        #   Cache
        self.assertEqual(
            to_return,
            reader.layer("layer-here", "cfr", "label-abc", "version-here"))
        self.assertEqual(1, fetch.call_count)

        self.assertEqual(
            to_return,
            reader.layer("layer-here", "cfr", "lablab", "version-here"))
        self.assertEqual(2, fetch.call_count)
        param = fetch.call_args[0][0]
        self.assertIn('layer-here/cfr/version-here/lablab', param)

        self.assertEqual(
            to_return,
            reader.layer("layer-here", "preamble", "lablab"))
        self.assertEqual(3, fetch.call_count)
        param = fetch.call_args[0][0]
        self.assertIn('layer-here/preamble/lablab', param)
Ejemplo n.º 6
0
def fetch_toc(reg_part, version, flatten=False):
    """Fetch the toc, transform it into a list usable by navigation, etc."""
    api = ApiReader()
    toc = api.layer('toc', reg_part, version)

    toc_list = []
    if reg_part in toc:
        for data in toc[reg_part]:
            if 'Subpart' in data['index']:
                toc_list.append(toc_subpart(data, toc_list, toc))
            elif 'Subjgrp' in data['index']:
                toc_list.append(toc_subjgrp(data, toc_list, toc))
            elif 'Interp' in data['index']:
                toc_list.append(toc_interp(data, toc_list, toc))
            else:
                toc_list.append(toc_sect_appendix(data, toc_list))
    if flatten:
        flattened = []
        for el in toc_list:
            if 'sub_toc' in el:
                flattened.extend(el['sub_toc'])
            else:
                flattened.append(el)
        return flattened
    return toc_list
Ejemplo n.º 7
0
    def test_layer(self, api_client):
        to_return = {'example': 1}
        api_client.ApiClient.return_value.get.return_value = to_return
        reader = ApiReader()
        self.assertEqual(to_return,
                         reader.layer("layer-here", "label-here", "date-here"))
        get = api_client.ApiClient.return_value.get
        self.assertEqual(1, get.call_count)
        param = api_client.ApiClient.return_value.get.call_args[0][0]
        self.assertTrue('layer-here' in param)
        self.assertTrue('label' in param)  # grabs the root
        self.assertTrue('date-here' in param)

        #   Cache
        self.assertEqual(to_return,
                         reader.layer("layer-here", "label-abc", "date-here"))
        self.assertEqual(1, get.call_count)

        self.assertEqual(to_return,
                         reader.layer("layer-here", "lablab", "date-here"))
        self.assertEqual(2, get.call_count)
        param = get.call_args[0][0]
        self.assertTrue('layer-here' in param)
        self.assertTrue('lablab' in param)
        self.assertTrue('date-here' in param)
Ejemplo n.º 8
0
 def test_notice(self, fetch):
     to_return = {'example': 1}
     fetch.return_value = to_return
     reader = ApiReader()
     self.assertEqual(to_return, reader.notice("doc"))
     self.assertTrue(fetch.called)
     param = fetch.call_args[0][0]
     self.assertTrue('doc' in param)
Ejemplo n.º 9
0
 def test_regversion(self, fetch):
     to_return = {}
     fetch.return_value = to_return
     reader = ApiReader()
     self.assertEqual(to_return, reader.regversions('765'))
     self.assertTrue(fetch.called)
     param = fetch.call_args[0][0]
     self.assertTrue('765' in param)
 def test_notice(self, fetch):
     to_return = {'example': 1}
     fetch.return_value = to_return
     reader = ApiReader()
     self.assertEqual(to_return, reader.notice("doc"))
     self.assertTrue(fetch.called)
     param = fetch.call_args[0][0]
     self.assertTrue('doc' in param)
 def test_regversion(self, fetch):
     to_return = {}
     fetch.return_value = to_return
     reader = ApiReader()
     self.assertEqual(to_return, reader.regversions('765'))
     self.assertTrue(fetch.called)
     param = fetch.call_args[0][0]
     self.assertTrue('765' in param)
 def test_notice(self, api_client):
     to_return = {'example': 1}
     api_client.ApiClient.return_value.get.return_value = to_return
     reader = ApiReader()
     self.assertEqual(to_return, reader.notice("doc"))
     get = api_client.ApiClient.return_value.get
     self.assertTrue(get.called)
     param = get.call_args[0][0]
     self.assertTrue('doc' in param)
Ejemplo n.º 13
0
 def test_regulation(self, api_client):
     to_return = {"example": 0, "label": ["204"], "children": []}
     api_client.ApiClient.return_value.get.return_value = to_return
     reader = ApiReader()
     self.assertEqual(to_return, reader.regulation("label-here", "date-here"))
     self.assertTrue(api_client.ApiClient.return_value.get.called)
     param = api_client.ApiClient.return_value.get.call_args[0][0]
     self.assertTrue("label-here" in param)
     self.assertTrue("date-here" in param)
 def test_regversion(self, api_client):
     to_return = {}
     api_client.ApiClient.return_value.get.return_value = to_return
     reader = ApiReader()
     self.assertEqual(to_return, reader.regversions('765'))
     get = api_client.ApiClient.return_value.get
     self.assertTrue(get.called)
     param = get.call_args[0][0]
     self.assertTrue('765' in param)
 def test_notice(self, api_client):
     to_return = {'example': 1}
     api_client.ApiClient.return_value.get.return_value = to_return
     reader = ApiReader()
     self.assertEqual(to_return, reader.notice("doc"))
     get = api_client.ApiClient.return_value.get
     self.assertTrue(get.called)
     param = get.call_args[0][0]
     self.assertTrue('doc' in param)
 def test_regversion(self, api_client):
     to_return = {}
     api_client.ApiClient.return_value.get.return_value = to_return
     reader = ApiReader()
     self.assertEqual(to_return, reader.regversions('765'))
     get = api_client.ApiClient.return_value.get
     self.assertTrue(get.called)
     param = get.call_args[0][0]
     self.assertTrue('765' in param)
 def test_regulation(self, api_client):
     to_return = {'example': 0, 'label': ['204'], 'children': []}
     api_client.ApiClient.return_value.get.return_value = to_return
     reader = ApiReader()
     self.assertEqual(to_return,
                      reader.regulation("label-here", "date-here"))
     self.assertTrue(api_client.ApiClient.return_value.get.called)
     param = api_client.ApiClient.return_value.get.call_args[0][0]
     self.assertTrue('label-here' in param)
     self.assertTrue('date-here' in param)
Ejemplo n.º 18
0
    def test_notices(self, fetch):
        to_return = {'example': 1}
        fetch.return_value = to_return
        reader = ApiReader()
        self.assertEqual(to_return, reader.notices())
        self.assertTrue(fetch.called)

        self.assertEqual(to_return, reader.notices(part='p'))
        self.assertTrue(fetch.called)
        self.assertEqual({'part': 'p'}, fetch.call_args[0][1])
 def test_regulation(self, fetch):
     to_return = {'example': 0, 'label': ['204'], 'children': []}
     fetch.return_value = to_return
     reader = ApiReader()
     self.assertEqual(to_return, reader.regulation("label-here",
                                                   "date-here"))
     self.assertTrue(fetch.called)
     param = fetch.call_args[0][0]
     self.assertTrue('label-here' in param)
     self.assertTrue('date-here' in param)
 def test_regulation(self, api_client):
     to_return = {'example': 0, 'label': ['204'], 'children': []}
     api_client.ApiClient.return_value.get.return_value = to_return
     reader = ApiReader()
     self.assertEqual(to_return,
                      reader.regulation("label-here", "date-here"))
     self.assertTrue(api_client.ApiClient.return_value.get.called)
     param = api_client.ApiClient.return_value.get.call_args[0][0]
     self.assertTrue('label-here' in param)
     self.assertTrue('date-here' in param)
Ejemplo n.º 21
0
 def test_regulation(self, fetch):
     to_return = {'example': 0, 'label': ['204'], 'children': []}
     fetch.return_value = to_return
     reader = ApiReader()
     self.assertEqual(to_return,
                      reader.regulation("label-here", "date-here"))
     self.assertTrue(fetch.called)
     param = fetch.call_args[0][0]
     self.assertTrue('label-here' in param)
     self.assertTrue('date-here' in param)
    def test_notices(self, fetch):
        to_return = {'example': 1}
        fetch.return_value = to_return
        reader = ApiReader()
        self.assertEqual(to_return, reader.notices())
        self.assertTrue(fetch.called)

        self.assertEqual(to_return, reader.notices(part='p'))
        self.assertTrue(fetch.called)
        self.assertEqual({'part': 'p'}, fetch.call_args[0][1])
Ejemplo n.º 23
0
    def test_diff(self, fetch):
        to_return = {'example': 1}
        fetch.return_value = to_return
        reader = ApiReader()
        self.assertEqual(to_return, reader.diff("204", "old", "new"))

        self.assertTrue(fetch.called)
        param = fetch.call_args[0][0]
        self.assertTrue('204' in param)
        self.assertTrue('old' in param)
        self.assertTrue('new' in param)
    def test_diff(self, fetch):
        to_return = {'example': 1}
        fetch.return_value = to_return
        reader = ApiReader()
        self.assertEqual(to_return, reader.diff("204", "old", "new"))

        self.assertTrue(fetch.called)
        param = fetch.call_args[0][0]
        self.assertTrue('204' in param)
        self.assertTrue('old' in param)
        self.assertTrue('new' in param)
    def test_notices(self, api_client):
        to_return = {'example': 1}
        api_client.ApiClient.return_value.get.return_value = to_return
        reader = ApiReader()
        self.assertEqual(to_return, reader.notices())
        get = api_client.ApiClient.return_value.get
        self.assertTrue(get.called)

        self.assertEqual(to_return, reader.notices(part='p'))
        self.assertTrue(get.called)
        self.assertEqual({'part': 'p'}, get.call_args[0][1])
    def test_diff(self, api_client):
        to_return = {'example': 1}
        api_client.ApiClient.return_value.get.return_value = to_return
        reader = ApiReader()
        self.assertEqual(to_return, reader.diff("204", "old", "new"))

        get = api_client.ApiClient.return_value.get
        self.assertTrue(get.called)
        param = get.call_args[0][0]
        self.assertTrue('204' in param)
        self.assertTrue('old' in param)
        self.assertTrue('new' in param)
Ejemplo n.º 27
0
    def test_notices(self, api_client):
        to_return = {"example": 1}
        api_client.ApiClient.return_value.get.return_value = to_return
        reader = ApiReader()
        self.assertEqual(to_return, reader.notices())
        get = api_client.ApiClient.return_value.get
        self.assertTrue(get.called)
        param = get.call_args[0][0]

        self.assertEqual(to_return, reader.notices(part="p"))
        self.assertTrue(get.called)
        self.assertEqual({"part": "p"}, get.call_args[0][1])
    def test_diff(self, api_client):
        to_return = {'example': 1}
        api_client.ApiClient.return_value.get.return_value = to_return
        reader = ApiReader()
        self.assertEqual(to_return, reader.diff("204", "old", "new"))

        get = api_client.ApiClient.return_value.get
        self.assertTrue(get.called)
        param = get.call_args[0][0]
        self.assertTrue('204' in param)
        self.assertTrue('old' in param)
        self.assertTrue('new' in param)
Ejemplo n.º 29
0
    def test_cache_mutability(self, api_client):
        to_return = {"text": "parent", "label": ["1024"], "children": []}
        get = api_client.ApiClient.return_value.get
        get.return_value = to_return
        reader = ApiReader()

        result = reader.regulation("1024", "ver")
        self.assertEqual(to_return, result)
        child = {"text": "child", "children": [], "label": ["1024", "a"]}
        result["children"] = [child]

        second = reader.regulation("1024", "ver")
        self.assertEqual(1, get.call_count)
        self.assertEqual(second, {"text": "parent", "label": ["1024"], "children": []})
Ejemplo n.º 30
0
def notice_data(doc_number):
    preamble = ApiReader().preamble(doc_number.replace('-', '_'))
    if preamble is None:
        raise Http404

    notice = ApiReader().notice(doc_number.replace('_', '-')) or {}

    fields = ("amendments", "cfr_parts", "cfr_title", "comment_doc_id",
              "comments_close", "dockets", "document_number", "effective_on",
              "footnotes", "fr_citation", "fr_url", "fr_volume", "meta",
              "primary_agency", "primary_docket", "publication_date",
              "regulation_id_numbers", "section_by_section",
              "supporting_documents", "title", "versions")

    meta = {}
    for field in fields:
        if field in notice:
            meta[field] = convert_to_python(deepcopy(notice[field]))

    # If there's no metadata, fall back to getting it from settings:
    if not meta:
        meta = getattr(settings, 'PREAMBLE_INTRO', {}).get(doc_number,
                                                           {}).get('meta', {})
        meta = convert_to_python(deepcopy(meta))

    today = date.today()
    if 'comments_close' in meta and 'publication_date' in meta:
        close_date = meta['comments_close'].date()
        publish_date = meta['publication_date'].date()
        if today < publish_date:
            meta['comment_state'] = CommentState.PREPUB
        elif today <= close_date:
            meta['comment_state'] = CommentState.OPEN
            meta['days_remaining'] = 1 + (close_date - today).days
        else:
            meta['comment_state'] = CommentState.CLOSED
    else:
        meta['comment_state'] = CommentState.NO_COMMENT

    # We don't pass along cfr_ref information in a super useful format, yet.
    # Construct one here:
    if 'cfr_refs' not in meta and 'cfr_title' in meta and 'cfr_parts' in meta:
        meta['cfr_refs'] = [{
            "title": meta['cfr_title'],
            "parts": meta['cfr_parts']
        }]

    return preamble, meta, notice
Ejemplo n.º 31
0
    def get(self, request, *args, **kwargs):
        notices = ApiReader().notices().get("results", [])
        context = {}
        notices_meta = []

        for notice in notices:
            try:
                if notice.get("document_number"):
                    _, meta, _ = notice_data(notice["document_number"])
                    notices_meta.append(meta)
            except Http404:
                pass

        notices_meta = sorted(notices_meta,
                              key=itemgetter("publication_date"),
                              reverse=True)

        context["notices"] = notices_meta
        # Django templates won't show contents of CommentState as an Enum, so:
        context["comment_state"] = {
            state.name: state.value
            for state in CommentState
        }

        template = self.template_name
        return TemplateResponse(request=request,
                                template=template,
                                context=context)
Ejemplo n.º 32
0
    def regtext_changes_context(amendments, version_info, label_id,
                                doc_number):
        """Generate diffs for the changed section"""
        cfr_part = label_id.split('-')[0]
        relevant = []
        for amd in amendments:
            changes = amd.get('changes', [])
            if isinstance(changes, dict):
                changes = list(changes.items())
            keys = {change[0] for change in changes}
            if any(is_contained_in(key, label_id) for key in keys):
                relevant.append(amd)

        versions = Versions(version_info[cfr_part]['left'],
                            version_info[cfr_part]['right'])
        tree = ApiReader().regulation(label_id, versions.older)
        appliers = get_appliers(label_id, versions)

        builder = CFRChangeHTMLBuilder(*appliers,
                                       id_prefix=[str(doc_number), 'cfr'])
        builder.tree = tree
        builder.generate_html()
        return {
            'instructions': [a['instruction'] for a in relevant],
            'tree': builder.tree
        }
Ejemplo n.º 33
0
def common_context(doc_number):
    """All of the "preamble" views share common context, such as preamble
    data, toc info, etc. This function retrieves that data and returns the
    results as a dict. This may throw a 404"""
    preamble = ApiReader().preamble(doc_number)
    if preamble is None:
        raise Http404

    # @todo - right now we're shimming in fake data; eventually this data
    # should come from the API
    intro = getattr(settings, 'PREAMBLE_INTRO', {}).get(doc_number, {})
    intro = deepcopy(intro)
    if intro.get('tree'):
        preamble['children'].insert(0, intro['tree'])
    intro['meta'] = convert_to_python(intro.get('meta', {}))
    if 'comments_close' in intro['meta']:
        intro['meta']['days_remaining'] = 1 + (
            intro['meta']['comments_close'].date() - date.today()).days

    return {
        'cfr_change_toc': CFRChangeToC.for_doc_number(doc_number),
        "doc_number": doc_number,
        "meta": intro['meta'],
        "preamble": preamble,
        'preamble_toc': make_preamble_toc(preamble['children']),
    }
Ejemplo n.º 34
0
    def regtext_changes_context(cls, amendments, version_info, label_id,
                                doc_number, toc_position):
        """Generate diffs for the changed section"""
        cfr_part = label_id.split('-')[0]
        relevant = []
        for amd in amendments:
            changes = amd.get('changes', [])
            keys = {change[0] for change in changes}
            if any(is_contained_in(key, label_id) for key in keys):
                relevant.append(amd)

        versions = Versions(version_info[cfr_part]['left'],
                            version_info[cfr_part]['right'])
        left_tree = ApiReader().regulation(label_id, versions.older)
        diff_applier = generator.get_diff_applier(label_id, versions.older,
                                                  versions.newer)
        if diff_applier is None:
            raise error_handling.MissingContentException()

        layers = list(generator.diff_layers(versions, label_id))

        builder = CFRChangeHTMLBuilder(layers,
                                       diff_applier,
                                       id_prefix=[str(doc_number), 'cfr'],
                                       index_prefix=[1, toc_position])
        builder.tree = left_tree or {}
        builder.generate_html()

        return {
            'instructions': [a['instruction'] for a in relevant],
            'subparts':
            list(cls.subpart_changes(doc_number, relevant, label_id)),
            'tree': builder.tree,
        }
    def test_cache_mutability(self, fetch):
        to_return = {'text': 'parent', 'label': ['1024'], 'children': []}
        fetch.return_value = to_return
        reader = ApiReader()

        result = reader.regulation('1024', 'ver')
        self.assertEqual(to_return, result)
        child = {'text': 'child', 'children': [], 'label': ['1024', 'a']}
        result['children'] = [child]

        second = reader.regulation('1024', 'ver')
        self.assertEqual(1, fetch.call_count)
        self.assertEqual(second, {
            'text': 'parent',
            'label': ['1024'],
            'children': []
        })
    def test_layer(self, api_client):
        to_return = {'example': 1}
        api_client.ApiClient.return_value.get.return_value = to_return
        reader = ApiReader()
        self.assertEqual(
            to_return,
            reader.layer("layer-here", "cfr", "label-here", "version-here"))
        get = api_client.ApiClient.return_value.get
        self.assertEqual(1, get.call_count)
        param = api_client.ApiClient.return_value.get.call_args[0][0]
        self.assertIn('layer-here/cfr/version-here/label', param)
        self.assertNotIn('label-here', param)   # only grabs the root, "label"

        #   Cache
        self.assertEqual(
            to_return,
            reader.layer("layer-here", "cfr", "label-abc", "version-here"))
        self.assertEqual(1, get.call_count)

        self.assertEqual(
            to_return,
            reader.layer("layer-here", "cfr", "lablab", "version-here"))
        self.assertEqual(2, get.call_count)
        param = get.call_args[0][0]
        self.assertIn('layer-here/cfr/version-here/lablab', param)

        self.assertEqual(
            to_return,
            reader.layer("layer-here", "preamble", "lablab"))
        self.assertEqual(3, get.call_count)
        param = get.call_args[0][0]
        self.assertIn('layer-here/preamble/lablab', param)
    def test_cache_mutability(self, api_client):
        to_return = {
            'text': 'parent',
            'label': ['1024'],
            'children': []
        }
        get = api_client.ApiClient.return_value.get 
        get.return_value = to_return
        reader = ApiReader()

        result = reader.regulation('1024', 'ver')
        self.assertEqual(to_return, result)
        child = {
            'text':'child',
            'children':[], 
            'label':['1024', 'a']
        }
        result['children'] = [child]

        second = reader.regulation('1024', 'ver')
        self.assertEqual(1, get.call_count)
        self.assertEqual(second, {'text':'parent', 'label':['1024'], 'children':[]})
    def test_reg_cache(self, api_client):
        child = {
            'text': 'child',
            'children': [],
            'label': ['923', 'a']
        }
        to_return = {
            'text': 'parent',
            'label': ['923'],
            'children': [child]
        }
        api_client.ApiClient.return_value.get.return_value = to_return
        reader = ApiReader()

        self.assertEqual(to_return, reader.regulation('923', 'ver'))
        self.assertEqual(to_return, reader.regulation('923', 'ver'))
        self.assertEqual(child, reader.regulation('923-a', 'ver'))
        get = api_client.ApiClient.return_value.get
        self.assertEqual(1, get.call_count)

        get.return_value = to_return
        reader.regulation('923-b', 'ver')
        self.assertEqual(2, get.call_count)
Ejemplo n.º 39
0
    def get(self, request, *args, **kwargs):
        preamble = ApiReader().preamble(kwargs['doc_number'])
        if preamble is None:
            raise Http404

        subtree = find_subtree(preamble, [kwargs['doc_number']])
        if subtree is None:
            raise Http404

        context = generate_html_tree(subtree)
        context.update({
            'preamble': preamble,
        })
        template = 'regulations/comment-review-chrome.html'

        return TemplateResponse(request=request, template=template,
                                context=context)
Ejemplo n.º 40
0
    def get(self, request, *args, **kwargs):
        label_parts = kwargs.get('paragraphs', '').split('/')
        preamble = ApiReader().preamble(label_parts[0])
        if preamble is None:
            raise Http404

        subtree = find_subtree(preamble, label_parts)
        if subtree is None:
            raise Http404

        context = generate_html_tree(subtree)
        template = context['node']['template_name']

        if not request.is_ajax() and request.GET.get('partial') != 'true':
            # Wrap the inner context
            context = {'sub_context': context, 'sub_template': template,
                       'preamble': preamble}
            template = 'regulations/preamble-chrome.html'
        return TemplateResponse(request=request, template=template,
                                context=context)
Ejemplo n.º 41
0
    def test_reg_cache(self, api_client):
        child = {'text': 'child', 'children': [], 'label': ['923', 'a']}
        to_return = {'text': 'parent', 'label': ['923'], 'children': [child]}
        api_client.ApiClient.return_value.get.return_value = to_return
        reader = ApiReader()

        self.assertEqual(to_return, reader.regulation('923', 'ver'))
        self.assertEqual(to_return, reader.regulation('923', 'ver'))
        self.assertEqual(child, reader.regulation('923-a', 'ver'))
        get = api_client.ApiClient.return_value.get
        self.assertEqual(1, get.call_count)

        get.return_value = to_return
        reader.regulation('923-b', 'ver')
        self.assertEqual(2, get.call_count)
Ejemplo n.º 42
0
    def test_reg_cache(self, fetch):
        child = {
            'text': 'child',
            'node_type': 'interp',
            'children': [],
            'label': ['923', 'a', 'Interp'],
            'title': 'Some title'
        }
        child2 = {
            'text': 'child2',
            'node_type': 'interp',
            'children': [],
            'label': ['923', 'Interp', '1']
        }
        child3 = {
            'text': 'child',
            'node_type': 'interp',
            'children': [],
            'label': ['923', 'b', 'Interp'],
        }
        to_return = {
            'text': 'parent',
            'node_type': 'interp',
            'label': ['923', 'Interp'],
            'children': [child, child2, child3]
        }
        fetch.return_value = to_return
        reader = ApiReader()

        reader.regulation('923-Interp', 'ver')
        reader.regulation('923-Interp', 'ver')
        reader.regulation('923-a-Interp', 'ver')
        self.assertEqual(1, fetch.call_count)

        fetch.return_value = child2
        reader.regulation('923-Interp-1', 'ver')
        self.assertEqual(2, fetch.call_count)

        fetch.return_value = child3
        reader.regulation('923-b-Interp', 'ver')
        self.assertEqual(3, fetch.call_count)

        child = {
            'text': 'child',
            'children': [],
            'label': ['923', '1', 'a']
        }
        to_return = {
            'text': 'parent',
            'label': ['923', '1'],
            'children': [child]
        }
        fetch.reset_mock()
        fetch.return_value = to_return
        reader.regulation('923-1', 'ver')
        reader.regulation('923-1', 'ver')
        reader.regulation('923-1-a', 'ver')
        self.assertEqual(2, fetch.call_count)
    def test_reg_cache(self, api_client):
        child = {
            'text': 'child',
            'node_type': 'interp',
            'children': [],
            'label': ['923', 'a', 'Interp'],
            'title': 'Some title'
        }
        child2 = {
            'text': 'child2',
            'node_type': 'interp',
            'children': [],
            'label': ['923', 'Interp', '1']
        }
        child3 = {
            'text': 'child',
            'node_type': 'interp',
            'children': [],
            'label': ['923', 'b', 'Interp'],
        }
        to_return = {
            'text': 'parent',
            'node_type': 'interp',
            'label': ['923', 'Interp'],
            'children': [child, child2, child3]
        }
        get = api_client.ApiClient.return_value.get
        get.return_value = to_return
        reader = ApiReader()

        reader.regulation('923-Interp', 'ver')
        reader.regulation('923-Interp', 'ver')
        reader.regulation('923-a-Interp', 'ver')
        self.assertEqual(1, get.call_count)

        get.return_value = child2
        reader.regulation('923-Interp-1', 'ver')
        self.assertEqual(2, get.call_count)

        get.return_value = child3
        reader.regulation('923-b-Interp', 'ver')
        self.assertEqual(3, get.call_count)

        child = {
            'text': 'child',
            'children': [],
            'label': ['923', '1', 'a']
        }
        to_return = {
            'text': 'parent',
            'label': ['923', '1'],
            'children': [child]
        }
        get.reset_mock()
        get.return_value = to_return
        reader.regulation('923-1', 'ver')
        reader.regulation('923-1', 'ver')
        reader.regulation('923-1-a', 'ver')
        get = api_client.ApiClient.return_value.get
        self.assertEqual(2, get.call_count)
Ejemplo n.º 44
0
    def test_reg_cache(self, api_client):
        child = {
            "text": "child",
            "node_type": "interp",
            "children": [],
            "label": ["923", "a", "Interp"],
            "title": "Some title",
        }
        child2 = {"text": "child2", "node_type": "interp", "children": [], "label": ["923", "Interp", "1"]}
        child3 = {"text": "child", "node_type": "interp", "children": [], "label": ["923", "b", "Interp"]}
        to_return = {
            "text": "parent",
            "node_type": "interp",
            "label": ["923", "Interp"],
            "children": [child, child2, child3],
        }
        get = api_client.ApiClient.return_value.get
        get.return_value = to_return
        reader = ApiReader()

        reader.regulation("923-Interp", "ver")
        reader.regulation("923-Interp", "ver")
        reader.regulation("923-a-Interp", "ver")
        self.assertEqual(1, get.call_count)

        get.return_value = child2
        reader.regulation("923-Interp-1", "ver")
        self.assertEqual(2, get.call_count)

        get.return_value = child3
        reader.regulation("923-b-Interp", "ver")
        self.assertEqual(3, get.call_count)

        child = {"text": "child", "children": [], "label": ["923", "1", "a"]}
        to_return = {"text": "parent", "label": ["923", "1"], "children": [child]}
        get.reset_mock()
        get.return_value = to_return
        reader.regulation("923-1", "ver")
        reader.regulation("923-1", "ver")
        reader.regulation("923-1-a", "ver")
        get = api_client.ApiClient.return_value.get
        self.assertEqual(2, get.call_count)
Ejemplo n.º 45
0
    def test_reg_cache(self, api_client):
        child = {
            'text': 'child',
            'children': [],
            'label': ['923', 'a', 'Interp']
        }
        child2 = {
            'text': 'child2',
            'children': [],
            'label': ['923', 'Interp', '1']
        }
        to_return = {
            'text': 'parent',
            'label': ['923', 'Interp'],
            'children': [child, child2]
        }
        get = api_client.ApiClient.return_value.get
        get.return_value = to_return
        reader = ApiReader()

        reader.regulation('923-Interp', 'ver')
        reader.regulation('923-Interp', 'ver')
        reader.regulation('923-a-Interp', 'ver')
        self.assertEqual(1, get.call_count)

        get.return_value = child2
        reader.regulation('923-Interp-1', 'ver')
        self.assertEqual(2, get.call_count)

        child = {
            'text': 'child',
            'children': [],
            'label': ['923', '1', 'a']
        }
        to_return = {
            'text': 'parent',
            'label': ['923', '1'],
            'children': [child]
        }
        get.reset_mock()
        get.return_value = to_return
        reader.regulation('923-1', 'ver')
        reader.regulation('923-1', 'ver')
        reader.regulation('923-1-a', 'ver')
        get = api_client.ApiClient.return_value.get
        self.assertEqual(2, get.call_count)