From 2d525231c34ef6e3903a2280783e654ecd5d948f Mon Sep 17 00:00:00 2001 From: Xavier Bustamante Talavera Date: Wed, 31 Oct 2018 11:40:45 +0100 Subject: [PATCH] Fix searching for IDs, numbers, HP, and Asus --- README.md | 5 +++-- ereuse_devicehub/resources/device/search.py | 15 +++++++++++++++ ereuse_devicehub/resources/device/views.py | 2 +- tests/test_device_find.py | 16 +++++++++------- tests/test_lot.py | 6 +++--- 5 files changed, 31 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index cbea64aa..3e1d53bd 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,8 @@ # Devicehub -Devicehub is an IT Asset Management System focused in reusing devices, -created under the project [eReuse.org](https://www.ereuse.org). +Devicehub is a distributed IT Asset Management System focused in +reusing devices, created under the project +[eReuse.org](https://www.ereuse.org). Our main objectives are: diff --git a/ereuse_devicehub/resources/device/search.py b/ereuse_devicehub/resources/device/search.py index defd7cb7..ff7203a5 100644 --- a/ereuse_devicehub/resources/device/search.py +++ b/ereuse_devicehub/resources/device/search.py @@ -83,14 +83,29 @@ class DeviceSearch(db.Model): assert not isinstance(device, Component) tokens = [ + (str(device.id), search.Weight.A), (inflection.humanize(device.type), search.Weight.B), (Device.model, search.Weight.B), (Device.manufacturer, search.Weight.C), (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): + # Aggregate the values of all the components of pc Comp = aliased(Component) 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.manufacturer, ' '), search.Weight.D), (db.func.string_agg(Comp.serial_number, ' '), search.Weight.B), diff --git a/ereuse_devicehub/resources/device/views.py b/ereuse_devicehub/resources/device/views.py index 783d041e..a0e4731b 100644 --- a/ereuse_devicehub/resources/device/views.py +++ b/ereuse_devicehub/resources/device/views.py @@ -69,7 +69,7 @@ class Sorting(query.Sort): class DeviceView(View): class FindArgs(marshmallow.Schema): - search = f.Str() + search = f.Raw() filter = f.Nested(Filters, missing=[]) sort = f.Nested(Sorting, missing=[]) page = f.Integer(validate=v.Range(min=1), missing=1) diff --git a/tests/test_device_find.py b/tests/test_device_find.py index e5fd3807..0b8fdb6a 100644 --- a/tests/test_device_find.py +++ b/tests/test_device_find.py @@ -199,25 +199,27 @@ def test_device_query_search(user: UserClient): assert i['items'][0]['id'] == 1 i, _ = user.get(res=Device, query=[('search', 'intel')]) 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): user.post(file('real-eee-1001pxd.snapshot.11'), res=Snapshot) 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')]) - 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): s = file('real-hp.snapshot.11') s['device']['model'] = 'foo' # The model had the word 'HP' in it user.post(s, res=Snapshot) 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')]) - assert len(i['items']) == 1 + assert 1 == len(i['items']) 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']) diff --git a/tests/test_lot.py b/tests/test_lot.py index f84345c2..2227d884 100644 --- a/tests/test_lot.py +++ b/tests/test_lot.py @@ -278,20 +278,20 @@ def test_lot_post_add_children_view_ui_tree_normal(user: UserClient): # Format UiTree lots = user.get(res=Lot, query=[('format', 'UiTree')])[0]['items'] - assert len(lots) == 1 + assert 1 == len(lots) assert lots[0]['name'] == 'Parent' assert len(lots[0]['nodes']) == 1 assert lots[0]['nodes'][0]['name'] == 'Child' # Normal list format lots = user.get(res=Lot)[0]['items'] - assert len(lots) == 2 + assert 2 == len(lots) assert lots[0]['name'] == 'Parent' assert lots[1]['name'] == 'Child' # List format with a filter lots = user.get(res=Lot, query=[('search', 'pa')])[0]['items'] - assert len(lots) == 1 + assert 1 == len(lots) assert lots[0]['name'] == 'Parent'