summaryrefslogtreecommitdiff
path: root/web.py
diff options
context:
space:
mode:
authorYves Fischer <yvesf-git@xapek.org>2015-11-21 13:27:51 +0100
committerYves Fischer <yvesf-git@xapek.org>2015-11-21 13:37:05 +0100
commit76b682ac3073638fda5d6caa23594dd56bf6f06d (patch)
tree0b398cf3f8ea9f95e6b9727bfb8bc639c9dc8376 /web.py
downloadwatchnews-76b682ac3073638fda5d6caa23594dd56bf6f06d.tar.gz
watchnews-76b682ac3073638fda5d6caa23594dd56bf6f06d.zip
a functional draft
Diffstat (limited to 'web.py')
-rw-r--r--web.py130
1 files changed, 130 insertions, 0 deletions
diff --git a/web.py b/web.py
new file mode 100644
index 0000000..35f2e89
--- /dev/null
+++ b/web.py
@@ -0,0 +1,130 @@
+import data
+
+from ll.xist import xsc, parse
+from ll.xist.ns import html, xml, meta
+from flask import Flask, Response, url_for
+
+import difflib
+
+
+class View:
+
+ @staticmethod
+ def template(title, body):
+ return xsc.Frag(
+ html.DocTypeXHTML10transitional(),
+ "\n",
+ html.html(
+ html.head(
+ html.meta(charset='utf-8'),
+ html.title(title),
+ html.style("""\
+.diff_add {
+color: green;
+}
+
+table.diff {font-family:Courier; border:medium;}
+.diff_header {background-color:#e0e0e0}
+td.diff_header {text-align:right}
+.diff_next {background-color:#c0c0c0}
+.diff_sub {background-color:#ffaaaa}
+
+tr td:nth-child(3) .diff_chg {
+ color: blue;
+}
+tr td:nth-child(6) .diff_chg {
+ color: orangered;
+}""")
+ ),
+ html.body(body)))
+
+ @staticmethod
+ def index(feeds):
+ return View.template("Index", html.ul(
+ list(map(lambda feed:
+ html.li(
+ html.a(feed.title or feed.url, href=url_for('feed', id=feed.id))),
+ feeds))))
+
+ @staticmethod
+ def feed(feed, items):
+ return View.template("Feed {}".format(feed.title or feed.url),
+ html.div(
+ html.a("Back", href=url_for('index')),
+ html.h1(feed.title),
+ *list(map(lambda item: [
+ html.h2(html.a(item.title, href=url_for('item', id=item.id))),
+ html.ul(
+ *map(lambda version:
+ html.li("{} {}".format(version.created_date.strftime("%x %X"),
+ version.title)),
+ item.versions)
+ )
+ ], items))))
+
+ @staticmethod
+ def format_version(a, b):
+ temp = """\
+Title: {0.title}
+Authors: {0.authors}
+Url: {0.url}
+Text:
+{0.text}"""
+ if a == None:
+ adata = []
+ fromdate = ""
+ else:
+ adata = temp.format(a).split("\n")
+ fromdate = a.created_date.strftime("%x %X")
+ todate = b.created_date.strftime("%x %X")
+
+ bdata = temp.format(b).split("\n")
+ table = difflib.HtmlDiff(wrapcolumn=60) \
+ .make_table(adata, bdata,
+ fromdesc=fromdate, todesc=todate)
+
+ table = table.encode('utf-8')
+ node = parse.tree(table, parse.Expat(), parse.NS(
+ html), parse.Node(pool=xsc.Pool(html)))
+ return html.div(node)
+
+ @staticmethod
+ def item(item, versions):
+ versionsA = versions
+ versionsB = [None] + versions[:-1]
+ versions = list(zip(versionsA, versionsB))
+ return View.template("Item: {}".format(item.title),
+ html.div(
+ html.a("Back to {}".format(item.feed.title),
+ href=url_for('feed', id=item.feed.id)),
+ html.h1(item.title),
+ *list(map(lambda versionAB:
+ View.format_version(versionAB[1], versionAB[0]),
+ versions))
+ ))
+
+
+def run():
+ app = Flask(__name__)
+
+ @app.route('/')
+ def index():
+ return View.index(data.Feed.select()).string("utf-8")
+
+ @app.route('/feed/<id>')
+ def feed(id):
+ feed = data.Feed.get(data.Feed.id == id)
+ items = data.Item.select() \
+ .where(data.Item.feed == feed) \
+ .order_by(data.Item.created_date.desc())
+ return View.feed(feed, items).string("utf-8")
+
+ @app.route('/item/<id>')
+ def item(id):
+ item = data.Item.get(data.Item.id == id)
+ versions = data.Version.select() \
+ .where(data.Version.item == item) \
+ .order_by(data.Version.created_date)
+ return View.item(item, list(versions)).string("utf-8")
+
+ app.run()