From f93b5c42ed0969c55fe72d3a7812a6fdee381213 Mon Sep 17 00:00:00 2001 From: Max Goodman Date: Fri, 25 Apr 2014 13:53:30 -0700 Subject: [PATCH] Quantize listing dates to "just now", like in JS. --- r2/r2/lib/template_helpers.py | 21 ++++++++++++++++----- r2/r2/templates/utils.html | 15 ++++----------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/r2/r2/lib/template_helpers.py b/r2/r2/lib/template_helpers.py index dd82dc465..94cf6cc28 100755 --- a/r2/r2/lib/template_helpers.py +++ b/r2/r2/lib/template_helpers.py @@ -22,7 +22,7 @@ from r2.models import * from filters import unsafe, websafe, _force_unicode, _force_utf8 -from r2.lib.utils import UrlParser, timesince, is_subdomain +from r2.lib.utils import UrlParser, timeago, timesince, is_subdomain from r2.lib import hooks from r2.lib.static import static_mtime @@ -281,16 +281,16 @@ def replace_render(listing, item, render_func): if hasattr(item, "promoted") and item.promoted is not None: from r2.lib import promote # promoted links are special in their date handling - replacements['timesince'] = timesince(item._date - - promote.timezone_offset) + replacements['timesince'] = \ + simplified_timesince(item._date - promote.timezone_offset) else: - replacements['timesince'] = timesince(item._date) + replacements['timesince'] = simplified_timesince(item._date) replacements['time_period'] = calc_time_period(item._date) # compute the last edited time here so we don't end up caching it if hasattr(item, "editted") and not isinstance(item.editted, bool): - replacements['lastedited'] = timesince(item.editted) + replacements['lastedited'] = simplified_timesince(item.editted) # Set in front.py:GET_comments() replacements['previous_visits_hex'] = c.previous_visits_hex @@ -558,3 +558,14 @@ def html_datetime(date): def js_timestamp(date): return '%d' % (calendar.timegm(date.timetuple()) * 1000) + + +def simplified_timesince(date, include_tense=True): + if date > timeago("1 minute"): + return _("just now") + + since = [] + since.append(timesince(date)) + if include_tense: + since.append(_("ago")) + return " ".join(since) diff --git a/r2/r2/templates/utils.html b/r2/r2/templates/utils.html index c40d5b4cf..cb2d0453e 100755 --- a/r2/r2/templates/utils.html +++ b/r2/r2/templates/utils.html @@ -24,8 +24,8 @@ import json from r2.models import FakeSubreddit from r2.lib.filters import spaceCompress, unsafe, safemarkdown - from r2.lib.template_helpers import add_sr, js_config, static, html_datetime - from r2.lib.utils import cols, long_datetime, timesince + from r2.lib.template_helpers import add_sr, js_config, static, html_datetime, simplified_timesince + from r2.lib.utils import cols, long_datetime from r2.lib import tracking from datetime import datetime %> @@ -550,17 +550,10 @@ ${unsafe(txt)} ## todo: use pubdate attribute once things are
tags. ## note: comment and link templates will pass a CachedVariable stub as since. <% - now = date.now(g.tz) timestamp_class = unsafe(' class="live-timestamp"') if live else '' %> - @@ -594,7 +587,7 @@ ${unsafe(txt)} <%def name="edited(thing, lastedited=None)"> %if isinstance(thing.editted, datetime): - + %elif thing.editted: * %endif