Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Commit

Permalink
Merge pull request #308 from jmcarp/redirect-confirm-get
Browse files Browse the repository at this point in the history
Redirect to preamble on confirm GET.
  • Loading branch information
cmc333333 committed Apr 29, 2016
2 parents a02fef5 + 52ec1d4 commit 748aee9
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
15 changes: 15 additions & 0 deletions regulations/tests/views_preamble_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@ def test_get_integration(self, ApiReader, api_reader, CFRChangeToC):
'4',
)

@patch('regulations.views.preamble.CFRChangeToC')
@patch('regulations.generator.generator.api_reader')
@patch('regulations.views.preamble.ApiReader')
def test_get_top_level_redirect(self, ApiReader, api_reader, CFRChangeToC):
ApiReader.return_value.preamble.return_value = self._mock_preamble
api_reader.ApiReader.return_value.layer.return_value = {
'1-c-x': ['something']
}
view = preamble.PreambleView.as_view()

path = '/preamble/1'
response = view(RequestFactory().get(path), paragraphs='1')
assert_equal(response.status_code, 302)
assert_equal(response.get('Location'), '/preamble/1/c')

@patch('regulations.views.preamble.ApiReader')
def test_get_404(self, ApiReader):
"""When a requested doc is not present, we should return a 404"""
Expand Down
15 changes: 13 additions & 2 deletions regulations/views/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import requests
from django.conf import settings
from django.core.cache import caches
from django.http import JsonResponse
from django.shortcuts import redirect
from django.http import Http404, JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_http_methods
from django.template.response import TemplateResponse
Expand All @@ -16,7 +17,9 @@

from regulations import tasks
from regulations import docket
from regulations.views.preamble import common_context, generate_html_tree
from regulations.views.preamble import (
common_context, generate_html_tree, get_preamble,
first_preamble_section)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -83,6 +86,14 @@ def preview_comment(request):

class SubmitCommentView(View):

def get(self, request, doc_number):
preamble, _ = get_preamble(doc_number)
section = first_preamble_section(preamble)
if section is None:
raise Http404
return redirect(
'chrome_preamble', paragraphs='/'.join(section['label']))

def post(self, request, doc_number):
form_data = {
key: value for key, value in request.POST.items()
Expand Down
34 changes: 29 additions & 5 deletions regulations/views/preamble.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
from datetime import date
from collections import namedtuple

from django.http import Http404
from django.conf import settings
from django.shortcuts import redirect
from django.core.urlresolvers import reverse
from django.http import Http404
from django.template.response import TemplateResponse
from django.views.generic.base import View

Expand Down Expand Up @@ -256,10 +257,7 @@ def section_navigation(preamble_toc, cfr_toc, **ids):
return nav


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"""
def get_preamble(doc_number):
preamble = ApiReader().preamble(doc_number)
if preamble is None:
raise Http404
Expand All @@ -275,6 +273,24 @@ def common_context(doc_number):
intro['meta']['days_remaining'] = 1 + (
intro['meta']['comments_close'].date() - date.today()).days

return preamble, intro


def first_preamble_section(preamble):
return next(
(
node for node in preamble['children']
if not node['label'][-1].startswith('p')
),
None,
)


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, intro = get_preamble(doc_number)
preamble_toc = make_preamble_toc(preamble['children'])

return {
Expand All @@ -296,6 +312,14 @@ def get(self, request, *args, **kwargs):
doc_number = label_parts[0]
context = common_context(doc_number)

# Redirect to first section on top-level preamble
if len(label_parts) == 1:
section = first_preamble_section(context['preamble'])
if not section:
raise Http404
return redirect(
'chrome_preamble', paragraphs='/'.join(section['label']))

subtree = find_subtree(context['preamble'], label_parts)
if subtree is None:
raise Http404
Expand Down

0 comments on commit 748aee9

Please sign in to comment.