Skip to content
Snippets Groups Projects
Commit dd0502fd authored by Rajmund Hruška's avatar Rajmund Hruška
Browse files

Fix: Convert user input to ip range. (Redmine issue: #7603)

parent 8cc60ea5
No related branches found
No related tags found
No related merge requests found
......@@ -32,7 +32,7 @@ from sqlalchemy import or_
from sqlalchemy import text
from mentat.datatype.sqldb import NetworkModel, GroupModel, ItemChangeLogModel
from mentat.datatype.internal import t_net
from ipranges import from_str, ip_from_str
import hawat.acl
import hawat.menu
......@@ -112,10 +112,16 @@ class ListView(HTMLMixin, SQLAlchemyMixin, ItemListView):
def build_query(query, model, form_args):
# Adjust query based on text search string.
if 'search' in form_args and form_args['search']:
# First of all, assume the user entered address/net/range.
try:
net = t_net(form_args['search'])
query = query.filter(text("network >>= '{}'".format(str(net))))
# Convert user input to ipranges object.
net = from_str(form_args['search'])
# 'a >>= b' means: a contains b or is equal to b
# So search for networks which contain the address/net/range from the user input.
# Also, the user input is converted to range to solve issues with addresses such as 195.113.144.1/24
query = query.filter(text("network >>= '{}-{}'".format(ip_from_str(net.low()), ip_from_str(net.high()))))
except ValueError:
# Try searching by the name of the network or the description.
query = query \
.filter(
or_(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment