index(es)

This commit is contained in:
gauthiier
2020-01-21 11:38:31 +01:00
parent cabfe50777
commit afc71795d1
26 changed files with 151318 additions and 13 deletions
+109 -10
View File
@@ -3,6 +3,7 @@ from www import app
import json, logging, os, glob
from lxml import etree as et
import config
from collections import OrderedDict
def list_all(d, ext):
@@ -12,6 +13,112 @@ def list_all(d, ext):
return [os.path.basename(f) for f in glob.glob(os.path.join(d, "*." + ext))]
@app.route('/')
def top():
return render_template("index.html")
'''
INDEX
'''
def read_index(d, fn):
fp = os.path.join(d, fn)
if not os.path.isfile(fp):
return None
with open(fp) as f:
index_data = json.load(f, object_pairs_hook=OrderedDict)
return index_data
# def add_selected_kw_index(d, fn, kw):
# fp = os.path.join(d, fn)
# if not os.path.isfile(fp):
# return False
# with open(fp) as f:
# index_data = json.load(f)
# if kw not in index_data['orphan']:
# return False
# v = index_data['orphan'].pop(kw)
# if kw not in index_data['selected']:
# index_data['selected'][kw] = []
# index_data['selected'][kw] += v
# with open(fp, 'w') as fout:
# json.dump(index_data, fout, indent=4, sort_keys=True, ensure_ascii=False)
# return True
def modify_selected_kw_index(d, fn, kw, action="add"):
fp = os.path.join(d, fn)
if not os.path.isfile(fp):
return False
with open(fp) as f:
index_data = json.load(f)
if action == 'add':
in_dic = index_data['selected']
out_dic = index_data['orphan']
elif action == 'delete':
out_dic = index_data['selected']
in_dic = index_data['orphan']
else:
return False
if kw not in out_dic:
return False
v = out_dic.pop(kw)
if kw not in in_dic:
in_dic[kw] = []
in_dic[kw] += v
with open(fp, 'w') as fout:
json.dump(index_data, fout, indent=4, sort_keys=True, ensure_ascii=False)
return True
@app.route('/index', methods = ['GET'])
def index():
if request.method == 'GET':
li = list_all(config.index['path'], 'js')
li = sorted(li, key=lambda x: int(x.split('.')[0]))
return render_template("list_files_all.html", title="INDEX [all]", prefix="/index/", files=li)
@app.route('/index/<path:fn>', methods = ['GET', 'POST'])
def indexfn(fn):
if request.method == 'GET':
data = read_index(config.index['path'], fn)
if data is not None:
return render_template("indx.html", fn=fn, selected=data['selected'], orphan=data['orphan'])
else:
return "File: " + fn + "does not exist."
elif request.method == 'POST':
data = request.form
a = data.get('action')
if a == "add":
logging.info("POST ADD " + fn + " -- " + data.get('kw') + " ++ " + data.get('list'))
if modify_selected_kw_index(config.index['path'], fn, data.get('kw')):
return "ok"
elif a == "delete":
logging.info("POST DELETE " + fn + " -- " + data.get('kw') + " ++ " + data.get('list'))
if modify_selected_kw_index(config.index['path'], fn, data.get('kw'), action="delete"):
return "ok"
return "-"
'''
XML
'''
def read_xml(d, fn):
fp = os.path.join(d, fn)
if not os.path.isfile(fp):
@@ -97,17 +204,12 @@ def delete_nbr_xml(d, fn, nbr, date):
tr.write(fp)
return True
@app.route('/')
def index():
return render_template("index.html")
@app.route('/xml', methods = ['GET', 'POST'])
@app.route('/xml', methods = ['GET'])
def xml():
if request.method == 'GET':
li = list_all(config.xml['path'], 'xml')
li = sorted(li, key=lambda x: int(x.split('.')[0]))
return render_template("xml_all.html", files=li)
return render_template("list_files_all.html", title="XML [all]", prefix="/xml/", files=li)
@app.route('/xml/<path:fn>', methods = ['GET', 'POST'])
def xmlfn(fn):
@@ -129,6 +231,3 @@ def xmlfn(fn):
if delete_nbr_xml(config.xml['path'], fn, data.get('nbr'), data.get('date')):
return "ok"
return "-"
+10
View File
@@ -0,0 +1,10 @@
$(document).ready(function(){
$('.add, .delete').click(function(e) {
var li = $(this).parent("li");
$.post('/index/' + li.data("file"), {'action': $(this).attr('class'), 'kw': li.data("kw"), 'list': li.data("list")}, function(d) {
if(d === 'ok') {
location.reload();
}
});
});
});
+28
View File
@@ -0,0 +1,28 @@
<html>
<head>
<meta charset="utf-8">
<title>{{fn}}</title>
<script type="text/javascript" src="{{ url_for('static',filename='jquery-3.2.1.min.js') }}" charset="utf-8"></script>
<script type="text/javascript" src="{{ url_for('static',filename='indx.js') }}"></script>
</head>
<body>
<h1>{{fn}}</h1>
<div id="all">
<h2>Selected</h2>
<ul>
{% for kw, s in selected.items() %}
<li data-file="{{fn}}" data-kw="{{kw}}" data-list="selected">{{kw}} {% for ss in s %} - {{ss}} {% endfor %}<button class="delete">-</button></li>
{% endfor %}
</ul>
<hr>
<hr>
<hr>
<h2>Orphans</h2>
<ul>
{% for kw, s in orphan.items() %}
<li data-file="{{fn}}" data-kw="{{kw}}" data-list="orphan">{{kw}} {% for ss in s %} - {{ss}} {% endfor %}<button class="add">+</button></li>
{% endfor %}
</ul>
</div>
</body>
</html>
@@ -1,14 +1,14 @@
<html>
<head>
<meta charset="utf-8">
<title>XML [all]</title>
<title>{{title}}</title>
</head>
<body>
<h1>XML [all]</h1>
<h1>{{title}}</h1>
<div id="all">
<ul>
{% for f in files %}
<li><a href="/xml/{{f}}">{{f}}</a></li>
<li><a href="{{prefix}}{{f}}">{{f}}</a></li>
{% endfor %}
</ul>
</div>