core: add phonenumber template filter

Signed-off-by: Skia <skia@libskia.so>
This commit is contained in:
Skia 2017-10-06 17:28:51 +02:00
parent 8a58055d06
commit a09c634ac6
2 changed files with 15 additions and 0 deletions

View File

@ -23,6 +23,8 @@
# #
# #
import phonenumbers
from django import template from django import template
from django.template.defaultfilters import stringfilter from django.template.defaultfilters import stringfilter
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
@ -38,6 +40,18 @@ register = template.Library()
def markdown(text): def markdown(text):
return mark_safe("<div class=\"markdown\">%s</div>" % md(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() @register.filter()
@stringfilter @stringfilter

View File

@ -139,6 +139,7 @@ TEMPLATES = [
], ],
"filters": { "filters": {
"markdown": "core.templatetags.renderer.markdown", "markdown": "core.templatetags.renderer.markdown",
"phonenumber": "core.templatetags.renderer.phonenumber",
}, },
"globals": { "globals": {
"can_edit_prop": "core.views.can_edit_prop", "can_edit_prop": "core.views.can_edit_prop",