mirror of
https://github.com/ae-utbm/sith.git
synced 2024-11-25 02:24:26 +00:00
core: add phonenumber template filter
Signed-off-by: Skia <skia@libskia.so>
This commit is contained in:
parent
8a58055d06
commit
a09c634ac6
@ -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",
|
||||
|
Loading…
Reference in New Issue
Block a user