Fix searching for IDs, numbers, HP, and Asus

This commit is contained in:
Xavier Bustamante Talavera 2018-10-31 11:40:45 +01:00
parent 414f3f9b73
commit 2d525231c3
5 changed files with 31 additions and 13 deletions

View File

@ -1,7 +1,8 @@
# Devicehub # Devicehub
Devicehub is an IT Asset Management System focused in reusing devices, Devicehub is a distributed IT Asset Management System focused in
created under the project [eReuse.org](https://www.ereuse.org). reusing devices, created under the project
[eReuse.org](https://www.ereuse.org).
Our main objectives are: Our main objectives are:

View File

@ -83,14 +83,29 @@ class DeviceSearch(db.Model):
assert not isinstance(device, Component) assert not isinstance(device, Component)
tokens = [ tokens = [
(str(device.id), search.Weight.A),
(inflection.humanize(device.type), search.Weight.B), (inflection.humanize(device.type), search.Weight.B),
(Device.model, search.Weight.B), (Device.model, search.Weight.B),
(Device.manufacturer, search.Weight.C), (Device.manufacturer, search.Weight.C),
(Device.serial_number, search.Weight.A) (Device.serial_number, search.Weight.A)
] ]
if device.manufacturer:
# todo this has to be done using a dictionary
manufacturer = device.manufacturer.lower()
if 'asus' in manufacturer:
tokens.append(('asus', search.Weight.B))
if 'hewlett' in manufacturer or 'hp' in manufacturer or 'h.p' in manufacturer:
tokens.append(('hp', search.Weight.B))
tokens.append(('h.p', search.Weight.C))
tokens.append(('hewlett', search.Weight.C))
tokens.append(('packard', search.Weight.C))
if isinstance(device, Computer): if isinstance(device, Computer):
# Aggregate the values of all the components of pc
Comp = aliased(Component) Comp = aliased(Component)
tokens.extend(( tokens.extend((
(db.func.string_agg(db.cast(Comp.id, db.TEXT), ' '), search.Weight.D),
(db.func.string_agg(Comp.model, ' '), search.Weight.C), (db.func.string_agg(Comp.model, ' '), search.Weight.C),
(db.func.string_agg(Comp.manufacturer, ' '), search.Weight.D), (db.func.string_agg(Comp.manufacturer, ' '), search.Weight.D),
(db.func.string_agg(Comp.serial_number, ' '), search.Weight.B), (db.func.string_agg(Comp.serial_number, ' '), search.Weight.B),

View File

@ -69,7 +69,7 @@ class Sorting(query.Sort):
class DeviceView(View): class DeviceView(View):
class FindArgs(marshmallow.Schema): class FindArgs(marshmallow.Schema):
search = f.Str() search = f.Raw()
filter = f.Nested(Filters, missing=[]) filter = f.Nested(Filters, missing=[])
sort = f.Nested(Sorting, missing=[]) sort = f.Nested(Sorting, missing=[])
page = f.Integer(validate=v.Range(min=1), missing=1) page = f.Integer(validate=v.Range(min=1), missing=1)

View File

@ -199,25 +199,27 @@ def test_device_query_search(user: UserClient):
assert i['items'][0]['id'] == 1 assert i['items'][0]['id'] == 1
i, _ = user.get(res=Device, query=[('search', 'intel')]) i, _ = user.get(res=Device, query=[('search', 'intel')])
assert len(i['items']) == 1 assert len(i['items']) == 1
i, _ = user.get(res=Device, query=[('search', '1')])
assert len(i['items']) == 1
@pytest.mark.xfail(reason='No dictionary yet that knows asustek = asus')
def test_device_query_search_synonyms_asus(user: UserClient): def test_device_query_search_synonyms_asus(user: UserClient):
user.post(file('real-eee-1001pxd.snapshot.11'), res=Snapshot) user.post(file('real-eee-1001pxd.snapshot.11'), res=Snapshot)
i, _ = user.get(res=Device, query=[('search', 'asustek')]) i, _ = user.get(res=Device, query=[('search', 'asustek')])
assert len(i['items']) == 1 assert 1 == len(i['items'])
i, _ = user.get(res=Device, query=[('search', 'asus')]) i, _ = user.get(res=Device, query=[('search', 'asus')])
assert len(i['items']) == 1 assert 1 == len(i['items'])
@pytest.mark.xfail(reason='No dictionary yet that knows hp = hewlett packard')
def test_device_query_search_synonyms_intel(user: UserClient): def test_device_query_search_synonyms_intel(user: UserClient):
s = file('real-hp.snapshot.11') s = file('real-hp.snapshot.11')
s['device']['model'] = 'foo' # The model had the word 'HP' in it s['device']['model'] = 'foo' # The model had the word 'HP' in it
user.post(s, res=Snapshot) user.post(s, res=Snapshot)
i, _ = user.get(res=Device, query=[('search', 'hewlett packard')]) i, _ = user.get(res=Device, query=[('search', 'hewlett packard')])
assert len(i['items']) == 1 assert 1 == len(i['items'])
i, _ = user.get(res=Device, query=[('search', 'hewlett')]) i, _ = user.get(res=Device, query=[('search', 'hewlett')])
assert len(i['items']) == 1 assert 1 == len(i['items'])
i, _ = user.get(res=Device, query=[('search', 'hp')]) i, _ = user.get(res=Device, query=[('search', 'hp')])
assert len(i['items']) == 1 assert 1 == len(i['items'])
i, _ = user.get(res=Device, query=[('search', 'h.p')])
assert 1 == len(i['items'])

View File

@ -278,20 +278,20 @@ def test_lot_post_add_children_view_ui_tree_normal(user: UserClient):
# Format UiTree # Format UiTree
lots = user.get(res=Lot, query=[('format', 'UiTree')])[0]['items'] lots = user.get(res=Lot, query=[('format', 'UiTree')])[0]['items']
assert len(lots) == 1 assert 1 == len(lots)
assert lots[0]['name'] == 'Parent' assert lots[0]['name'] == 'Parent'
assert len(lots[0]['nodes']) == 1 assert len(lots[0]['nodes']) == 1
assert lots[0]['nodes'][0]['name'] == 'Child' assert lots[0]['nodes'][0]['name'] == 'Child'
# Normal list format # Normal list format
lots = user.get(res=Lot)[0]['items'] lots = user.get(res=Lot)[0]['items']
assert len(lots) == 2 assert 2 == len(lots)
assert lots[0]['name'] == 'Parent' assert lots[0]['name'] == 'Parent'
assert lots[1]['name'] == 'Child' assert lots[1]['name'] == 'Child'
# List format with a filter # List format with a filter
lots = user.get(res=Lot, query=[('search', 'pa')])[0]['items'] lots = user.get(res=Lot, query=[('search', 'pa')])[0]['items']
assert len(lots) == 1 assert 1 == len(lots)
assert lots[0]['name'] == 'Parent' assert lots[0]['name'] == 'Parent'