2
0

templates: add ip_in_networks test

This commit is contained in:
Stefan Bühler 2023-08-02 11:23:20 +02:00
parent a832d0ad48
commit 77bb2b7ceb
2 changed files with 17 additions and 0 deletions

View File

@ -7,4 +7,5 @@ app = MyQuartApp(__name__)
__import__('capport.api.setup') __import__('capport.api.setup')
__import__('capport.api.proxy_fix') __import__('capport.api.proxy_fix')
__import__('capport.api.lang') __import__('capport.api.lang')
__import__('capport.api.template_filters')
__import__('capport.api.views') __import__('capport.api.views')

View File

@ -0,0 +1,16 @@
from __future__ import annotations
import ipaddress
import typing
from .app import app
@app.add_template_test
def ip_in_networks(ip_s: str, networks: typing.Iterable[str]) -> bool:
ip = ipaddress.ip_address(ip_s)
for network in networks:
net = ipaddress.ip_network(network)
if ip in net:
return True
return False