Brian S. Stephan 053e3d96a3 attempt to load resolved journal files
one step closer to actual functionality, attempt to load the resolved
markdown file in the instance directory, or 404 if it doesn't exist
2020-03-07 10:08:23 -06:00

41 lines
1.2 KiB
Python

"""Journal functionality."""
import logging
from flask import Blueprint, abort, current_app as app
logger = logging.getLogger(__name__)
bp = Blueprint('journal', __name__, url_prefix='/')
@bp.route('/', defaults={'path': 'index'})
@bp.route('/<path:path>')
def display_journal_entry(path):
"""Get the file contents of the requested path and render the file."""
resolved_path = journal_file_resolver(path)
logger.info("received request for path '%s', resolved to '%s'", path, resolved_path)
try:
with app.open_instance_resource(resolved_path, 'r') as entry_file:
logger.debug("file '%s' found", resolved_path)
except FileNotFoundError:
logger.warning("requested path '%s' (resolved path '%s') not found!", path, resolved_path)
abort(404)
else:
return "OK"
return resolved_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'
path = f'journal/{path}.md'
return path