summaryrefslogtreecommitdiffstats
path: root/activities/templatetags
diff options
context:
space:
mode:
authorAndrew Godwin2022-11-13 16:14:38 -0700
committerAndrew Godwin2022-11-13 16:15:14 -0700
commit34b24a0dcb044cb755e201ad1a83253d0690e78e (patch)
tree51e6842d9e1392fafcd617cf4de0fa7e28397c87 /activities/templatetags
parent5a8b6bb3d022a532562ad5fa6d629cfc48c51b46 (diff)
downloadtakahe-34b24a0dcb044cb755e201ad1a83253d0690e78e.tar.gz
takahe-34b24a0dcb044cb755e201ad1a83253d0690e78e.tar.bz2
takahe-34b24a0dcb044cb755e201ad1a83253d0690e78e.zip
More UI!
Diffstat (limited to 'activities/templatetags')
-rw-r--r--activities/templatetags/__init__.py0
-rw-r--r--activities/templatetags/activity_tags.py33
2 files changed, 33 insertions, 0 deletions
diff --git a/activities/templatetags/__init__.py b/activities/templatetags/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/activities/templatetags/__init__.py
diff --git a/activities/templatetags/activity_tags.py b/activities/templatetags/activity_tags.py
new file mode 100644
index 0000000..cbbae57
--- /dev/null
+++ b/activities/templatetags/activity_tags.py
@@ -0,0 +1,33 @@
+import datetime
+
+from django import template
+from django.utils import timezone
+
+register = template.Library()
+
+
+@register.filter
+def timedeltashort(value: datetime.datetime):
+ """
+ A more compact version of timesince
+ """
+ if not value:
+ return ""
+ # TODO: Handle things in the future properly
+ delta = timezone.now() - value
+ seconds = int(delta.total_seconds())
+ days = delta.days
+ if seconds < 60:
+ text = f"{seconds:0n}s"
+ elif seconds < 60 * 60:
+ minutes = seconds // 60
+ text = f"{minutes:0n}m"
+ elif seconds < 60 * 60 * 24:
+ hours = seconds // (60 * 60)
+ text = f"{hours:0n}h"
+ elif days < 365:
+ text = f"{days:0n}h"
+ else:
+ years = days // 365.25
+ text = f"{years:0n}y"
+ return text