91 lines
2.1 KiB
Python
91 lines
2.1 KiB
Python
|
|
from flask import render_template, request, jsonify
|
||
|
|
from www import app
|
||
|
|
import json, logging, os, glob
|
||
|
|
from lxml import etree as et
|
||
|
|
import config
|
||
|
|
|
||
|
|
def list_all(d, ext):
|
||
|
|
|
||
|
|
if not os.path.isdir(d):
|
||
|
|
logging.error(d + " is not a valid directory.")
|
||
|
|
return None
|
||
|
|
|
||
|
|
return [os.path.basename(f) for f in glob.glob(os.path.join(d, "*." + ext))]
|
||
|
|
|
||
|
|
def read_xml(d, fn):
|
||
|
|
fp = os.path.join(d, fn)
|
||
|
|
if not os.path.isfile(fp):
|
||
|
|
return None
|
||
|
|
|
||
|
|
root = et.parse(fp).getroot()
|
||
|
|
xml_data = []
|
||
|
|
for m in root.findall('mails/mail'):
|
||
|
|
nbr_str = m.find('nbr').text
|
||
|
|
date_str = m.find('date').text
|
||
|
|
from_str = m.find('from').text
|
||
|
|
subject_str = m.find('subject').text
|
||
|
|
xml_data.append({'nbr': nbr_str, 'date': date_str, 'from': from_str, 'subject': subject_str})
|
||
|
|
|
||
|
|
return xml_data
|
||
|
|
|
||
|
|
|
||
|
|
def update_nbr_xml(d, fn, nbr, new_nbr):
|
||
|
|
fp = os.path.join(d, fn)
|
||
|
|
if not os.path.isfile(fp):
|
||
|
|
return False
|
||
|
|
|
||
|
|
tr = et.parse(fp)
|
||
|
|
m = tr.xpath(".//nbr[text()='" + nbr + "']")
|
||
|
|
|
||
|
|
if m is None or len(m) > 1:
|
||
|
|
return False
|
||
|
|
|
||
|
|
|
||
|
|
# replace nbr
|
||
|
|
m[0].text = new_nbr
|
||
|
|
|
||
|
|
# order all mails according to their nbr
|
||
|
|
mails = tr.find('mails')
|
||
|
|
print(mails)
|
||
|
|
|
||
|
|
mails[:] = sorted(mails, key=lambda x: float(x.find('nbr').text))
|
||
|
|
|
||
|
|
tr.write(fp)
|
||
|
|
return True
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
@app.route('/')
|
||
|
|
def index():
|
||
|
|
return render_template("index.html")
|
||
|
|
|
||
|
|
@app.route('/xml', methods = ['GET', 'POST'])
|
||
|
|
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)
|
||
|
|
|
||
|
|
@app.route('/xml/<path:fn>', methods = ['GET', 'POST'])
|
||
|
|
def xmlfn(fn):
|
||
|
|
if request.method == 'GET':
|
||
|
|
data = read_xml(config.xml['path'], fn)
|
||
|
|
if data is not None:
|
||
|
|
return render_template("xml.html", fn=fn, data=data)
|
||
|
|
else:
|
||
|
|
return "File: " + fn + "does not exist."
|
||
|
|
elif request.method == 'POST':
|
||
|
|
data = request.form
|
||
|
|
print(data)
|
||
|
|
a = data.get('action')
|
||
|
|
if a == "update":
|
||
|
|
logging.info("POST update for " + fn + " -- " + data.get('nbr') + " ++ " + data.get('new_nbr'))
|
||
|
|
if update_nbr_xml(config.xml['path'], fn, data.get('nbr'), data.get('new_nbr')):
|
||
|
|
return "ok"
|
||
|
|
return "-"
|
||
|
|
|
||
|
|
|
||
|
|
|