Пример #1
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)
Пример #3
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)
Пример #4
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)
    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': []
        })
Пример #6
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)
Пример #7
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)
Пример #8
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 = []
    for data in toc[reg_part]:
        if 'Subpart' in data['index']:
            toc_list.append(toc_subpart(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