From df9b00c5a79fa6d490e336287acc0da7ab9918da Mon Sep 17 00:00:00 2001 From: Vidya Ramakrishnan Date: Thu, 3 Mar 2022 08:33:59 +0530 Subject: [PATCH] Add filter to return an avatar type based on initials --- baseframe/filters.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/baseframe/filters.py b/baseframe/filters.py index 7a2d2a96..e38802a1 100644 --- a/baseframe/filters.py +++ b/baseframe/filters.py @@ -68,6 +68,17 @@ def initials(text: str) -> str: return '' +@baseframe.app_template_filter('avatar_type') +def avatar_type(text: str, types: int = 6) -> str: + """Return an int from 1 to types based on initials from the given string""" + initial = initials(text) + parts = initial.split() + stringTotal = ord(parts[0][0]) + if len(parts) > 1: + stringTotal += ord(parts[0][1]) + return stringTotal % types or types + + @baseframe.app_template_filter('usessl') def usessl(url: str) -> str: """Convert a URL to https:// if SSL is enabled in site config."""