List_server_busy/www/routes.py

133 lines
3.5 KiB
Python
Raw Normal View History

2019-12-08 21:42:16 +01:00
from flask import render_template, request, jsonify, send_from_directory
from www import app
2019-12-09 13:45:24 +01:00
import json, logging
from selection import sel
import urllib.parse
import urllib.request
2019-12-23 14:54:12 +01:00
import config
2019-12-08 21:42:16 +01:00
2019-12-23 14:54:12 +01:00
lists_to_serve = config.listservs['lists_to_serve']
2019-12-08 21:42:16 +01:00
@app.route('/')
def index():
2019-12-09 13:45:24 +01:00
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)
2019-12-08 21:42:16 +01:00
2019-12-23 14:54:12 +01:00
@app.route('/tags', methods = ['GET', 'POST'])
2019-12-08 21:42:16 +01:00
def tags():
2019-12-23 14:54:12 +01:00
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"
2019-12-26 11:42:35 +01:00
if a == "delete":
if sel.delete_url(data.get('tag'), data.get('url')):
return "ok"
return "-"
2019-12-08 21:42:16 +01:00
@app.route('/report')
def report():
2019-12-09 13:45:24 +01:00
return jsonify(report=sel.report())
2019-12-08 21:42:16 +01:00
# @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
2019-12-09 13:45:24 +01:00
commited = sel.commit_dump(li, url, tag)
if not commited:
2019-12-08 21:42:16 +01:00
msg = url + " already exists..."
2019-12-09 13:45:24 +01:00
commited = []
2019-12-23 14:54:12 +01:00
return jsonify({ "msg": msg, "commited": commited, "tag": tag})
2019-12-09 13:45:24 +01:00
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'])
2019-12-23 14:54:12 +01:00
r = urllib.request.urlopen(config.listservs['url'] + '/lists')
2019-12-09 13:45:24 +01:00
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')
2019-12-21 14:13:16 +01:00
f_arg = request.args.get('field')
2019-12-09 13:45:24 +01:00
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)
2019-12-23 14:54:12 +01:00
URL = config.listservs['url'] + '/search?' + val
2019-12-09 13:45:24 +01:00
2019-12-21 14:13:16 +01:00
logging.debug(val)
2019-12-23 14:54:12 +01:00
r = urllib.request.urlopen(config.listservs['url'] + '/search?' + val)
2019-12-09 13:45:24 +01:00
data = json.loads(r.read().decode('utf8'))
filter_results_selection(data['result'])
2019-12-08 21:42:16 +01:00
2019-12-23 14:54:12 +01:00
return jsonify({'result': data['result'], 'tags': sel.tags_list()})
2019-12-09 13:45:24 +01:00
2019-12-08 21:42:16 +01:00