add the ability to redirect a file-looking request to a dir
if the client has requested /foo, and foo is actually a directory, this redirects the client to /foo/
This commit is contained in:
@@ -6,7 +6,7 @@ import os
|
||||
import markdown
|
||||
from flask import Blueprint, Markup, abort
|
||||
from flask import current_app as app
|
||||
from flask import render_template
|
||||
from flask import redirect, render_template
|
||||
from tzlocal import get_localzone
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -19,6 +19,9 @@ md = markdown.Markdown(extensions=['meta', 'tables'])
|
||||
@bp.route('/<path:path>')
|
||||
def display_page(path):
|
||||
"""Get the file contents of the requested path and render the file."""
|
||||
if is_file_path_actually_dir_path(path):
|
||||
return redirect(f'{path}/', code=301)
|
||||
|
||||
resolved_path = resolve_page_file(path)
|
||||
logger.debug("received request for path '%s', resolved to '%s'", path, resolved_path)
|
||||
try:
|
||||
@@ -52,6 +55,22 @@ def resolve_page_file(path):
|
||||
return path
|
||||
|
||||
|
||||
def is_file_path_actually_dir_path(path):
|
||||
"""Check if requested path which looks like a file is actually a directory.
|
||||
|
||||
If, for example, /foo used to be a file (foo.md) which later became a directory,
|
||||
foo/, this returns True. Useful for when I make my structure more complicated
|
||||
than it originally was, or if users are just weird.
|
||||
"""
|
||||
if not path.endswith('/'):
|
||||
logger.debug("requested path '%s' looks like a file", path)
|
||||
if os.path.isdir(f'{app.instance_path}/pages/{path}'):
|
||||
logger.debug("...and it's actually a dir")
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def generate_parent_navs(path):
|
||||
"""Create a series of paths/links to navigate up from the given path."""
|
||||
# derive additional path/location stuff based on path
|
||||
|
||||
Reference in New Issue
Block a user