Commit a09c634a authored by Skia's avatar Skia

core: add phonenumber template filter

Signed-off-by: Skia's avatarSkia <skia@libskia.so>
parent 8a58055d
......@@ -23,6 +23,8 @@
#
#
import phonenumbers
from django import template
from django.template.defaultfilters import stringfilter
from django.utils.safestring import mark_safe
......@@ -38,6 +40,18 @@ register = template.Library()
def markdown(text):
return mark_safe("<div class=\"markdown\">%s</div>" % md(text))
@register.filter(name='phonenumber')
def phonenumber(value, country='FR',
format=phonenumbers.PhoneNumberFormat.NATIONAL):
"""
This filter is kindly borrowed from https://github.com/foundertherapy/django-phonenumber-filter
"""
value = str(value)
try:
parsed = phonenumbers.parse(value, country)
return phonenumbers.format_number(parsed, format)
except phonenumbers.NumberParseException as e:
return value
@register.filter()
@stringfilter
......
......@@ -139,6 +139,7 @@ TEMPLATES = [
],
"filters": {
"markdown": "core.templatetags.renderer.markdown",
"phonenumber": "core.templatetags.renderer.phonenumber",
},
"globals": {
"can_edit_prop": "core.views.can_edit_prop",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment