From 65b554986c2d109c117d11792ea5aa4728601e45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Tue, 8 Nov 2016 12:56:19 +0100 Subject: [PATCH] pretty_date(None) now returns None --- pillar/web/nodes/custom/comments.py | 5 ++--- pillar/web/utils/__init__.py | 14 ++++++++++---- tests/test_web/test_utils.py | 5 +++++ 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/pillar/web/nodes/custom/comments.py b/pillar/web/nodes/custom/comments.py index 40194c6c..338acfaa 100644 --- a/pillar/web/nodes/custom/comments.py +++ b/pillar/web/nodes/custom/comments.py @@ -1,7 +1,6 @@ import logging import warnings -import flask from flask import current_app from flask import request from flask import jsonify @@ -14,7 +13,7 @@ import werkzeug.exceptions as wz_exceptions from pillar.web import subquery from pillar.web.nodes.routes import blueprint from pillar.web.utils import gravatar -from pillar.web.utils import pretty_date +from pillar.web.utils import pretty_date, datetime_now from pillar.web.utils import system_util log = logging.getLogger(__name__) @@ -112,7 +111,7 @@ def format_comment(comment, is_reply=False, is_team=False, replies=None): return dict(_id=comment._id, gravatar=gravatar(comment.user.email, size=32), - time_published=pretty_date(comment._created, detail=True), + time_published=pretty_date(comment._created or datetime_now(), detail=True), rating=comment.properties.rating_positive - comment.properties.rating_negative, author=comment.user.full_name, author_username=comment.user.username, diff --git a/pillar/web/utils/__init__.py b/pillar/web/utils/__init__.py index bf08fb23..4860a041 100644 --- a/pillar/web/utils/__init__.py +++ b/pillar/web/utils/__init__.py @@ -1,3 +1,4 @@ +import datetime import hashlib import urllib import logging @@ -53,19 +54,24 @@ def gravatar(email, size=64): "?" + urllib.urlencode(parameters) -def pretty_date(time=None, detail=False, now=None): +def datetime_now(): + """Returns a datetime.datetime that represents 'now' in UTC.""" + + return datetime.datetime.now(tz=pillarsdk.utils.utc) + + +def pretty_date(time, detail=False, now=None): """Get a datetime object or a int() Epoch timestamp and return a pretty string like 'an hour ago', 'Yesterday', '3 months ago', 'just now', etc """ - import datetime + if time is None: + return None # Normalize the 'time' parameter so it's always a datetime. if type(time) is int: time = datetime.datetime.fromtimestamp(time, tz=pillarsdk.utils.utc) - elif time is None: - time = now now = now or datetime.datetime.now(tz=time.tzinfo) diff = now - time # TODO: flip the sign, so that future = positive and past = negative. diff --git a/tests/test_web/test_utils.py b/tests/test_web/test_utils.py index a770568c..a6a1ab54 100644 --- a/tests/test_web/test_utils.py +++ b/tests/test_web/test_utils.py @@ -42,6 +42,11 @@ class IsValidIdTest(unittest.TestCase): class PrettyDateTest(unittest.TestCase): + def test_none(self): + from pillar.web.utils import pretty_date + + self.assertIsNone(pretty_date(None)) + def test_past(self): from pillar.web.utils import pretty_date