From 0d3a67d72dac49a17e8554a02f4763ae7c753c4f Mon Sep 17 00:00:00 2001 From: Yves Fischer Date: Fri, 9 Dec 2016 20:50:18 +0100 Subject: cosmetic changes - code reformat - .gitignore more files --- watchnews/web.py | 69 ++++++++++++++++++++++++-------------------------------- 1 file changed, 30 insertions(+), 39 deletions(-) (limited to 'watchnews/web.py') diff --git a/watchnews/web.py b/watchnews/web.py index 2699554..721b4b6 100644 --- a/watchnews/web.py +++ b/watchnews/web.py @@ -1,16 +1,15 @@ from . import data, css -from ll.xist import xsc, parse -from ll.xist.ns import html, chars +from ll.xist import xsc +from ll.xist.ns import html from flask import Flask, url_for -import peewee +import peewee as pw import re import difflib class DiffSupport: - def __init__(self): self.inline_style = False @@ -25,8 +24,10 @@ class DiffSupport: in_change = False for ((line1, diff1), (line2, diff2), flag) in diff: if flag ^ in_change: - if flag: symbol = '↓' - else: symbol = '↑' + if flag: + symbol = '↓' + else: + symbol = '↑' rows.append(html.tr(html.td(' '.join(symbol * 10)), html.td(' '.join(symbol * 10)))) in_change = flag @@ -42,8 +43,8 @@ class DiffSupport: '-': 'diff_sub', '^': 'diff_chg'} actionname = { - '+': 'em', - '-': 'em', + '+': 'strong', + '-': 'strong', '^': 'strong'} elems = [] nextpos = line.find('\x00') @@ -60,7 +61,7 @@ class DiffSupport: elems += [elem] else: elems += [html.span(text, ** - {'class': actionclass[line[nextpos + 1]]})] + {'class': actionclass[line[nextpos + 1]]})] line = line[endpos:] nextpos = line.find('\x00') @@ -68,12 +69,11 @@ class DiffSupport: if line != '': # trailing unchanged text elems += [html.span(line)] - elems += [ html.br() ] + elems += [html.br()] return elems class Difftable(html.div, DiffSupport): - def __init__(self, to_version, from_version=None, inline_style=False): html.div.__init__(self) DiffSupport.__init__(self) @@ -86,11 +86,9 @@ class Difftable(html.div, DiffSupport): def single_version(self, version): self.append(html.table( - html.tr(html.th('Title'), html.td(html.a(version.title, - href=version.url))), + html.tr(html.th('Title'), html.td(html.a(version.title, href=version.url))), html.tr(html.th('Authors'), html.td(version.authors)), - html.tr(html.th('Date'), html.td( - version.created_date.strftime('%x %X'))), + html.tr(html.th('Date'), html.td(version.created_date.strftime('%x %X'))), html.tr(html.th('Text', colspan=2)), html.tr(html.td(map(html.p, version.text.split('\n')), colspan=2)) )) @@ -98,6 +96,7 @@ class Difftable(html.div, DiffSupport): def two_versions(self, to_version, from_version): def prepare_text(text): return re.sub('\n\n\n*', '\n\n', text).split('\n') + from_text = prepare_text(from_version.text) to_text = prepare_text(to_version.text) @@ -126,12 +125,10 @@ class Difftable(html.div, DiffSupport): class ItemWidget(html.div, DiffSupport): - def __init__(self, item): html.div.__init__(self) DiffSupport.__init__(self) - self.append(html.h2(html.a(item.title, - href=url_for('item', id=item.id)))) + self.append(html.h2(html.a(item.title, href=url_for('item', id=item.id)))) versionsFrom = [None] + list(item.versions)[:-1] versionsTo = item.versions versions = list(zip(versionsFrom, versionsTo)) @@ -153,7 +150,6 @@ class ItemWidget(html.div, DiffSupport): class Template: - @staticmethod def template(title, body): return xsc.Frag( @@ -163,14 +159,10 @@ class Template: html.meta(charset='utf-8'), html.title(title), html.style(css.string( - css.Rule('.textdiff', '.diff_add', - backgroundColor='#CEF6CE'), - css.Rule('.textdiff', '.diff_next', - backgroundColor='#c0c0c0'), - css.Rule('.textdiff', '.diff_sub', - backgroundColor='#FFDADA'), - css.Rule('.textdiff', '.diff_chg', - backgroundColor='#E5E1FF') + css.Rule('.textdiff', '.diff_add', backgroundColor='#CEF6CE'), + css.Rule('.textdiff', '.diff_next', backgroundColor='#c0c0c0'), + css.Rule('.textdiff', '.diff_sub', backgroundColor='#FFDADA'), + css.Rule('.textdiff', '.diff_chg', backgroundColor='#E5E1FF') )) ), '\n', html.body(body))) @@ -182,7 +174,7 @@ class Template: html.ul( *map(lambda feed: html.li( html.a(feed.title or feed.url, href=url_for('feed', id=feed.id))), - feeds))) + feeds))) @staticmethod def feed(feed, items): @@ -198,15 +190,13 @@ class Template: versionsA = versions versionsB = [None] + versions[:-1] versions = list(zip(versionsA, versionsB)) - return Template.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), - *map(lambda versionAB: html.div( - Difftable(versionAB[0], versionAB[1]), - html.hr()), - versions))) + return Template.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), + *map(lambda versionAB: html.div( + Difftable(versionAB[0], versionAB[1]), + html.hr()), versions))) def get_app(): @@ -220,13 +210,14 @@ def get_app(): def feed(id): feed = data.Feed.get(data.Feed.id == id) counts = (data.Version.select(data.Version.item.alias('item_id'), - peewee.fn.Count(data.Version.id).alias('count')) + pw.fn.Count(data.Version.id).alias('count')) .group_by(data.Version.item) .alias('counts')) items = (data.Item.select() .join(counts, on=(data.Item.id == counts.c.item_id)) - .where((data.Item.feed == feed) & (counts.c.count > 1)) + .where((data.Item.feed == feed) + & (counts.c.count > 1)) .order_by(data.Item.created_date.desc())) return Template.feed(feed, items).string('utf-8') -- cgit v1.2.1