# HG changeset patch # User Eddie Barraco # Date 1561890417 -7200 # Sun Jun 30 12:26:57 2019 +0200 # Node ID 5f44b4c693319578fd3f584c542527ab2bf785ff # Parent 705896bb653cd65618c7b72ff8dfcc6930093854 Fix the id file usage diff --git a/cogito2/app.py b/cogito2/app.py --- a/cogito2/app.py +++ b/cogito2/app.py @@ -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 @@ 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/") def article_show(id): - file = get_file(id) + file = get_file_by_id(id) try: article = build_from_posix_file(file) except FileNotFoundError: diff --git a/cogito2/database/article_factory.py b/cogito2/database/article_factory.py --- a/cogito2/database/article_factory.py +++ b/cogito2/database/article_factory.py @@ -2,16 +2,18 @@ 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 diff --git a/cogito2/database/filerepository.py b/cogito2/database/filerepository.py --- a/cogito2/database/filerepository.py +++ b/cogito2/database/filerepository.py @@ -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)