2017-07-27 10:09:33 +02:00
|
|
|
from flask import render_template, request, jsonify
|
2017-07-25 11:30:04 +02:00
|
|
|
from www import app
|
|
|
|
|
from www import archives
|
2017-07-27 10:09:33 +02:00
|
|
|
import search.archive
|
2017-07-25 11:30:04 +02:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
2017-11-07 14:36:05 +01:00
|
|
|
logging.info(' ------- arch = Archives() -------- ')
|
|
|
|
|
arch = archives.Archives()
|
|
|
|
|
arch.load()
|
|
|
|
|
archives_data = arch.data
|
|
|
|
|
|
|
|
|
|
|
2017-07-25 11:30:04 +02:00
|
|
|
@app.route('/')
|
|
|
|
|
def index():
|
2017-11-07 14:36:05 +01:00
|
|
|
k = archives_data.keys()
|
2017-07-25 11:30:04 +02:00
|
|
|
return render_template("index.html", archives=k)
|
|
|
|
|
|
|
|
|
|
def get_key(kv_tuple):
|
|
|
|
|
|
|
|
|
|
k = kv_tuple[0]
|
|
|
|
|
|
|
|
|
|
# k is of the form "Month_Year" - ex.: "January_2001"
|
|
|
|
|
try:
|
|
|
|
|
return datetime.strptime(k, "%B_%Y")
|
|
|
|
|
except Exception:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
# k is of the form "Month(abv)_Year(abv)" - ex.: "Jan_01"
|
|
|
|
|
try:
|
|
|
|
|
return datetime.strptime(k, "%b_%y")
|
|
|
|
|
except Exception:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
# k is of the form "Year" - ex.: "2001"
|
|
|
|
|
try:
|
|
|
|
|
return datetime.strptime(k, "%Y")
|
|
|
|
|
except Exception:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
@app.route('/<list>')
|
|
|
|
|
def get_list(list):
|
2017-11-07 14:36:05 +01:00
|
|
|
if list in archives_data:
|
2017-07-25 11:30:04 +02:00
|
|
|
d = []
|
2017-11-07 14:36:05 +01:00
|
|
|
for k, v in sorted(archives_data[list].items(), key=get_key, reverse=True):
|
2017-07-25 11:30:04 +02:00
|
|
|
d.append({"name": k, "url": v['url'], "nbr_threads": len(v['threads'])})
|
|
|
|
|
return render_template("list.html", list_name=list, list=d)
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
return 'nee nee'
|
|
|
|
|
|
|
|
|
|
@app.route('/<list>/<sublist>')
|
|
|
|
|
def get_sublist(list, sublist):
|
|
|
|
|
|
2017-07-27 10:09:33 +02:00
|
|
|
print(list)
|
|
|
|
|
print(sublist)
|
|
|
|
|
|
2017-07-25 11:30:04 +02:00
|
|
|
sublist = sublist.replace(' ', '_')
|
2017-11-07 14:36:05 +01:00
|
|
|
if list in archives_data and sublist in archives_data[list]:
|
|
|
|
|
return render_template("threads.html", sublist_name=sublist, threads=archives_data[list][sublist]['threads'])
|
2017-07-25 11:30:04 +02:00
|
|
|
else:
|
|
|
|
|
return 'na na'
|
|
|
|
|
|
|
|
|
|
@app.route('/<list>/<sublist>/<int:index>')
|
|
|
|
|
def get_message(list, sublist, index):
|
|
|
|
|
|
|
|
|
|
sublist = sublist.replace(' ', '_')
|
|
|
|
|
index = int(index)
|
2017-11-07 14:36:05 +01:00
|
|
|
if list in archives_data and sublist in archives_data[list] and index < len(archives_data[list][sublist]['threads']):
|
|
|
|
|
return render_template("message.html", message=archives_data[list][sublist]['threads'][index])
|
2017-07-25 11:30:04 +02:00
|
|
|
else:
|
|
|
|
|
'non non'
|
|
|
|
|
|
|
|
|
|
@app.route('/<list>/<sublist>/<int:index>/<path:follow_ups>')
|
|
|
|
|
def get_follow_ups(list, sublist, index, follow_ups):
|
|
|
|
|
|
|
|
|
|
sublist = sublist.replace(' ', '_')
|
|
|
|
|
index = int(index)
|
|
|
|
|
|
|
|
|
|
ups = follow_ups.split('/')
|
|
|
|
|
follow = []
|
|
|
|
|
for u in ups:
|
|
|
|
|
follow.append(int(u))
|
|
|
|
|
|
2017-11-07 14:36:05 +01:00
|
|
|
if list in archives_data and sublist in archives_data[list] and index < len(archives_data[list][sublist]['threads']):
|
|
|
|
|
message = archives_data[list][sublist]['threads'][index]
|
2017-07-25 11:30:04 +02:00
|
|
|
for f in follow:
|
|
|
|
|
message = message['follow-up'][f]
|
|
|
|
|
return render_template("message.html", message=message)
|
|
|
|
|
else:
|
|
|
|
|
'nope nope'
|
|
|
|
|
|
2017-07-27 10:09:33 +02:00
|
|
|
@app.route('/search')
|
|
|
|
|
def searh():
|
|
|
|
|
|
|
|
|
|
if len(request.args) < 1:
|
2017-11-07 14:36:05 +01:00
|
|
|
k = archives_data.keys()
|
2017-11-06 14:11:18 +01:00
|
|
|
return render_template("search.html", archives=k, fields=['content', 'from(name)', 'from(email)'])
|
2017-07-27 10:09:33 +02:00
|
|
|
|
|
|
|
|
k_arg = request.args.get('keyword')
|
|
|
|
|
l_arg = request.args.get('list')
|
|
|
|
|
sl_arg = request.args.get('sublist')
|
2017-11-06 14:11:18 +01:00
|
|
|
f_arg = request.args.get('field')
|
2017-07-27 10:09:33 +02:00
|
|
|
|
|
|
|
|
if k_arg is None or k_arg.strip() == '':
|
|
|
|
|
return "no keyword..."
|
|
|
|
|
|
|
|
|
|
if l_arg is None:
|
|
|
|
|
return "no list..."
|
|
|
|
|
|
2017-11-07 14:36:05 +01:00
|
|
|
if not (l_arg == "all") and not (l_arg in archives_data):
|
2017-07-27 10:09:33 +02:00
|
|
|
return "list '" + l_arg + "' does not exist"
|
|
|
|
|
|
|
|
|
|
if sl_arg is not None:
|
2017-11-07 14:36:05 +01:00
|
|
|
if not sl_arg in archives_data[l]:
|
2017-07-27 10:09:33 +02:00
|
|
|
return "sublist '" + sl_arg + "' does not exist in list '" + l_arg + "'"
|
|
|
|
|
|
2017-11-06 14:11:18 +01:00
|
|
|
if f_arg == "from(name)":
|
|
|
|
|
f_arg = 'author_name'
|
|
|
|
|
elif f_arg == "from(email)":
|
|
|
|
|
f_arg = 'from'
|
|
|
|
|
|
2017-07-27 10:09:33 +02:00
|
|
|
lists = []
|
|
|
|
|
if l_arg == "all":
|
2017-11-07 14:36:05 +01:00
|
|
|
for k in archives_data.keys():
|
2017-07-27 10:09:33 +02:00
|
|
|
lists.append(k)
|
|
|
|
|
else:
|
|
|
|
|
lists.append(l_arg)
|
|
|
|
|
|
|
|
|
|
################################
|
|
|
|
|
##
|
2017-11-06 14:11:18 +01:00
|
|
|
## need to cache all the below
|
2017-07-27 10:09:33 +02:00
|
|
|
##
|
|
|
|
|
################################
|
|
|
|
|
|
|
|
|
|
results = []
|
2017-11-07 14:28:20 +01:00
|
|
|
|
|
|
|
|
logging.info("search keyword = " + k_arg)
|
|
|
|
|
|
2017-07-27 10:09:33 +02:00
|
|
|
for l in lists:
|
2017-11-06 14:11:18 +01:00
|
|
|
# this makes no sense...
|
2017-07-27 10:09:33 +02:00
|
|
|
a = search.archive.Archive()
|
|
|
|
|
a.load(l)
|
2017-11-06 14:11:18 +01:00
|
|
|
results.append(a.search(keyword=k_arg, field=f_arg))
|
2017-07-27 10:09:33 +02:00
|
|
|
|
2017-11-04 13:34:05 +01:00
|
|
|
## -- sort results?
|
|
|
|
|
search_results = sorted(results, key=get_result_key)
|
|
|
|
|
|
|
|
|
|
return jsonify(result=search_results)
|
|
|
|
|
|
|
|
|
|
def get_result_key(r):
|
|
|
|
|
return r['archive']
|
2017-07-27 10:09:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-07-25 11:30:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|