publik: add housenumber number/btq filters (#88884)
gitea/publik-django-templatetags/pipeline/head This commit looks good Details

This commit is contained in:
Thomas NOËL 2024-03-29 17:51:00 +01:00 committed by Thomas NOËL
parent e68e6e7ff9
commit 83f99fafee
2 changed files with 49 additions and 0 deletions

View File

@ -17,6 +17,7 @@
import collections
import datetime
import math
import re
import urllib.parse
from decimal import Decimal
from decimal import DivisionByZero as DecimalDivisionByZero
@ -260,3 +261,21 @@ def with_auth(value, arg):
parsed_url = urllib.parse.urlparse(value)
new_netloc = '%s@%s' % (arg, parsed_url.netloc.rsplit('@', 1)[-1])
return urllib.parse.urlunparse(parsed_url._replace(netloc=new_netloc))
@register.filter
def housenumber_number(housenumber):
match = re.match(r'^\s*([0-9]+)(.*)$', force_str(housenumber))
if not match:
return ''
number, btq = match.groups()
return number
@register.filter
def housenumber_btq(housenumber):
match = re.match(r'^\s*([0-9]+)(.*)$', force_str(housenumber))
if not match:
return ''
number, btq = match.groups()
return btq.strip()

View File

@ -450,3 +450,33 @@ def test_with_auth():
Template('{{ service_url|with_auth:"username:password" }}').render(context)
== 'https://username:password@www.example.net/api/whatever?x=y'
)
def test_housenumber():
context = Context({'value': '42bis'})
assert Template('{{ value|housenumber_number }}').render(context) == '42'
assert Template('{{ value|housenumber_btq }}').render(context) == 'bis'
context = Context({'value': ' 42 bis '})
assert Template('{{ value|housenumber_number }}').render(context) == '42'
assert Template('{{ value|housenumber_btq }}').render(context) == 'bis'
context = Context({'value': '42 t 3 '})
assert Template('{{ value|housenumber_number }}').render(context) == '42'
assert Template('{{ value|housenumber_btq }}').render(context) == 't 3'
context = Context({'value': ' 42 '})
assert Template('{{ value|housenumber_number }}').render(context) == '42'
assert Template('{{ value|housenumber_btq }}').render(context) == ''
context = Context({'value': ' 42 34 bis '})
assert Template('{{ value|housenumber_number }}').render(context) == '42'
assert Template('{{ value|housenumber_btq }}').render(context) == '34 bis'
context = Context({'value': ' bis '})
assert Template('{{ value|housenumber_number }}').render(context) == ''
assert Template('{{ value|housenumber_btq }}').render(context) == ''
context = Context({'value': 42})
assert Template('{{ value|housenumber_number }}').render(context) == '42'
assert Template('{{ value|housenumber_btq }}').render(context) == ''