pretty_date(None) now returns None

This commit is contained in:
Sybren A. Stüvel 2016-11-08 12:56:19 +01:00
parent fb6e326a14
commit 65b554986c
3 changed files with 17 additions and 7 deletions

View File

@ -1,7 +1,6 @@
import logging import logging
import warnings import warnings
import flask
from flask import current_app from flask import current_app
from flask import request from flask import request
from flask import jsonify from flask import jsonify
@ -14,7 +13,7 @@ import werkzeug.exceptions as wz_exceptions
from pillar.web import subquery from pillar.web import subquery
from pillar.web.nodes.routes import blueprint from pillar.web.nodes.routes import blueprint
from pillar.web.utils import gravatar 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 from pillar.web.utils import system_util
log = logging.getLogger(__name__) 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, return dict(_id=comment._id,
gravatar=gravatar(comment.user.email, size=32), 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, rating=comment.properties.rating_positive - comment.properties.rating_negative,
author=comment.user.full_name, author=comment.user.full_name,
author_username=comment.user.username, author_username=comment.user.username,

View File

@ -1,3 +1,4 @@
import datetime
import hashlib import hashlib
import urllib import urllib
import logging import logging
@ -53,19 +54,24 @@ def gravatar(email, size=64):
"?" + urllib.urlencode(parameters) "?" + 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 """Get a datetime object or a int() Epoch timestamp and return a
pretty string like 'an hour ago', 'Yesterday', '3 months ago', pretty string like 'an hour ago', 'Yesterday', '3 months ago',
'just now', etc 'just now', etc
""" """
import datetime if time is None:
return None
# Normalize the 'time' parameter so it's always a datetime. # Normalize the 'time' parameter so it's always a datetime.
if type(time) is int: if type(time) is int:
time = datetime.datetime.fromtimestamp(time, tz=pillarsdk.utils.utc) time = datetime.datetime.fromtimestamp(time, tz=pillarsdk.utils.utc)
elif time is None:
time = now
now = now or datetime.datetime.now(tz=time.tzinfo) now = now or datetime.datetime.now(tz=time.tzinfo)
diff = now - time # TODO: flip the sign, so that future = positive and past = negative. diff = now - time # TODO: flip the sign, so that future = positive and past = negative.

View File

@ -42,6 +42,11 @@ class IsValidIdTest(unittest.TestCase):
class PrettyDateTest(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): def test_past(self):
from pillar.web.utils import pretty_date from pillar.web.utils import pretty_date