add method to convert .*/ paths to .*/index

This commit is contained in:
Brian S. Stephan 2020-03-07 08:37:38 -06:00
parent 76bdc28a9f
commit 11073b4811
2 changed files with 28 additions and 1 deletions

View File

@ -4,7 +4,20 @@ from flask import Blueprint
bp = Blueprint('journal_views', __name__, url_prefix='/')
@bp.route('/', defaults={'path': 'INDEX'})
@bp.route('/', defaults={'path': 'index'})
@bp.route('/<path:path>')
def display_journal_entry(path):
return path
def journal_file_resolver(path):
"""Manipulate the request path to find appropriate journal files.
* convert dir requests to index files
Worth noting, Flask already does stuff like convert '/foo/../../../bar' to
'/bar', so we don't need to take care around file access here.
"""
if path.endswith('/'):
path = f'{path}index'
return path

14
tests/test_journal.py Normal file
View File

@ -0,0 +1,14 @@
"""Test journal views and helper methods."""
from incorporealcms.journal import journal_file_resolver
def test_journal_file_resolver_dir_to_index():
assert journal_file_resolver('/foo/') == '/foo/index'
def test_journal_file_resolver_subdir_to_index():
assert journal_file_resolver('/foo/bar/') == '/foo/bar/index'
def test_journal_file_resolver_other_requests_fine():
assert journal_file_resolver('/foo/baz') == '/foo/baz'