From 0d732aa3deb31867d2774762ca2b3d41b9c96a4d Mon Sep 17 00:00:00 2001 From: "Phyks (Lucas Verney)" Date: Fri, 26 Mar 2021 23:36:36 +0100 Subject: [PATCH] Support 'OR' operations for description_should_contain --- doc/0.getting_started.md | 5 ++++- flatisfy/config.py | 9 +++++++-- flatisfy/filters/__init__.py | 9 ++++++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/doc/0.getting_started.md b/doc/0.getting_started.md index fa3f6d7..3958692 100644 --- a/doc/0.getting_started.md +++ b/doc/0.getting_started.md @@ -194,7 +194,10 @@ under the `constraints` key. The available constraints are: * `description_should_contain` lets you specify a list of terms that should be present in the posts descriptions. Typically, if you expect "parking" to be in all the posts Flatisfy fetches for you, you can set - `description_should_contain: ["parking"]`. + `description_should_contain: ["parking"]`. You can also use list of terms + which acts as an "or" operation. For example, if you are looking for a flat + with a parking and with either a balcony or a terrace, you can use + `description_should_contain: ["parking", ["balcony", "terrace"]]` * `description_should_not_contain` lets you specify a list of terms that should never occur in the posts descriptions. Typically, if you wish to avoid "coloc" in the posts Flatisfy fetches for you, you can set diff --git a/flatisfy/config.py b/flatisfy/config.py index ce49ff0..5a0e31c 100644 --- a/flatisfy/config.py +++ b/flatisfy/config.py @@ -38,7 +38,8 @@ DEFAULT_CONFIG = { "rooms": (None, None), # (min, max) "bedrooms": (None, None), # (min, max) "minimum_nb_photos": None, # min number of photos - "description_should_contain": [], # list of terms + "description_should_contain": [], # list of terms (str) or list + # (acting as an or) "description_should_not_contain": [ "vendu", "Vendu", @@ -187,7 +188,11 @@ def validate_config(config, check_with_data): assert isinstance(constraint["description_should_contain"], list) if constraint["description_should_contain"]: for term in constraint["description_should_contain"]: - assert isinstance(term, str) + try: + assert isinstance(term, str) + except AssertionError: + assert isinstance(term, list) + assert all(isinstance(x, str) for x in term) assert "description_should_not_contain" in constraint assert isinstance(constraint["description_should_not_contain"], list) diff --git a/flatisfy/filters/__init__.py b/flatisfy/filters/__init__.py index b92e893..73ec5a9 100644 --- a/flatisfy/filters/__init__.py +++ b/flatisfy/filters/__init__.py @@ -117,13 +117,20 @@ def refine_with_details_criteria(flats_list, constraint): is_ok[i] = False for term in constraint["description_should_contain"]: - if term.lower() not in flat["text"].lower(): + if isinstance(term, str) and term.lower() not in flat["text"].lower(): LOGGER.info( ("Description for flat %s does not contain required term '%s'."), flat["id"], term, ) is_ok[i] = False + elif isinstance(term, list) and all(x.lower() not in flat["text"].lower() for x in term): + LOGGER.info( + ("Description for flat %s does not contain any of required terms '%s'."), + flat["id"], + term, + ) + is_ok[i] = False for term in constraint["description_should_not_contain"]: if term.lower() in flat["text"].lower(): LOGGER.info(