From 19c027a5719f7120cdd7f23fd342acb641fe14ea Mon Sep 17 00:00:00 2001 From: Chad Birch Date: Sun, 30 Dec 2018 15:57:52 -0700 Subject: [PATCH] Switch to "adaptive" date display in most places Previously, dates were always displayed in the relative, "ago" style, but these become pretty unwieldy for longer time spans, especially since the ago library I'm using jumps directly from days to years, so it will show ones like "200 days ago" that are hard to place. This adds an "adaptive" date display method and uses it almost everywhere instead. These will use the "ago" style for shorter periods, and then switch to showing an absolute date for dates longer ago than that. The threshold for the switch is currently set to 7 days. --- tildes/tildes/jinja.py | 7 +++++-- tildes/tildes/lib/datetime.py | 21 +++++++++++++++++++ .../tildes/templates/macros/comments.jinja2 | 6 +++--- .../tildes/templates/macros/datetime.jinja2 | 14 +++++-------- .../tildes/templates/macros/messages.jinja2 | 4 ++-- tildes/tildes/templates/macros/topics.jinja2 | 4 ++-- tildes/tildes/templates/messages.jinja2 | 4 ++-- tildes/tildes/templates/topic.jinja2 | 10 ++++----- 8 files changed, 45 insertions(+), 25 deletions(-) diff --git a/tildes/tildes/jinja.py b/tildes/tildes/jinja.py index 26e68b8..c32ccd6 100644 --- a/tildes/tildes/jinja.py +++ b/tildes/tildes/jinja.py @@ -7,7 +7,7 @@ from typing import Any from pyramid.config import Configurator -from tildes.lib.datetime import descriptive_timedelta +from tildes.lib.datetime import adaptive_date, descriptive_timedelta from tildes.models.comment import Comment from tildes.models.group import Group from tildes.models.topic import Topic @@ -36,7 +36,10 @@ def includeme(config: Configurator) -> None: settings["jinja2.trim_blocks"] = True # add custom jinja filters - settings["jinja2.filters"] = {"ago": descriptive_timedelta} + settings["jinja2.filters"] = { + "adaptive_date": adaptive_date, + "ago": descriptive_timedelta, + } # add custom jinja tests settings["jinja2.tests"] = { diff --git a/tildes/tildes/lib/datetime.py b/tildes/tildes/lib/datetime.py index e97d4ce..c0aaf05 100644 --- a/tildes/tildes/lib/datetime.py +++ b/tildes/tildes/lib/datetime.py @@ -130,3 +130,24 @@ def descriptive_timedelta(target: datetime, abbreviate: bool = False) -> str: result = result.replace(",", "") return result + + +def adaptive_date(target: datetime, abbreviate: bool = False) -> str: + """Return a date string that switches from relative to absolute past a threshold.""" + threshold = timedelta(days=7) + + # if the date is more recent than threshold, return the relative "ago"-style string + if utc_now() - target < threshold: + return descriptive_timedelta(target, abbreviate) + + # if abbreviating, use the short version of month name ("Dec" instead of "December") + if abbreviate: + format_str = "%b %-d" + else: + format_str = "%B %-d" + + # only append the year if it's not the current year + if target.year != utc_now().year: + format_str += ", %Y" + + return target.strftime(format_str) diff --git a/tildes/tildes/templates/macros/comments.jinja2 b/tildes/tildes/templates/macros/comments.jinja2 index 2657bad..e36b82d 100644 --- a/tildes/tildes/templates/macros/comments.jinja2 +++ b/tildes/tildes/templates/macros/comments.jinja2 @@ -1,7 +1,7 @@ {# Copyright (c) 2018 Tildes contributors #} {# SPDX-License-Identifier: AGPL-3.0-or-later #} -{% from 'datetime.jinja2' import time_ago_responsive %} +{% from 'datetime.jinja2' import adaptive_date_responsive %} {% from 'links.jinja2' import username_linked %} {% macro render_single_comment(comment) %} @@ -57,11 +57,11 @@ {% endif %} - {{ time_ago_responsive(comment.created_time, class_="comment-posted-time") }} + {{ adaptive_date_responsive(comment.created_time, class_="comment-posted-time") }} {% if comment.last_edited_time %} - (edited {{ time_ago_responsive(comment.last_edited_time) }}) + (edited {{ adaptive_date_responsive(comment.last_edited_time) }}) {% endif %} {% else %} diff --git a/tildes/tildes/templates/macros/datetime.jinja2 b/tildes/tildes/templates/macros/datetime.jinja2 index 0ddf716..80b31d2 100644 --- a/tildes/tildes/templates/macros/datetime.jinja2 +++ b/tildes/tildes/templates/macros/datetime.jinja2 @@ -1,19 +1,15 @@ {# Copyright (c) 2018 Tildes contributors #} {# SPDX-License-Identifier: AGPL-3.0-or-later #} -{% macro time_ago(datetime) -%} - +{% macro time_ago(datetime, abbreviate=False) -%} + {%- endmacro %} -{% macro time_ago_abbreviated(datetime) -%} - -{%- endmacro %} - -{% macro time_ago_responsive(datetime, class_=None) -%} +{% macro adaptive_date_responsive(datetime, class_=None) -%} + data-abbreviated="{{ datetime|adaptive_date(abbreviate=True) }}" +>{{ datetime|adaptive_date(abbreviate=False) }} {%- endmacro %} diff --git a/tildes/tildes/templates/macros/messages.jinja2 b/tildes/tildes/templates/macros/messages.jinja2 index 7b81726..2896e1d 100644 --- a/tildes/tildes/templates/macros/messages.jinja2 +++ b/tildes/tildes/templates/macros/messages.jinja2 @@ -1,7 +1,7 @@ {# Copyright (c) 2018 Tildes contributors #} {# SPDX-License-Identifier: AGPL-3.0-or-later #} -{% from 'macros/datetime.jinja2' import time_ago %} +{% from 'macros/datetime.jinja2' import adaptive_date_responsive %} {% from 'macros/links.jinja2' import username_linked %} {% macro render_message(message) %} @@ -13,7 +13,7 @@
{{ username_linked(message.sender.username) }} - {{ time_ago(message.created_time) }} + {{ adaptive_date_responsive(message.created_time) }}
#} {# SPDX-License-Identifier: AGPL-3.0-or-later #} -{% from 'macros/datetime.jinja2' import time_ago_responsive %} +{% from 'macros/datetime.jinja2' import adaptive_date_responsive %} {% from 'macros/links.jinja2' import group_linked, username_linked %} {% macro render_topic_for_listing(topic, show_group=False, rank=None) %} @@ -78,7 +78,7 @@
{{ username_linked(topic.user.username) }}
-
{{ time_ago_responsive(topic.created_time) }}
+
{{ adaptive_date_responsive(topic.created_time) }}
{{ topic_voting(topic) }} diff --git a/tildes/tildes/templates/messages.jinja2 b/tildes/tildes/templates/messages.jinja2 index 660edd9..f969de7 100644 --- a/tildes/tildes/templates/messages.jinja2 +++ b/tildes/tildes/templates/messages.jinja2 @@ -3,7 +3,7 @@ {% extends 'base_user_menu.jinja2' %} -{% from 'macros/datetime.jinja2' import time_ago_responsive %} +{% from 'macros/datetime.jinja2' import adaptive_date_responsive %} {% from 'macros/links.jinja2' import username_linked %} {% block title %}Message Inbox{% endblock %} @@ -31,7 +31,7 @@ {{ conversation.subject }} {{ username_linked(conversation.other_user(request.user).username) }} - {{ time_ago_responsive(conversation.last_activity_time) }} + {{ adaptive_date_responsive(conversation.last_activity_time) }} {{ conversation.num_replies + 1 }} {% endfor %} diff --git a/tildes/tildes/templates/topic.jinja2 b/tildes/tildes/templates/topic.jinja2 index 211ea03..615b4fa 100644 --- a/tildes/tildes/templates/topic.jinja2 +++ b/tildes/tildes/templates/topic.jinja2 @@ -4,7 +4,7 @@ {% extends 'base.jinja2' %} {% from 'macros/comments.jinja2' import comment_label_options_template, render_comment_tree with context %} -{% from 'macros/datetime.jinja2' import time_ago, time_ago_abbreviated, time_ago_responsive %} +{% from 'macros/datetime.jinja2' import adaptive_date_responsive, time_ago %} {% from 'macros/forms.jinja2' import markdown_textarea %} {% from 'macros/links.jinja2' import group_linked, username_linked %} {% from 'macros/topics.jinja2' import topic_voting with context %} @@ -20,7 +20,7 @@
{{ topic_voting(topic) }}

{{ topic.title }}

-