From d62c58cae89ce745e746236773f510a1098cf42d Mon Sep 17 00:00:00 2001 From: Kyle Mahan Date: Fri, 17 Apr 2015 16:33:41 +0000 Subject: [PATCH] better formatting of timestamps, add pre to bleach whitelist --- woodwind/tasks.py | 9 +++++---- woodwind/util.py | 6 ++++-- woodwind/views.py | 38 ++++++++++++++++++++++---------------- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/woodwind/tasks.py b/woodwind/tasks.py index 9c644b8..e872cd4 100644 --- a/woodwind/tasks.py +++ b/woodwind/tasks.py @@ -31,8 +31,8 @@ TAG_RE = re.compile(r']*?>') COMMENT_RE = re.compile(r'') logger = logging.getLogger(__name__) -logger.setLevel(logging.DEBUG) -logger.addHandler(logging.StreamHandler(sys.stdout)) +#logger.setLevel(logging.DEBUG) +#logger.addHandler(logging.StreamHandler(sys.stdout)) engine = sqlalchemy.create_engine(Config.SQLALCHEMY_DATABASE_URI) Session = sqlalchemy.orm.sessionmaker(bind=engine) redis = StrictRedis() @@ -249,8 +249,9 @@ def is_content_equal(e1, e2): syntax highlighting (crayon) generates slightly different markup every time it's called. """ - content = TAG_RE.sub('', content) - content = COMMENT_RE.sub('', content) + if content: + content = TAG_RE.sub('', content) + content = COMMENT_RE.sub('', content) return content return (e1.title == e2.title diff --git a/woodwind/util.py b/woodwind/util.py index dea7547..aa949f3 100644 --- a/woodwind/util.py +++ b/woodwind/util.py @@ -1,8 +1,10 @@ import bleach import re -bleach.ALLOWED_TAGS += ['a', 'img', 'p', 'br', 'marquee', 'blink', - 'audio', 'video', 'table', 'tbody', 'td', 'tr'] +bleach.ALLOWED_TAGS += [ + 'a', 'img', 'p', 'br', 'marquee', 'blink', 'audio', 'video', 'table', + 'tbody', 'td', 'tr', 'pre', +] bleach.ALLOWED_ATTRIBUTES.update({ 'img': ['src', 'alt', 'title'], 'audio': ['preload', 'controls', 'src'], diff --git a/woodwind/views.py b/woodwind/views.py index 8b99b69..1a5e4f9 100644 --- a/woodwind/views.py +++ b/woodwind/views.py @@ -380,13 +380,14 @@ def relative_time(dt): now = datetime.datetime.utcnow() diff = now - dt zero = datetime.timedelta(0) - years = diff.days // 365 - hours = diff.seconds // 60 // 60 - minutes = diff.seconds // 60 if diff == zero: pretty = 'Right now' - if diff > zero: + elif diff > zero: + years = diff.days // 365 + hours = diff.seconds // 60 // 60 + minutes = diff.seconds // 60 + if years > 1: pretty = str(years) + ' years ago' elif diff.days == 1: @@ -404,22 +405,27 @@ def relative_time(dt): else: pretty = str(diff.seconds) + ' seconds ago' else: - if years < -1: - pretty = str(-years) + ' years from now' - elif diff.days == -1: + diff = abs(diff) + years = diff.days // 365 + hours = diff.seconds // 60 // 60 + minutes = diff.seconds // 60 + + if years > 1: + pretty = str(years) + ' years from now' + elif diff.days == 1: pretty = 'A day from now' - elif diff.days < -1: - pretty = str(-diff.days) + ' days from now' - elif hours == -1: + elif diff.days > 1: + pretty = str(diff.days) + ' days from now' + elif hours == 1: pretty = 'An hour from now' - elif hours < -1: - pretty = str(-hours) + ' hours from now' - elif minutes == -1: + elif hours > 1: + pretty = str(hours) + ' hours from now' + elif minutes == 1: pretty = 'A minute from now' - elif minutes < -1: - pretty = str(-minutes) + ' minutes from now' + elif minutes > 1: + pretty = str(minutes) + ' minutes from now' else: - pretty = str(-diff.seconds) + ' seconds from now' + pretty = str(diff.seconds) + ' seconds from now' return ''.format(dt.isoformat(), pretty)