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
This commit is contained in:
Brian S. Stephan 2020-03-07 10:08:23 -06:00
parent 3f22b56c09
commit 053e3d96a3
5 changed files with 72 additions and 2 deletions

View File

@ -1,5 +1,9 @@
"""Journal functionality."""
from flask import Blueprint
import logging
from flask import Blueprint, abort, current_app as app
logger = logging.getLogger(__name__)
bp = Blueprint('journal', __name__, url_prefix='/')
@ -7,7 +11,19 @@ bp = Blueprint('journal', __name__, url_prefix='/')
@bp.route('/', defaults={'path': 'index'})
@bp.route('/<path:path>')
def display_journal_entry(path):
return 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):

20
tests/conftest.py Normal file
View File

@ -0,0 +1,20 @@
"""Create the test app and other fixtures."""
import os
import pytest
from incorporealcms import create_app
HERE = os.path.dirname(os.path.abspath(__file__))
@pytest.fixture
def app():
app = create_app(instance_path=os.path.join(HERE, 'instance'))
yield app
@pytest.fixture
def client(app):
return app.test_client()

21
tests/instance/config.py Normal file
View File

@ -0,0 +1,21 @@
LOGGING = {
'version': 1,
'formatters': {
'default': {
'format': '[%(asctime)s %(levelname)-7s %(name)s] %(message)s',
},
},
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'default',
},
},
'loggers': {
'': {
'level': 'DEBUG',
'handlers': ['console'],
},
},
}

View File

@ -0,0 +1,3 @@
# test index
this is some test content

View File

@ -12,3 +12,13 @@ def test_journal_file_resolver_subdir_to_index():
def test_journal_file_resolver_other_requests_fine():
assert journal_file_resolver('foo/baz') == 'journal/foo/baz.md'
def test_journal_file_that_exists(client):
response = client.get('/')
assert response.status_code == 200
def test_journal_file_that_doesnt_exist(client):
response = client.get('/ohuesthaoeusth')
assert response.status_code == 404