Browse Source

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.
merge-requests/53/head
Chad Birch 6 years ago
parent
commit
19c027a571
  1. 7
      tildes/tildes/jinja.py
  2. 21
      tildes/tildes/lib/datetime.py
  3. 6
      tildes/tildes/templates/macros/comments.jinja2
  4. 14
      tildes/tildes/templates/macros/datetime.jinja2
  5. 4
      tildes/tildes/templates/macros/messages.jinja2
  6. 4
      tildes/tildes/templates/macros/topics.jinja2
  7. 4
      tildes/tildes/templates/messages.jinja2
  8. 10
      tildes/tildes/templates/topic.jinja2

7
tildes/tildes/jinja.py

@ -7,7 +7,7 @@ from typing import Any
from pyramid.config import Configurator 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.comment import Comment
from tildes.models.group import Group from tildes.models.group import Group
from tildes.models.topic import Topic from tildes.models.topic import Topic
@ -36,7 +36,10 @@ def includeme(config: Configurator) -> None:
settings["jinja2.trim_blocks"] = True settings["jinja2.trim_blocks"] = True
# add custom jinja filters # add custom jinja filters
settings["jinja2.filters"] = {"ago": descriptive_timedelta}
settings["jinja2.filters"] = {
"adaptive_date": adaptive_date,
"ago": descriptive_timedelta,
}
# add custom jinja tests # add custom jinja tests
settings["jinja2.tests"] = { settings["jinja2.tests"] = {

21
tildes/tildes/lib/datetime.py

@ -130,3 +130,24 @@ def descriptive_timedelta(target: datetime, abbreviate: bool = False) -> str:
result = result.replace(",", "") result = result.replace(",", "")
return result 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)

6
tildes/tildes/templates/macros/comments.jinja2

@ -1,7 +1,7 @@
{# Copyright (c) 2018 Tildes contributors <code@tildes.net> #} {# Copyright (c) 2018 Tildes contributors <code@tildes.net> #}
{# SPDX-License-Identifier: AGPL-3.0-or-later #} {# 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 %} {% from 'links.jinja2' import username_linked %}
{% macro render_single_comment(comment) %} {% macro render_single_comment(comment) %}
@ -57,11 +57,11 @@
</span> </span>
{% endif %} {% 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 %} {% if comment.last_edited_time %}
<span class="comment-edited-time"> <span class="comment-edited-time">
(edited {{ time_ago_responsive(comment.last_edited_time) }})
(edited {{ adaptive_date_responsive(comment.last_edited_time) }})
</span> </span>
{% endif %} {% endif %}
{% else %} {% else %}

14
tildes/tildes/templates/macros/datetime.jinja2

@ -1,19 +1,15 @@
{# Copyright (c) 2018 Tildes contributors <code@tildes.net> #} {# Copyright (c) 2018 Tildes contributors <code@tildes.net> #}
{# SPDX-License-Identifier: AGPL-3.0-or-later #} {# SPDX-License-Identifier: AGPL-3.0-or-later #}
{% macro time_ago(datetime) -%}
<time datetime="{{ datetime.strftime('%Y-%m-%dT%H:%M:%SZ') }}" title="{{ datetime.strftime('%Y-%m-%d %H:%M:%S UTC') }}">{{ datetime|ago }}</time>
{% macro time_ago(datetime, abbreviate=False) -%}
<time datetime="{{ datetime.strftime('%Y-%m-%dT%H:%M:%SZ') }}" title="{{ datetime.strftime('%Y-%m-%d %H:%M:%S UTC') }}">{{ datetime|ago(abbreviate) }}</time>
{%- endmacro %} {%- endmacro %}
{% macro time_ago_abbreviated(datetime) -%}
<time datetime="{{ datetime.strftime('%Y-%m-%dT%H:%M:%SZ') }}" title="{{ datetime.strftime('%Y-%m-%d %H:%M:%S UTC') }}">{{ datetime|ago(abbreviate=True) }}</time>
{%- endmacro %}
{% macro time_ago_responsive(datetime, class_=None) -%}
{% macro adaptive_date_responsive(datetime, class_=None) -%}
<time <time
class="time-responsive{{ ' '+class_ if class_ else '' }}" class="time-responsive{{ ' '+class_ if class_ else '' }}"
datetime="{{ datetime.strftime('%Y-%m-%dT%H:%M:%SZ') }}" datetime="{{ datetime.strftime('%Y-%m-%dT%H:%M:%SZ') }}"
title="{{ datetime.strftime('%Y-%m-%d %H:%M:%S UTC') }}" title="{{ datetime.strftime('%Y-%m-%d %H:%M:%S UTC') }}"
data-abbreviated="{{ datetime|ago(abbreviate=True) }}"
><span class="time-responsive-full">{{ datetime|ago(abbreviate=False) }}</span></time>
data-abbreviated="{{ datetime|adaptive_date(abbreviate=True) }}"
><span class="time-responsive-full">{{ datetime|adaptive_date(abbreviate=False) }}</span></time>
{%- endmacro %} {%- endmacro %}

4
tildes/tildes/templates/macros/messages.jinja2

@ -1,7 +1,7 @@
{# Copyright (c) 2018 Tildes contributors <code@tildes.net> #} {# Copyright (c) 2018 Tildes contributors <code@tildes.net> #}
{# SPDX-License-Identifier: AGPL-3.0-or-later #} {# 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 %} {% from 'macros/links.jinja2' import username_linked %}
{% macro render_message(message) %} {% macro render_message(message) %}
@ -13,7 +13,7 @@
<header> <header>
{{ username_linked(message.sender.username) }} {{ username_linked(message.sender.username) }}
{{ time_ago(message.created_time) }}
{{ adaptive_date_responsive(message.created_time) }}
</header> </header>
<div class="message-text" <div class="message-text"

4
tildes/tildes/templates/macros/topics.jinja2

@ -1,7 +1,7 @@
{# Copyright (c) 2018 Tildes contributors <code@tildes.net> #} {# Copyright (c) 2018 Tildes contributors <code@tildes.net> #}
{# SPDX-License-Identifier: AGPL-3.0-or-later #} {# 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 %} {% from 'macros/links.jinja2' import group_linked, username_linked %}
{% macro render_topic_for_listing(topic, show_group=False, rank=None) %} {% macro render_topic_for_listing(topic, show_group=False, rank=None) %}
@ -78,7 +78,7 @@
<div aria-label="Posted by">{{ username_linked(topic.user.username) }}</div> <div aria-label="Posted by">{{ username_linked(topic.user.username) }}</div>
<div aria-label="Post time">{{ time_ago_responsive(topic.created_time) }}</div>
<div aria-label="Post time">{{ adaptive_date_responsive(topic.created_time) }}</div>
</footer> </footer>
{{ topic_voting(topic) }} {{ topic_voting(topic) }}

4
tildes/tildes/templates/messages.jinja2

@ -3,7 +3,7 @@
{% extends 'base_user_menu.jinja2' %} {% 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 %} {% from 'macros/links.jinja2' import username_linked %}
{% block title %}Message Inbox{% endblock %} {% block title %}Message Inbox{% endblock %}
@ -31,7 +31,7 @@
<a href="{{ request.route_url('message_conversation', conversation_id36=conversation.conversation_id36) }}">{{ conversation.subject }}</a> <a href="{{ request.route_url('message_conversation', conversation_id36=conversation.conversation_id36) }}">{{ conversation.subject }}</a>
</td> </td>
<td>{{ username_linked(conversation.other_user(request.user).username) }}</td> <td>{{ username_linked(conversation.other_user(request.user).username) }}</td>
<td class="text-right">{{ time_ago_responsive(conversation.last_activity_time) }}</td>
<td class="text-right">{{ adaptive_date_responsive(conversation.last_activity_time) }}</td>
<td class="text-right">{{ conversation.num_replies + 1 }}</td> <td class="text-right">{{ conversation.num_replies + 1 }}</td>
</tr> </tr>
{% endfor %} {% endfor %}

10
tildes/tildes/templates/topic.jinja2

@ -4,7 +4,7 @@
{% extends 'base.jinja2' %} {% extends 'base.jinja2' %}
{% from 'macros/comments.jinja2' import comment_label_options_template, render_comment_tree with context %} {% 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/forms.jinja2' import markdown_textarea %}
{% from 'macros/links.jinja2' import group_linked, username_linked %} {% from 'macros/links.jinja2' import group_linked, username_linked %}
{% from 'macros/topics.jinja2' import topic_voting with context %} {% from 'macros/topics.jinja2' import topic_voting with context %}
@ -20,7 +20,7 @@
<header> <header>
{{ topic_voting(topic) }} {{ topic_voting(topic) }}
<h1>{{ topic.title }}</h1> <h1>{{ topic.title }}</h1>
<div class="topic-full-byline">Posted {{ time_ago_responsive(topic.created_time) }} by
<div class="topic-full-byline">Posted {{ adaptive_date_responsive(topic.created_time) }} by
{% if request.has_permission('view_author', topic) %} {% if request.has_permission('view_author', topic) %}
{{ username_linked(topic.user.username) }} {{ username_linked(topic.user.username) }}
{% else %} {% else %}
@ -29,7 +29,7 @@
{% if topic.last_edited_time %} {% if topic.last_edited_time %}
<span class="text-italic"> <span class="text-italic">
(edited {{ time_ago_responsive(topic.last_edited_time) }})
(edited {{ adaptive_date_responsive(topic.last_edited_time) }})
</span> </span>
{% endif %} {% endif %}
</div> </div>
@ -306,7 +306,7 @@
<dt>Last comment posted</dt> <dt>Last comment posted</dt>
<dd> <dd>
<a href="{{ comments.most_recent_comment.permalink }}" data-js-hide-sidebar-no-preventdefault> <a href="{{ comments.most_recent_comment.permalink }}" data-js-hide-sidebar-no-preventdefault>
{{ time_ago(topic.last_activity_time) }}
{{ adaptive_date_responsive(topic.last_activity_time) }}
</a> </a>
</dd> </dd>
{% else %} {% else %}
@ -326,7 +326,7 @@
{{ username_linked(entry.user) }} {{ username_linked(entry.user) }}
{% endif %} {% endif %}
{{ entry }} {{ entry }}
<span class="topic-log-entry-time">({{ time_ago_abbreviated(entry.event_time) }})</span>
<span class="topic-log-entry-time">({{ time_ago(entry.event_time, abbreviate=True) }})</span>
</li> </li>
{% endfor %} {% endfor %}
</ol> </ol>

Loading…
Cancel
Save