listserv and www

This commit is contained in:
gauthiier
2017-07-25 11:30:04 +02:00
parent cca498d887
commit 064a05b806
12 changed files with 469 additions and 86 deletions
+10
View File
@@ -0,0 +1,10 @@
from flask import Flask
app = Flask(__name__)
from www import routes
import logging
logging.basicConfig(level=logging.DEBUG)
# from www import archives
+63
View File
@@ -0,0 +1,63 @@
import logging, os, json
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class Archives(metaclass=Singleton):
def __init__(self, archives_dir=None):
if archives_dir==None:
self.archives_dir = "archives/"
else:
self.archives_dir = archives_dir
self.loaded = False
def load(self):
if self.loaded:
return
if not os.path.isdir(self.archives_dir):
logging.error("Archives:: the path - " + self.archives_dir + " - is not a valid directory. Aborting.")
return
arch = [d for d in os.listdir(self.archives_dir) if os.path.isdir(os.path.join(self.archives_dir, d))]
self.data = {}
for a in arch:
logging.info("loading " + a)
archive_path = os.path.join(self.archives_dir, a)
self.data[a] = self.load_archive(archive_path)
logging.info("done.")
def load_archive(self, archive_dir):
if not os.path.isdir(archive_dir):
logging.error("Archives:: the path - " + archive_dir + " - is not a valid directory. Aborting.")
return
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
arch = Archives()
arch.load()
archives_data = arch.data
+91
View File
@@ -0,0 +1,91 @@
from flask import render_template
from www import app
from www import archives
from datetime import datetime
@app.route('/')
def index():
k = archives.archives_data.keys()
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):
if list in archives.archives_data:
d = []
for k, v in sorted(archives.archives_data[list].items(), key=get_key, reverse=True):
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):
sublist = sublist.replace(' ', '_')
if list in archives.archives_data and sublist in archives.archives_data[list]:
return render_template("threads.html", sublist_name=sublist, threads=archives.archives_data[list][sublist]['threads'])
else:
return 'na na'
@app.route('/<list>/<sublist>/<int:index>')
def get_message(list, sublist, index):
sublist = sublist.replace(' ', '_')
index = int(index)
if list in archives.archives_data and sublist in archives.archives_data[list] and index < len(archives.archives_data[list][sublist]['threads']):
return render_template("message.html", message=archives.archives_data[list][sublist]['threads'][index])
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))
if list in archives.archives_data and sublist in archives.archives_data[list] and index < len(archives.archives_data[list][sublist]['threads']):
message = archives.archives_data[list][sublist]['threads'][index]
for f in follow:
message = message['follow-up'][f]
return render_template("message.html", message=message)
else:
'nope nope'
+8
View File
@@ -0,0 +1,8 @@
<html>
<head></head>
<body>
{% for a in archives %}
<a href="/{{ a }}"><h3>{{ a }}</h3></a>
{% endfor %}
</body>
</html>
+10
View File
@@ -0,0 +1,10 @@
<html>
<head></head>
<body>
<ul>
{% for t in list %}
<li><a href="{{ list_name }}/{{ t.name }}"><h3>{{ t.name }} -- {{ t.nbr_threads }}</h3></a></li>
{% endfor %}
</ul>
</body>
</html>
+11
View File
@@ -0,0 +1,11 @@
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h3>{{ message.subject }}</h3>
<h4>{{ message.author_name }}</h4>
<h4>{{ message.date }}</h4>
<p>{{ message.content }} </p>
</body>
</html>
+25
View File
@@ -0,0 +1,25 @@
<html>
<head></head>
<body>
{% macro message(m, index, urlpath)-%}
{% set path = urlpath + '/' + index|string %}
<li>
{{ index }}. <a href="{{ path }}">{{ m.subject }}</a> <i>{{ m.author_name }}</i>
{% if m.get('follow-up') %}
<ul>
{% for msg in m.get('follow-up') %}
{{ message(m=msg, index=loop.index - 1, urlpath=path) }}
{% endfor %}
</ul>
{% endif %}
</li>
{%- endmacro %}
<ul>
{% for m in threads recursive %}
{{ message(m=m, index=loop.index - 1, urlpath=sublist_name) }}
{% endfor %}
</ul>
</body>
</html>