Skip to content

Commit

Permalink
Redirect /contents/<book uuid>:<page num> to the module
Browse files Browse the repository at this point in the history
  • Loading branch information
karenc committed Aug 28, 2014
1 parent 84814ac commit adea12e
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 5 deletions.
6 changes: 3 additions & 3 deletions cnxarchive/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ def main(global_config, **settings):
load_logging_configuration(logging_config_filepath)

app = Application()
app.add_route('/contents/{ident_hash}{ignore:(/.*)?}.html', 'cnxarchive.views:get_content_html')
app.add_route('/contents/{ident_hash}{ignore:(/.*)?}.json', 'cnxarchive.views:get_content_json')
app.add_route('/contents/{ident_hash:([^:/]*)}{pagenum:(:[0-9]+)?}{ignore:(/.*)?}.html', 'cnxarchive.views:get_content_html')
app.add_route('/contents/{ident_hash:([^:/]*)}{pagenum:(:[0-9]+)?}{ignore:(/.*)?}.json', 'cnxarchive.views:get_content_json')
# app.add_route('/contents/{ident_hash}.snippet', 'cnxarchive.views:get_content_snippet')
app.add_route('/contents/{ident_hash}{ignore:(/.*)?}', 'cnxarchive.views:get_content')
app.add_route('/contents/{ident_hash:([^:/]*)}{pagenum:(:[0-9]+)?}{ignore:(/.*)?}', 'cnxarchive.views:get_content')
app.add_route('/resources/{hash}{ignore:(/.*)?}', 'cnxarchive.views:get_resource')
app.add_route('/exports/{ident_hash}.{type}{ignore:(/.*)?}', 'cnxarchive.views:get_export')
app.add_route('/extras/{ident_hash}', 'cnxarchive.views:get_extra')
Expand Down
48 changes: 48 additions & 0 deletions cnxarchive/tests/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,38 +145,86 @@ def test_route_setup(self):
'get_content_html': (
('/contents/abcd-1234.html', {
'ident_hash': 'abcd-1234',
'pagenum': '',
'ignore': '',
}),
('/contents/abcd-1234/title.html', {
'ident_hash': 'abcd-1234',
'pagenum': '',
'ignore': '/title',
}),
('/contents/abcd-1234:10.html', {
'ident_hash': 'abcd-1234',
'pagenum': ':10',
'ignore': '',
}),
('/contents/abcd-1234:10/title.html', {
'ident_hash': 'abcd-1234',
'pagenum': ':10',
'ignore': '/title',
}),
),
'get_content_json': (
('/contents/abcd-1234.json', {
'ident_hash': 'abcd-1234',
'pagenum': '',
'ignore': '',
}),
('/contents/abcd-1234/title.json', {
'ident_hash': 'abcd-1234',
'pagenum': '',
'ignore': '/title',
}),
('/contents/abcd-1234:10.json', {
'ident_hash': 'abcd-1234',
'pagenum': ':10',
'ignore': '',
}),
('/contents/abcd-1234:10/title.json', {
'ident_hash': 'abcd-1234',
'pagenum': ':10',
'ignore': '/title',
}),
),
'get_content': (
('/contents/abcd-1234', {
'ident_hash': 'abcd-1234',
'pagenum': '',
'ignore': '',
}),
('/contents/abcd-1234:10', {
'ident_hash': 'abcd-1234',
'pagenum': ':10',
'ignore': '',
}),
('/contents/abcd-1234/', {
'ident_hash': 'abcd-1234',
'pagenum': '',
'ignore': '/',
}),
('/contents/abcd-1234:10/', {
'ident_hash': 'abcd-1234',
'pagenum': ':10',
'ignore': '/',
}),
('/contents/abcd-1234/title', {
'ident_hash': 'abcd-1234',
'pagenum': '',
'ignore': '/title',
}),
('/contents/abcd-1234:10/title', {
'ident_hash': 'abcd-1234',
'pagenum': ':10',
'ignore': '/title',
}),
('/contents/abcd-1234/title/', {
'ident_hash': 'abcd-1234',
'pagenum': '',
'ignore': '/title/',
}),
('/contents/abcd-1234:10/title/', {
'ident_hash': 'abcd-1234',
'pagenum': ':10',
'ignore': '/title/',
}),
),
Expand Down
41 changes: 41 additions & 0 deletions cnxarchive/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,47 @@ def test_module_content(self):
# Check the content is the html file.
self.assertTrue(content_text.find('<html') >= 0)

def test_module_content_in_collection_context(self):
uuid = 'e79ffde3-7fb4-4af3-9ec8-df648b391597'
version = '7.1'
page_uuid = 'd395b566-5fe3-4428-bcb2-19016e3aa3ce'

# Build the request environment
environ = self._make_environ()
environ['wsgiorg.routing_args'] = {
'ident_hash': '{}@{}'.format(uuid, version),
'pagenum': ':3',
}

# Call the view, check that it redirects to the module
from ..views import get_content
with self.assertRaises(httpexceptions.HTTPFound) as raiser:
get_content(environ, self._start_response)

e = raiser.exception
self.assertEqual(e.status, '302 Found')
self.assertEqual(e.headers, [
('Location', '/contents/{}@4'.format(page_uuid))])

def test_module_content_in_collection_context_not_found(self):
uuid = 'e79ffde3-7fb4-4af3-9ec8-df648b391597'
version = '7.1'

# Build the request environment
environ = self._make_environ()
environ['wsgiorg.routing_args'] = {
'ident_hash': '{}@{}'.format(uuid, version),
'pagenum': ':100',
}

# Call the view, check that it returns not found
from ..views import get_content
with self.assertRaises(httpexceptions.HTTPNotFound) as raiser:
get_content(environ, self._start_response)

e = raiser.exception
self.assertEqual(e.status, '404 Not Found')

def test_content_without_version(self):
uuid = 'ae3e18de-638d-4738-b804-dc69cd4db3a3'

Expand Down
19 changes: 17 additions & 2 deletions cnxarchive/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,12 @@ def _get_page_in_book(page_uuid, page_version, book_uuid, book_version):
return book_uuid, version


def _get_content_json(environ=None, ident_hash=None):
def _get_content_json(environ=None, ident_hash=None, pagenum=None):
"""Helper that return a piece of content as a dict using the ident-hash (uuid@version)."""
settings = get_settings()
routing_args = environ and environ.get('wsgiorg.routing_args', {}) or {}
if not ident_hash:
ident_hash = environ['wsgiorg.routing_args']['ident_hash']
ident_hash = routing_args['ident_hash']
id, version = split_ident_hash(ident_hash)

with psycopg2.connect(settings[CONNECTION_SETTINGS_KEY]) as db_connection:
Expand All @@ -242,6 +243,20 @@ def _get_content_json(environ=None, ident_hash=None):
tree = cursor.fetchone()[0]
# Must unparse, otherwise we end up double encoding.
result['tree'] = json.loads(tree)

# if there's a page number, redirect to the module
pagenum = pagenum or routing_args.get('pagenum')
if pagenum:
# pagenum may have : at the beginning from the url
pagenum = pagenum.strip(':')
pages = list(flatten_tree_to_ident_hashes(result['tree']))
try:
page = pages[int(pagenum)]
page_id, page_version = split_ident_hash(page)
except (TypeError, ValueError, IndexError):
raise httpexceptions.HTTPNotFound()
redirect_to(cursor, page_id, '/contents/{}@{}',
version=page_version)
else:
# Grab the html content.
args = dict(id=id, version=result['version'],
Expand Down

0 comments on commit adea12e

Please sign in to comment.