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)
def get(self, request, *args, **kwargs): """ Override GET so that we can catch and propagate any errors. """ try: return super(PartialSectionDiffView, self).get(request, *args, **kwargs) except error_handling.MissingContentException: return error_handling.handle_generic_404(request)
def get(self, request, *args, **kwargs): """ Override GET so that we can catch and propagate any errors. """ try: return super(PartialSectionDiffView, self).get(request, *args, **kwargs) except error_handling.MissingContentException, e: return error_handling.handle_generic_404(request)
def diff_redirect(request, label_id, version): """Handles constructing the diff url by pulling the new version from GET. We check for bad data here (as we can't rely on url regex)""" new_version = request.GET.get('new_version', '') if not re.match(r'^[-\d\w]+$', new_version): return handle_generic_404(request) response = order_diff_versions(label_id, version, new_version) response['Location'] += '?from_version=%s' % version return response
def get(self, request, *args, **kwargs): """Override GET so that we can catch and propagate any errors in the included partial(s)""" try: return super(ChromeView, self).get(request, *args, **kwargs) except BadComponentException as e: return e.response except error_handling.MissingSectionException as e: return error_handling.handle_missing_section_404( request, e.label_id, e.version, e.context) except error_handling.MissingContentException as e: return error_handling.handle_generic_404(request)
def redirect_by_date_get(request, label_id): """Handles date, etc. if they are part of the GET variable. We check for bad data here (as we can't rely on url regex)""" try: year = abs(int(request.GET.get('year', ''))) month = abs(int(request.GET.get('month', ''))) day = abs(int(request.GET.get('day', ''))) if year < 100: # Assume two-digit years are for 2000 year = 2000 + year return redirect_by_date(request, label_id, "%04d" % year, "%02d" % month, "%02d" % day) except ValueError: return handle_generic_404(request)
def redirect_by_date_get(request, label_id): """Handles date, etc. if they are part of the GET variable. We check for bad data here (as we can't rely on url regex)""" try: year = abs(int(request.GET.get('year'))) month = abs(int(request.GET.get('month'))) day = abs(int(request.GET.get('day'))) if year < 100: # Assume two-digit years are for 2000 year = 2000 + year return redirect_by_date(request, label_id, "%04d" % year, "%02d" % month, "%02d" % day) except ValueError: return handle_generic_404(request)
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) label_id = context['label_id'] older = context['version'] newer = context['newer_version'] tree = generator.get_tree_paragraph(label_id, older) if tree is None: #TODO We need a more complicated check here to see if the diffs #add the requested section. If not -> 404 tree = {} appliers = get_appliers(label_id, older, newer) builder = HTMLBuilder(*appliers) builder.tree = tree if not builder.tree: return error_handling.handle_generic_404(self.request) builder.generate_html() child_of_root = builder.tree if builder.tree['node_type'] == REGTEXT: child_of_root = { 'node_type': EMPTYPART, 'children': [builder.tree] } context['tree'] = {'children': [child_of_root]} context['markup_page_type'] = 'diff' regpart = label_id.split('-')[0] old_toc = fetch_toc(regpart, older) diff = generator.get_diff_json(regpart, older, newer) from_version = self.request.GET.get('from_version', older) context['TOC'] = diff_toc(older, newer, old_toc, diff, from_version) context['navigation'] = self.footer_nav(label_id, context['TOC'], older, newer, from_version) 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) label_id = context['label_id'] older = context['version'] newer = context['newer_version'] tree = generator.get_tree_paragraph(label_id, older) if tree is None: #TODO We need a more complicated check here to see if the diffs #add the requested section. If not -> 404 tree = {} appliers = get_appliers(label_id, older, newer) builder = HTMLBuilder(*appliers) builder.tree = tree if not builder.tree: return error_handling.handle_generic_404(self.request) builder.generate_html() child_of_root = builder.tree if builder.tree['node_type'] == REGTEXT: child_of_root = { 'node_type': EMPTYPART, 'children': [builder.tree]} context['tree'] = {'children': [child_of_root]} context['markup_page_type'] = 'diff' regpart = label_id.split('-')[0] old_toc = fetch_toc(regpart, older) diff = generator.get_diff_json(regpart, older, newer) from_version = self.request.GET.get('from_version', older) context['TOC'] = diff_toc(older, newer, old_toc, diff, from_version) context['navigation'] = self.footer_nav(label_id, context['TOC'], older, newer, from_version) return context
def test_handle_generic_404(self): request = RequestFactory().get('/fake-path') with self.assertRaises(Http404): error_handling.handle_generic_404(request)
def test_handle_generic_404(self): request = RequestFactory().get('/fake-path') result = error_handling.handle_generic_404(request) self.assertEqual(result.status_code, 404) self.assertTrue(b'Regulation content not found' in result.content)
raise error_handling.MissingSectionException(label_id, version, context) def get(self, request, *args, **kwargs): """Override GET so that we can catch and propagate any errors in the included partial(s)""" try: return super(ChromeView, self).get(request, *args, **kwargs) except BadComponentException, e: return e.response except error_handling.MissingSectionException, e: return error_handling.handle_missing_section_404( request, e.label_id, e.version, e.context) except error_handling.MissingContentException, e: return error_handling.handle_generic_404(request) def _assert_good(self, response): if response.status_code != 200: raise BadComponentException(response) def add_main_content(self, context): view = self.partial_class() view.request = self.request context['main_content_context'] = view.get_context_data(**context) context['main_content_template'] = view.template_name def diff_redirect_label(self, label_id, toc): """Most of the time, we want diff_redirect to link to *this* section's label. This gives us an out for when we need to link somewhere else."""
def test_handle_generic_404(self): request = RequestFactory().get('/fake-path') result = error_handling.handle_generic_404(request) self.assertEqual(result.status_code, 404) self.assertTrue('Regulation content not found' in result.content)