diff --git a/src/api/document.py b/src/api/api_document.py similarity index 92% rename from src/api/document.py rename to src/api/api_document.py index 0387791..50db2d5 100644 --- a/src/api/document.py +++ b/src/api/api_document.py @@ -6,7 +6,7 @@ import os from data.document import Document -bp = Blueprint('document', __name__, url_prefix='/api/doc') +bp = Blueprint('api_document', __name__, url_prefix='/api/doc') @bp.route('/build') def build(): diff --git a/src/app.py b/src/app.py index be16c7e..e3ae692 100644 --- a/src/app.py +++ b/src/app.py @@ -1,5 +1,6 @@ import os from flask import Flask + import data.document def create_app(): @@ -8,8 +9,13 @@ def create_app(): src_path = os.path.dirname(os.path.realpath(__file__)) data.document.set_document_root(os.path.realpath(src_path+'/../data/doc')) - from api import document - app.register_blueprint(document.bp) + from api import api_document + app.register_blueprint(api_document.bp) + + from web import web_admin + app.register_blueprint(web_admin.bp) + + from web import web_document + app.register_blueprint(web_document.bp) return app - diff --git a/src/data/document.py b/src/data/document.py index 82a67ad..2a4ee34 100644 --- a/src/data/document.py +++ b/src/data/document.py @@ -1,4 +1,5 @@ import os +from types import SimpleNamespace from web_utils.run import run import shutil from unicodedata import normalize @@ -28,15 +29,20 @@ def sanitize_name(initial_name): return name class Document: - def __init__(self, doc_name, branch = 'master'): + def __init__(self, doc_name, branch = 'master', allow_invalid = False): self.doc_name = doc_name self.branch = branch doc_path = Document.make_doc_path(doc_name, branch) if not os.path.isdir(doc_path + "/repo/.git"): - raise Exception("This document does not exist: "+doc_name+"@"+branch) + if allow_invalid: + self.valid = False + return + else: + raise Exception("This document does not exist: "+doc_name+"@"+branch) self.doc_path = doc_path + self.valid = True def build(self): #venv_path = os.getenv('VIRTUAL_ENV') @@ -46,6 +52,9 @@ class Document: def pull(self): return run("cd \"" + self.doc_path + "/repo\" && git pull") + def get_url(self): + return "/doc/" + sanitize_name(self.doc_name)+'/'+sanitize_name(self.branch) + "/index.html" + @staticmethod def make_doc_path(doc_name, branch): doc_path = os.path.realpath(get_document_root()+'/'+sanitize_name(doc_name)+'/'+sanitize_name(branch)) @@ -85,6 +94,15 @@ class Document: # cloning failed, clean up and raise the same exception again shutil.rmtree(doc_path) raise e + + @staticmethod + def list(): + result = [] + for doc_name in os.listdir(get_document_root()): + for branch in os.listdir(get_document_root() + "/" + doc_name): + doc = Document(doc_name, branch, allow_invalid = True) + result.append(doc) + return result def set_document_root(dir): global document_root diff --git a/src/templates/index.html b/src/templates/index.html new file mode 100644 index 0000000..e6a7abe --- /dev/null +++ b/src/templates/index.html @@ -0,0 +1,24 @@ + + + + + + Documentation (admin) + + + +

Administration des documents

+ Nouveau document... +

Liste des documents

+ + + + diff --git a/src/web/web_admin.py b/src/web/web_admin.py new file mode 100644 index 0000000..0604826 --- /dev/null +++ b/src/web/web_admin.py @@ -0,0 +1,9 @@ +from flask import Blueprint, render_template + +from data.document import Document + +bp = Blueprint('web_admin', __name__, url_prefix='/admin') + +@bp.route('/') +def index(): + return render_template("index.html", documents=Document.list()) diff --git a/src/web/web_document.py b/src/web/web_document.py new file mode 100644 index 0000000..47cf0e5 --- /dev/null +++ b/src/web/web_document.py @@ -0,0 +1,12 @@ +import os +from flask import Blueprint, render_template, send_from_directory +from data.document import get_document_root + +from data.document import Document + +bp = Blueprint('web_document', __name__, url_prefix='/doc') + +@bp.route('///') +def index(doc_name, branch, path): + doc = Document(doc_name, branch) + return send_from_directory(doc.doc_path + "/build/html", path)