M cogito2/app.py +3 -3
@@ 1,4 1,4 @@
-from .database.filerepository import get_file, get_page_files, get_file_count
+from .database.filerepository import get_file_by_id, get_page_files, get_file_count, file_root_path
from .database.article_factory import build_from_posix_file, build_from_posix_files
from .database.markdown import get_html
from math import ceil
@@ 14,12 14,12 @@ def article_archive():
page_count=ceil(get_file_count() / item_per_page)
files = get_page_files(page=page, items_per_page=item_per_page)
- articles = build_from_posix_files(files)
+ articles = build_from_posix_files(files, file_root_path)
return render_template('archive.html', articles=articles, page=page, page_count=page_count)
@app.route("/archive/<path:id>")
def article_show(id):
- file = get_file(id)
+ file = get_file_by_id(id)
try:
article = build_from_posix_file(file)
except FileNotFoundError:
M cogito2/database/article_factory.py +6 -4
@@ 2,16 2,18 @@ from .article import Article
from pathlib import PosixPath
from datetime import datetime
-def build_from_posix_file(posix_file: PosixPath):
+def build_from_posix_file(posix_file: PosixPath, file_root_path=''):
+ file_path = str(posix_file)
+ id = file_path.replace(file_root_path, '').replace('.md', '')
return Article(
- id=str(posix_file),
+ id=id,
content=posix_file.read_text(),
created_at=datetime.fromtimestamp(posix_file.stat().st_ctime)
)
-def build_from_posix_files(posix_files: list):
+def build_from_posix_files(posix_files: list, file_root_path=''):
articles = []
for posix_file in posix_files:
- articles.append(build_from_posix_file(posix_file))
+ articles.append(build_from_posix_file(posix_file, file_root_path))
return articles
M cogito2/database/filerepository.py +3 -3
@@ 1,14 1,14 @@
from pathlib import Path, PosixPath
import os
-file_root_path=os.getenv('DATA_ROOT_DIR', 'data')
+file_root_path=os.getenv('DATA_ROOT_DIR', 'data/')
def get_file_count():
p = Path(file_root_path)
return len(list(p.glob('**/*.md')))
-def get_file(file_path):
- return PosixPath(file_path)
+def get_file_by_id(id):
+ return PosixPath(file_root_path + id + '.md')
def get_page_files(page=0, items_per_page=10):
p = Path(file_root_path)