summaryrefslogtreecommitdiff
path: root/coip/extensions
diff options
context:
space:
mode:
authorLeif Johansson <leifj@sunet.se>2010-08-04 14:12:44 +0200
committerLeif Johansson <leifj@sunet.se>2010-08-04 14:12:44 +0200
commit845802ed460a53e6601aee86c4b49284d91b73fd (patch)
tree50d565a174388a3dd5a189ce62364f0603613740 /coip/extensions
parentb28be159c39ffe849b3dafc37dbfed865312d9ae (diff)
a couple of template filters
Diffstat (limited to 'coip/extensions')
-rw-r--r--coip/extensions/__init__.py0
-rw-r--r--coip/extensions/templatetags/__init__.py0
-rw-r--r--coip/extensions/templatetags/datehumanize.py35
-rw-r--r--coip/extensions/templatetags/userdisplay.py31
4 files changed, 66 insertions, 0 deletions
diff --git a/coip/extensions/__init__.py b/coip/extensions/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/coip/extensions/__init__.py
diff --git a/coip/extensions/templatetags/__init__.py b/coip/extensions/templatetags/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/coip/extensions/templatetags/__init__.py
diff --git a/coip/extensions/templatetags/datehumanize.py b/coip/extensions/templatetags/datehumanize.py
new file mode 100644
index 0000000..9570612
--- /dev/null
+++ b/coip/extensions/templatetags/datehumanize.py
@@ -0,0 +1,35 @@
+from django import template
+from django.template import defaultfilters
+
+register = template.Library()
+
+MOMENT = 120 # duration in seconds within which the time difference
+ # will be rendered as 'a moment ago'
+
+def datehumanize(value):
+ """
+ Finds the difference between the datetime value given and now()
+ and returns appropriate humanize form
+ """
+
+ from datetime import datetime
+
+ if isinstance(value, datetime):
+ delta = datetime.now() - value
+ if delta.days > 6:
+ return value.strftime("on %b %d") # May 15
+ if delta.days > 1:
+ return value.strftime("on %A") # Wednesday
+ elif delta.days == 1:
+ return 'yesterday' # yesterday
+ elif delta.seconds > 3600:
+ return str(delta.seconds / 3600 ) + ' hours ago' # 3 hours ago
+ elif delta.seconds > MOMENT:
+ return str(delta.seconds/60) + ' minutes ago' # 29 minutes ago
+ else:
+ return 'a moment ago' # a moment ago
+ return defaultfilters.date(value)
+ else:
+ return str(value)
+datehumanize.is_safe = True
+register.filter(datehumanize) \ No newline at end of file
diff --git a/coip/extensions/templatetags/userdisplay.py b/coip/extensions/templatetags/userdisplay.py
new file mode 100644
index 0000000..acf3e41
--- /dev/null
+++ b/coip/extensions/templatetags/userdisplay.py
@@ -0,0 +1,31 @@
+from django import template
+from django.template import defaultfilters
+from coip.apps.userprofile.models import last_used_profile
+from pprint import pprint
+
+register = template.Library()
+
+MOMENT = 120 # duration in seconds within which the time difference
+ # will be rendered as 'a moment ago'
+
+def userdisplay(user):
+ try:
+ p = last_used_profile(user)
+ return p.display_name
+ except Exception,e:
+ pprint(e)
+ return user.username
+
+userdisplay.is_safe = True
+register.filter(userdisplay)
+
+def lastidentifier(user):
+ #try:
+ p = last_used_profile(user)
+ return p.identifier
+ #except Exception,e:
+ # pprint(e)
+ # return user.username
+
+lastidentifier.is_safe = True
+register.filter(lastidentifier) \ No newline at end of file