fixed routes w/ search module

This commit is contained in:
gauthiier 2017-11-08 11:48:28 +01:00
parent 8fe6b7fc83
commit 1f16ffc91e
4 changed files with 49 additions and 40 deletions

View File

@ -34,7 +34,7 @@ class Archive():
with open(file_path) as fdata: with open(file_path) as fdata:
self.archive[label] = json.load(fdata) self.archive[label] = json.load(fdata)
self.loaded = True self.loaded = True
def search_message(self, keyword, msg, index_str, results, field='content'): def search_message(self, keyword, msg, index_str, results, field='content'):

View File

@ -1,2 +1,2 @@
from www import app from www import app
#app.run(debug=True, use_reloader=False) app.run(debug=True, use_reloader=False)

View File

@ -1,4 +1,5 @@
import logging, os, json import logging, os, json
import search.archive
class Singleton(type): class Singleton(type):
_instances = {} _instances = {}
@ -45,29 +46,35 @@ class Archives(metaclass=Singleton):
logging.info("loading " + a) logging.info("loading " + a)
archive_path = os.path.join(self.archives_dir, a) # archive_path = os.path.join(self.archives_dir, a)
self.data[a] = self.load_archive(archive_path) self.data[a] = self.load_archive(self.archives_dir, a)
logging.info("done.") logging.info("done.")
self.loaded = True self.loaded = True
def load_archive(self, archive_dir): def load_archive(self, archive_dir, archive_name):
if not os.path.isdir(archive_dir): if not os.path.isdir(archive_dir):
logging.error("Archives:: the path - " + archive_dir + " - is not a valid directory. Aborting.") logging.error("Archives:: the path - " + archive_dir + " - is not a valid directory. Aborting.")
return return
files = [f for f in os.listdir(archive_dir) if f.endswith('.json')] archive = search.archive.Archive(archive_dir)
archive.load(archive_name)
return archive
arch = {} # # -- shoudl use Archive in searh module here....
for f in files:
file_path = os.path.join(archive_dir, f)
with open(file_path) as fdata:
arch[f.replace('.json', '')] = json.load(fdata)
return arch # files = [f for f in os.listdir(archive_dir) if f.endswith('.json')]
# arch = {}
# for f in files:
# file_path = os.path.join(archive_dir, f)
# with open(file_path) as fdata:
# arch[f.replace('.json', '')] = json.load(fdata)
# return arch

View File

@ -16,35 +16,35 @@ def index():
k = archives_data.keys() k = archives_data.keys()
return render_template("index.html", archives=k) return render_template("index.html", archives=k)
def get_key(kv_tuple): # def get_key(kv_tuple):
k = kv_tuple[0] # k = kv_tuple[0]
# k is of the form "Month_Year" - ex.: "January_2001" # # k is of the form "Month_Year" - ex.: "January_2001"
try: # try:
return datetime.strptime(k, "%B_%Y") # return datetime.strptime(k, "%B_%Y")
except Exception: # except Exception:
pass # pass
# k is of the form "Month(abv)_Year(abv)" - ex.: "Jan_01" # # k is of the form "Month(abv)_Year(abv)" - ex.: "Jan_01"
try: # try:
return datetime.strptime(k, "%b_%y") # return datetime.strptime(k, "%b_%y")
except Exception: # except Exception:
pass # pass
# k is of the form "Year" - ex.: "2001" # # k is of the form "Year" - ex.: "2001"
try: # try:
return datetime.strptime(k, "%Y") # return datetime.strptime(k, "%Y")
except Exception: # except Exception:
pass # pass
return None # return None
@app.route('/<list>') @app.route('/<list>')
def get_list(list): def get_list(list):
if list in archives_data: if list in archives_data:
d = [] d = []
for k, v in sorted(archives_data[list].items(), key=get_key, reverse=True): for k, v in sorted(archives_data[list].archive.items(), key=search.archive.get_key, reverse=True):
d.append({"name": k, "url": v['url'], "nbr_threads": len(v['threads'])}) d.append({"name": k, "url": v['url'], "nbr_threads": len(v['threads'])})
return render_template("list.html", list_name=list, list=d) return render_template("list.html", list_name=list, list=d)
@ -58,8 +58,8 @@ def get_sublist(list, sublist):
print(sublist) print(sublist)
sublist = sublist.replace(' ', '_') sublist = sublist.replace(' ', '_')
if list in archives_data and sublist in archives_data[list]: if list in archives_data and sublist in archives_data[list].archive:
return render_template("threads.html", sublist_name=sublist, threads=archives_data[list][sublist]['threads']) return render_template("threads.html", sublist_name=sublist, threads=archives_data[list].archive[sublist]['threads'])
else: else:
return 'na na' return 'na na'
@ -68,8 +68,8 @@ def get_message(list, sublist, index):
sublist = sublist.replace(' ', '_') sublist = sublist.replace(' ', '_')
index = int(index) index = int(index)
if list in archives_data and sublist in archives_data[list] and index < len(archives_data[list][sublist]['threads']): if list in archives_data and sublist in archives_data[list].archive and index < len(archives_data[list].archive[sublist]['threads']):
return render_template("message.html", message=archives_data[list][sublist]['threads'][index]) return render_template("message.html", message=archives_data[list].archive[sublist]['threads'][index])
else: else:
'non non' 'non non'
@ -84,8 +84,8 @@ def get_follow_ups(list, sublist, index, follow_ups):
for u in ups: for u in ups:
follow.append(int(u)) follow.append(int(u))
if list in archives_data and sublist in archives_data[list] and index < len(archives_data[list][sublist]['threads']): if list in archives_data and sublist in archives_data[list].archive and index < len(archives_data[list].archive[sublist]['threads']):
message = archives_data[list][sublist]['threads'][index] message = archives_data[list].archive[sublist]['threads'][index]
for f in follow: for f in follow:
message = message['follow-up'][f] message = message['follow-up'][f]
return render_template("message.html", message=message) return render_template("message.html", message=message)
@ -140,10 +140,12 @@ def searh():
logging.info("search keyword = " + k_arg) logging.info("search keyword = " + k_arg)
for l in lists: for l in lists:
# this makes no sense... # this makes no sense...
a = search.archive.Archive() # a = search.archive.Archive()
a.load(l) # a.load(l)
results.append(a.search(keyword=k_arg, field=f_arg))
results.append(archives_data[l].search(keyword=k_arg, field=f_arg))
## -- sort results? ## -- sort results?
search_results = sorted(results, key=get_result_key) search_results = sorted(results, key=get_result_key)