From 53aef92bf89f2d655a125d94a0e24203e76af1d6 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Sun, 1 Jan 2006 18:37:33 +0000 Subject: Fixed #1145 -- Added unit tests for default template filters and fixed two bugs in filters. Thanks, Luke Plant git-svn-id: http://code.djangoproject.com/svn/django/trunk@1811 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/template/defaultfilters.py | 12 ++++++++++-- django/utils/timesince.py | 6 ++++-- 2 files changed, 14 insertions(+), 4 deletions(-) (limited to 'django') diff --git a/django/core/template/defaultfilters.py b/django/core/template/defaultfilters.py index de6da35cbb..f273fa0726 100644 --- a/django/core/template/defaultfilters.py +++ b/django/core/template/defaultfilters.py @@ -117,7 +117,8 @@ def urlize(value): def urlizetrunc(value, limit): """ - Converts URLs into clickable links, truncating URLs to the given character limit + Converts URLs into clickable links, truncating URLs to the given character limit, + and adding 'rel=nofollow' attribute to discourage spamming. Argument: Length to truncate URLs to. """ @@ -254,7 +255,14 @@ def slice_(value, arg): for an introduction. """ try: - return value[slice(*[x and int(x) or None for x in arg.split(':')])] + bits = [] + for x in arg.split(':'): + if len(x) == 0: + bits.append(None) + else: + bits.append(int(x)) + return value[slice(*bits)] + except (ValueError, TypeError): return value # Fail silently. diff --git a/django/utils/timesince.py b/django/utils/timesince.py index b9edb12554..0d1ab470e5 100644 --- a/django/utils/timesince.py +++ b/django/utils/timesince.py @@ -24,14 +24,16 @@ def timesince(d, now=None): else: tz = None now = datetime.datetime(t[0], t[1], t[2], t[3], t[4], t[5], tzinfo=tz) - delta = now - d + + # ignore microsecond part of 'd' since we removed it from 'now' + delta = now - (d - datetime.timedelta(0, 0, d.microsecond)) since = delta.days * 24 * 60 * 60 + delta.seconds for i, (seconds, name) in enumerate(chunks): count = since / seconds if count != 0: break if count < 0: - return '%d milliseconds' % math.floor(delta.microseconds / 1000) + return '%d milliseconds' % math.floor((now - d).microseconds / 1000) s = '%d %s' % (count, name(count)) if i + 1 < len(chunks): # Now get the second item -- cgit v1.3