Browse Source

Added first admin page and possibility to consult built documents

master
Youen 2 years ago
parent
commit
3a3edd3651
  1. 2
      src/api/api_document.py
  2. 12
      src/app.py
  3. 22
      src/data/document.py
  4. 24
      src/templates/index.html
  5. 9
      src/web/web_admin.py
  6. 12
      src/web/web_document.py

2
src/api/document.py → 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():

12
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

22
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

24
src/templates/index.html

@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>Documentation (admin)</title>
</head>
<body>
<h1>Administration des documents</h1>
<a href="">Nouveau document...</a>
<h2>Liste des documents</h2>
<ul>
{% for doc in documents %}
{% if doc.valid %}
<li>{{ doc.doc_name }} / {{ doc.branch }} <a href="{{ doc.get_url() }}">Consulter</a></li>
{% else %}
<li>{{ doc.doc_name }} / {{ doc.branch }} (document invalide)</li>
{% endif %}
{% endfor %}
</ul>
<script type="text/javascript" src="/static/index.js"></script>
</body>

9
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())

12
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('/<doc_name>/<branch>/<path:path>')
def index(doc_name, branch, path):
doc = Document(doc_name, branch)
return send_from_directory(doc.doc_path + "/build/html", path)
Loading…
Cancel
Save