add method to from resource path to request path
This commit is contained in:
parent
1c40f45ffd
commit
4dcc1c91c2
|
@ -2,6 +2,7 @@
|
||||||
import datetime
|
import datetime
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
|
|
||||||
from flask import Blueprint, Markup, abort
|
from flask import Blueprint, Markup, abort
|
||||||
from flask import current_app as app
|
from flask import current_app as app
|
||||||
|
@ -110,6 +111,15 @@ def request_path_to_instance_resource_path(path):
|
||||||
return absolute_resource.replace(f'{app.instance_path}{os.path.sep}', '')
|
return absolute_resource.replace(f'{app.instance_path}{os.path.sep}', '')
|
||||||
|
|
||||||
|
|
||||||
|
def instance_resource_path_to_request_path(path):
|
||||||
|
"""Reverse a (presumed to exist) disk path to the canonical path that would show up in a Flask route.
|
||||||
|
|
||||||
|
This does not include the leading /, so aside from the root index case, this should be
|
||||||
|
bidirectional.
|
||||||
|
"""
|
||||||
|
return re.sub(r'^pages/', '', re.sub(r'.md$', '', re.sub(r'index.md$', '', path)))
|
||||||
|
|
||||||
|
|
||||||
def resolve_page_file(path):
|
def resolve_page_file(path):
|
||||||
"""Manipulate the request path to find appropriate page file.
|
"""Manipulate the request path to find appropriate page file.
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,8 @@
|
||||||
import pytest
|
import pytest
|
||||||
from werkzeug.http import dump_cookie
|
from werkzeug.http import dump_cookie
|
||||||
|
|
||||||
from incorporealcms.pages import (generate_parent_navs, render, request_path_to_instance_resource_path,
|
from incorporealcms.pages import (generate_parent_navs, instance_resource_path_to_request_path, render,
|
||||||
resolve_page_file)
|
request_path_to_instance_resource_path, resolve_page_file)
|
||||||
|
|
||||||
|
|
||||||
def test_resolve_page_file_dir_to_index():
|
def test_resolve_page_file_dir_to_index():
|
||||||
|
@ -146,3 +146,39 @@ def test_request_path_to_instance_resource_path_absolute_file_errors(app):
|
||||||
with app.test_request_context():
|
with app.test_request_context():
|
||||||
with pytest.raises(PermissionError):
|
with pytest.raises(PermissionError):
|
||||||
assert request_path_to_instance_resource_path('/etc/hosts')
|
assert request_path_to_instance_resource_path('/etc/hosts')
|
||||||
|
|
||||||
|
|
||||||
|
def test_instance_resource_path_to_request_path_on_index(app):
|
||||||
|
"""Test index.md -> /."""
|
||||||
|
with app.test_request_context():
|
||||||
|
assert instance_resource_path_to_request_path('index.md') == ''
|
||||||
|
|
||||||
|
|
||||||
|
def test_instance_resource_path_to_request_path_on_page(app):
|
||||||
|
"""Test no-title.md -> no-title."""
|
||||||
|
with app.test_request_context():
|
||||||
|
assert instance_resource_path_to_request_path('no-title.md') == 'no-title'
|
||||||
|
|
||||||
|
|
||||||
|
def test_instance_resource_path_to_request_path_on_subdir(app):
|
||||||
|
"""Test subdir/index.md -> subdir/."""
|
||||||
|
with app.test_request_context():
|
||||||
|
assert instance_resource_path_to_request_path('subdir/index.md') == 'subdir/'
|
||||||
|
|
||||||
|
|
||||||
|
def test_instance_resource_path_to_request_path_on_subdir_and_page(app):
|
||||||
|
"""Test subdir/page.md -> subdir/page."""
|
||||||
|
with app.test_request_context():
|
||||||
|
assert instance_resource_path_to_request_path('subdir/page.md') == 'subdir/page'
|
||||||
|
|
||||||
|
|
||||||
|
def test_request_resource_request_root(app):
|
||||||
|
"""Test that a request can resolve to a resource and back to a request."""
|
||||||
|
with app.test_request_context():
|
||||||
|
instance_resource_path_to_request_path(request_path_to_instance_resource_path('index')) == ''
|
||||||
|
|
||||||
|
|
||||||
|
def test_request_resource_request_page(app):
|
||||||
|
"""Test that a request can resolve to a resource and back to a request."""
|
||||||
|
with app.test_request_context():
|
||||||
|
instance_resource_path_to_request_path(request_path_to_instance_resource_path('no-title')) == 'no-title'
|
||||||
|
|
Loading…
Reference in New Issue