List_server_busy/www/routes.py
2019-12-26 18:12:49 +01:00

158 lines
4.2 KiB
Python

from flask import render_template, request, jsonify, send_from_directory
from www import app
import json, logging
from selection import sel
from export import exportxml, exportlist
import urllib.parse
import urllib.request
import config
lists_to_serve = config.listservs['lists_to_serve']
@app.route('/')
def index():
l = sel.lists()
return render_template("index.html", lists=l, tags=sel.tags())
@app.route('/selection')
def selection():
s = sel.format_selection()
return render_template("selection.html", sel=s)
@app.route('/tags', methods = ['GET', 'POST'])
def tags():
if request.method == 'GET':
return render_template("selection_tags.html", tags=sel.tags())
if request.method == 'POST':
data = request.form
a = data.get('action')
r = False
if a == "delete":
r = sel.delete(data.get('tagid'))
elif a == "update":
r = sel.update(data.get('tagid'), data.get('tag'), data.get('desc'))
elif a == "new":
r = sel.new(data.get('tag'), data.get('desc'))
if r:
return "ok"
return "-"
@app.route('/tags_w_lists', methods = ['GET', 'POST'])
def tags_w_lists():
if request.method == 'GET':
return render_template("selection_tags_w_lists.html", tags=sel.tags_w_lists())
if request.method == 'POST':
data = request.form
a = data.get('action')
if a == "dump":
if sel.commit_from_selection():
return "done"
elif a == "delete":
if sel.delete_url(data.get('tag'), data.get('url')):
return "ok"
elif a == "export":
pass
return "-"
@app.route('/export', methods = ['POST'])
def export():
if request.method == 'POST':
data = request.form
a = data.get('action')
if a == "export":
if exportxml.export_selection_tag(data.get('tagid'), xml_out=data.get('exportpath')):
return 'ok'
elif a == "export_all":
if exportxml.export_selection_all():
return 'ok'
return "-"
@app.route('/ex/<path:fn>')
def ex(fn):
if fn == "all":
return render_template("ex_all.html", files=exportlist.list_all())
else:
e = exportlist.get(fn)
if e:
return render_template("chapter.html", chapter=e['chapter'])
return "Request for " + fn + " failed..."
@app.route('/report')
def report():
return jsonify(report=sel.report())
# @app.route('/favicon.ico')
# def favicon():
# return send_from_directory(os.path.join(app.root_path, 'static'),
# 'favicon.ico', mimetype='image/vnd.microsoft.icon')
@app.route('/collect')
def collect():
url = request.args.get('url')
tag = request.args.get('tags')
li = request.args.get('list')
msg = "Added " + url + " to " + tag
commited = sel.commit_dump(li, url, tag)
if not commited:
msg = url + " already exists..."
commited = []
return jsonify({ "msg": msg, "commited": commited, "tag": tag})
def filter_results_selection(res):
h = sel.hashmap()
for r in res:
for rr in r['results']:
for rrr in rr['hits']:
if rrr['url'] in h:
print(rrr['url'] + " in " + h[rrr['url']])
rrr['sel'] = h[rrr['url']]
@app.route('/search')
def searh():
global lists_to_serve
if len(request.args) < 1:
# q: list all table or keep predefined wconfig.lists_to_serve?
# wconfig.lists_to_serve = archive.list_tables_db(config.db['database'], config.db['host'], config.db['user'], config.db['password'])
r = urllib.request.urlopen(config.listservs['url'] + '/lists')
data = json.loads(r.read().decode('utf8'))
lists_to_serve = data['lists']
return render_template("search.html", archives=lists_to_serve, fields=['content', 'from'])
k_arg = request.args.get('keyword')
l_arg = request.args.get('list')
f_arg = request.args.get('field')
if k_arg is None or k_arg.strip() == '':
return "no keyword..."
if l_arg != "all" and l_arg not in lists_to_serve:
return "list '" + l_arg + "' does not exist"
if f_arg not in ['content', 'from']:
return "field '" + f_arg + "' does not exist"
data = {'keyword': k_arg, 'list': l_arg, 'field': f_arg}
val = urllib.parse.urlencode(data)
URL = config.listservs['url'] + '/search?' + val
logging.debug(val)
r = urllib.request.urlopen(config.listservs['url'] + '/search?' + val)
data = json.loads(r.read().decode('utf8'))
filter_results_selection(data['result'])
return jsonify({'result': data['result'], 'tags': sel.tags_list()})