have file handler return render type rather than bool

for when we have further types to render
This commit is contained in:
Brian S. Stephan 2021-04-15 20:36:30 -05:00
parent be88c3c1bc
commit 71ead20f3f
2 changed files with 36 additions and 33 deletions

View File

@ -21,7 +21,7 @@ bp = Blueprint('pages', __name__, url_prefix='/')
def display_page(path):
"""Get the file contents of the requested path and render the file."""
try:
resolved_path, render_md = request_path_to_instance_resource_path(path)
resolved_path, render_type = request_path_to_instance_resource_path(path)
logger.debug("received request for path '%s', resolved to '%s'", path, resolved_path)
except PermissionError:
abort(400)
@ -30,31 +30,34 @@ def display_page(path):
except FileNotFoundError:
abort(404)
if not render_md:
if render_type == 'file':
return send_from_directory(app.instance_path, resolved_path)
elif render_type == 'markdown':
try:
with app.open_instance_resource(resolved_path, 'r') as entry_file:
mtime = datetime.datetime.fromtimestamp(os.path.getmtime(entry_file.name), get_localzone())
entry = entry_file.read()
except OSError:
logger.exception("resolved path '%s' could not be opened!", resolved_path)
abort(500)
else:
md = init_md()
content = Markup(md.convert(entry))
logger.debug("file metadata: %s", md.Meta)
try:
with app.open_instance_resource(resolved_path, 'r') as entry_file:
mtime = datetime.datetime.fromtimestamp(os.path.getmtime(entry_file.name), get_localzone())
entry = entry_file.read()
except OSError:
logger.exception("resolved path '%s' could not be opened!", resolved_path)
abort(500)
parent_navs = generate_parent_navs(resolved_path)
page_name = (get_meta_str(md, 'title') if md.Meta.get('title') else
f'/{instance_resource_path_to_request_path(resolved_path)}')
page_title = f'{page_name} - {app.config["TITLE_SUFFIX"]}' if page_name else app.config['TITLE_SUFFIX']
logger.debug("title (potentially derived): %s", page_title)
return render('base.html', title=page_title, description=get_meta_str(md, 'description'),
image=get_meta_str(md, 'image'), base_url=request.base_url, content=content,
navs=parent_navs, mtime=mtime.strftime('%Y-%m-%d %H:%M:%S %Z'))
else:
md = init_md()
content = Markup(md.convert(entry))
logger.debug("file metadata: %s", md.Meta)
parent_navs = generate_parent_navs(resolved_path)
page_name = (get_meta_str(md, 'title') if md.Meta.get('title') else
f'/{instance_resource_path_to_request_path(resolved_path)}')
page_title = f'{page_name} - {app.config["TITLE_SUFFIX"]}' if page_name else app.config['TITLE_SUFFIX']
logger.debug("title (potentially derived): %s", page_title)
return render('base.html', title=page_title, description=get_meta_str(md, 'description'),
image=get_meta_str(md, 'image'), base_url=request.base_url, content=content, navs=parent_navs,
mtime=mtime.strftime('%Y-%m-%d %H:%M:%S %Z'))
logger.exception("unsupported render_type '%s'!?", render_type)
abort(500)
def request_path_to_instance_resource_path(path):
@ -84,7 +87,7 @@ def request_path_to_instance_resource_path(path):
absolute_resource = os.path.join(resolved_path, 'index.md')
elif os.path.exists(resolved_path):
logger.info("final DIRECT path = '%s' for request '%s'", resolved_path, path)
return resolved_path.replace(f'{app.instance_path}{os.path.sep}', ''), False
return resolved_path.replace(f'{app.instance_path}{os.path.sep}', ''), 'file'
else:
absolute_resource = f'{resolved_path}.md'
@ -95,7 +98,7 @@ def request_path_to_instance_resource_path(path):
logger.info("final path = '%s' for request '%s'", absolute_resource, path)
# we checked that the file exists via absolute path, but now we need to give the path relative to instance dir
return absolute_resource.replace(f'{app.instance_path}{os.path.sep}', ''), True
return absolute_resource.replace(f'{app.instance_path}{os.path.sep}', ''), 'markdown'
def instance_resource_path_to_request_path(path):

View File

@ -70,45 +70,45 @@ def test_render_with_no_user_theme(app):
def test_request_path_to_instance_resource_path(app):
"""Test a normal URL request is transformed into the file path."""
with app.test_request_context():
assert request_path_to_instance_resource_path('index') == ('pages/index.md', True)
assert request_path_to_instance_resource_path('index') == ('pages/index.md', 'markdown')
def test_request_path_to_instance_resource_path_direct_file(app):
"""Test a normal URL request is transformed into the file path."""
with app.test_request_context():
assert request_path_to_instance_resource_path('no-title') == ('pages/no-title.md', True)
assert request_path_to_instance_resource_path('no-title') == ('pages/no-title.md', 'markdown')
def test_request_path_to_instance_resource_path_in_subdir(app):
"""Test a normal URL request is transformed into the file path."""
with app.test_request_context():
assert request_path_to_instance_resource_path('subdir/page') == ('pages/subdir/page.md', True)
assert request_path_to_instance_resource_path('subdir/page') == ('pages/subdir/page.md', 'markdown')
def test_request_path_to_instance_resource_path_subdir_index(app):
"""Test a normal URL request is transformed into the file path."""
with app.test_request_context():
assert request_path_to_instance_resource_path('subdir/') == ('pages/subdir/index.md', True)
assert request_path_to_instance_resource_path('subdir/') == ('pages/subdir/index.md', 'markdown')
def test_request_path_to_instance_resource_path_relatives_walked(app):
"""Test a normal URL request is transformed into the file path."""
with app.test_request_context():
assert (request_path_to_instance_resource_path('subdir/more-subdir/../../more-metadata') ==
('pages/more-metadata.md', True))
('pages/more-metadata.md', 'markdown'))
def test_request_path_to_instance_resource_path_relatives_walked_indexes_work_too(app):
"""Test a normal URL request is transformed into the file path."""
with app.test_request_context():
assert request_path_to_instance_resource_path('subdir/more-subdir/../../') == ('pages/index.md', True)
assert request_path_to_instance_resource_path('subdir/more-subdir/../../') == ('pages/index.md', 'markdown')
def test_request_path_to_instance_resource_path_relatives_walked_into_subdirs_also_fine(app):
"""Test a normal URL request is transformed into the file path."""
with app.test_request_context():
assert (request_path_to_instance_resource_path('subdir/more-subdir/../../subdir/page') ==
('pages/subdir/page.md', True))
('pages/subdir/page.md', 'markdown'))
def test_request_path_to_instance_resource_path_permission_error_on_ref_above_pages(app):
@ -129,7 +129,7 @@ def test_request_path_to_instance_resource_path_actual_file(app):
"""Test that a request for e.g. '/foo.png' when foo.png is a real file works."""
with app.test_request_context():
assert (request_path_to_instance_resource_path('bss-square-no-bg.png') ==
('pages/bss-square-no-bg.png', False))
('pages/bss-square-no-bg.png', 'file'))
def test_request_path_to_instance_resource_path_nonexistant_file_errors(app):