From 8aeb8d65296b3d0c23e84521020838871bf7c37f Mon Sep 17 00:00:00 2001 From: Deimos Date: Tue, 11 Sep 2018 17:06:38 -0600 Subject: [PATCH] Display published date on link topics if old Uses the "published" time from the Embedly data to display the date that a link was published if it was more than 3 days before the link topic was submitted. --- tildes/tildes/lib/datetime.py | 5 +++++ tildes/tildes/models/topic/topic.py | 10 +++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/tildes/tildes/lib/datetime.py b/tildes/tildes/lib/datetime.py index d1027e3..2a66375 100644 --- a/tildes/tildes/lib/datetime.py +++ b/tildes/tildes/lib/datetime.py @@ -79,6 +79,11 @@ def utc_now() -> datetime: return datetime.now(timezone.utc) +def utc_from_timestamp(timestamp: int) -> datetime: + """Return timezone-aware datetime from a UTC timestamp.""" + return datetime.fromtimestamp(timestamp, timezone.utc) + + def descriptive_timedelta(target: datetime, abbreviate: bool = False) -> str: """Return a descriptive string for how long ago a datetime was. diff --git a/tildes/tildes/models/topic/topic.py b/tildes/tildes/models/topic/topic.py index ebe0b48..207fc5f 100644 --- a/tildes/tildes/models/topic/topic.py +++ b/tildes/tildes/models/topic/topic.py @@ -27,7 +27,7 @@ from titlecase import titlecase from tildes.enums import TopicType from tildes.lib.database import ArrayOfLtree -from tildes.lib.datetime import utc_now +from tildes.lib.datetime import utc_from_timestamp, utc_now from tildes.lib.id import id_to_id36 from tildes.lib.markdown import convert_markdown_to_safe_html from tildes.lib.string import convert_to_url_slug @@ -363,6 +363,7 @@ class Topic(DatabaseModel): @property def content_metadata_for_display(self) -> str: """Return a string of the content's metadata, suitable for display.""" + # pylint: disable=too-many-branches metadata_strings = [] if self.is_text_type: @@ -375,6 +376,13 @@ class Topic(DatabaseModel): elif self.is_link_type: metadata_strings.append(f"{self.link_domain}") + # display the published date if it's more than 3 days before the topic + published_timestamp = self.get_content_metadata("published") + if published_timestamp: + published = utc_from_timestamp(published_timestamp) + if self.created_time - published > timedelta(days=3): + metadata_strings.append(published.strftime("%b %-d %Y")) + return ", ".join(metadata_strings) @property