Skip to content

Commit 28e7d6d

Browse files
committed
Readd nice_datetime template tag.
1 parent f4fbfe3 commit 28e7d6d

3 files changed

Lines changed: 37 additions & 2 deletions

File tree

project/newsletter/templatetags/newsletter_utils.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
from datetime import timedelta
2+
13
from django.core.paginator import Paginator
24
from django.template import Library
5+
from django.utils import timezone
6+
7+
from project.newsletter.models import Post
38

49
register = Library()
510

@@ -10,3 +15,19 @@ def is_ellipsis(value):
1015
Determine if the value is an ellipsis
1116
"""
1217
return value == Paginator.ELLIPSIS
18+
19+
20+
@register.inclusion_tag("inclusion_tags/nice_datetime.html")
21+
def nice_datetime(post: Post, is_unread: bool):
22+
"""
23+
Format the datetime in the locale the user prefers with styling.
24+
"""
25+
now = timezone.now()
26+
week_ago = timezone.now() - timedelta(days=7)
27+
timestamp = post.created
28+
is_recent = week_ago <= timestamp <= now
29+
return {
30+
"is_unread": is_unread,
31+
"is_recent": is_recent,
32+
"timestamp": timestamp,
33+
}
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
from django.core.paginator import Paginator
2-
from django.test import SimpleTestCase
2+
from django.test import SimpleTestCase, TestCase
3+
from django.utils import timezone
34

4-
from project.newsletter.templatetags.newsletter_utils import is_ellipsis
5+
from project.newsletter.models import Post
6+
from project.newsletter.templatetags.newsletter_utils import is_ellipsis, nice_datetime
57

68

79
class TestIsEllipsis(SimpleTestCase):
810
def test_is_ellipsis(self):
911
self.assertTrue(is_ellipsis(Paginator.ELLIPSIS))
1012
self.assertFalse(is_ellipsis("..."))
13+
14+
15+
class TestNiceDatetime(TestCase):
16+
def test_nice_datetime(self):
17+
post = Post(created=timezone.now())
18+
actual = nice_datetime(post, is_unread=True)
19+
20+
self.assertEqual(
21+
actual, {"timestamp": post.created, "is_recent": True, "is_unread": True}
22+
)

project/templates/posts/includes/list_item.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
{% load martortags %}
2+
{% load newsletter_utils %}
23

34
<h3 class="ui header">{{ post.title }}</h3>
45

56
<div class="ui grid middle aligned">
67
<div class="left floated six wide column">
8+
{% nice_datetime post=post is_unread=post.is_unread %}
79
</div>
810
<div class="right floated six wide column">
911
<span class="ui small text">

0 commit comments

Comments
 (0)