comparison libervia/backend/tools/common/template.py @ 4354:4a8bb49b9d0f

template: Add `initials` filter.
author Goffi <goffi@goffi.org>
date Fri, 11 Apr 2025 18:19:17 +0200
parents e22a5d80be02
children
comparison
equal deleted inserted replaced
4353:e22a5d80be02 4354:4a8bb49b9d0f
495 495
496 # we want to have access to SàT constants in templates 496 # we want to have access to SàT constants in templates
497 self.env.globals["C"] = C 497 self.env.globals["C"] = C
498 498
499 # custom filters 499 # custom filters
500 self.env.filters["initials"] = self._initials
500 self.env.filters["bare_jid"] = self._bare_jid 501 self.env.filters["bare_jid"] = self._bare_jid
501 self.env.filters["next_gidx"] = self._next_gidx 502 self.env.filters["next_gidx"] = self._next_gidx
502 self.env.filters["cur_gidx"] = self._cur_gidx 503 self.env.filters["cur_gidx"] = self._cur_gidx
503 self.env.filters["date_fmt"] = self._date_fmt 504 self.env.filters["date_fmt"] = self._date_fmt
504 self.env.filters["timestamp_to_hour"] = self._timestamp_to_hour 505 self.env.filters["timestamp_to_hour"] = self._timestamp_to_hour
797 798
798 def _bare_jid(self, full_jid: str | jid.JID) -> str: 799 def _bare_jid(self, full_jid: str | jid.JID) -> str:
799 """Return the bare JID""" 800 """Return the bare JID"""
800 return str(jid.JID(str(full_jid)).bare) 801 return str(jid.JID(str(full_jid)).bare)
801 802
803 def _initials(self, name: str) -> str:
804 """Return the uppercased initials of a name."""
805 name_parts = name.strip().split()
806 if not name_parts:
807 initials = "??"
808 elif len(name_parts) == 1:
809 initials = name_parts[0][:2]
810 else:
811 initials = name_parts[0][0] + name_parts[1][0]
812 return initials.upper()
813
802 @pass_context 814 @pass_context
803 def _next_gidx(self, ctx, value): 815 def _next_gidx(self, ctx, value):
804 """Use next current global index as suffix""" 816 """Use next current global index as suffix"""
805 next_ = ctx["gidx"].next(value) 817 next_ = ctx["gidx"].next(value)
806 return value if next_ == 0 else "{}_{}".format(value, next_) 818 return value if next_ == 0 else "{}_{}".format(value, next_)