Support 'OR' operations for description_should_contain

This commit is contained in:
Lucas Verney 2021-03-26 23:36:36 +01:00
parent 1a95495c30
commit 0d732aa3de
3 changed files with 19 additions and 4 deletions

View File

@ -194,7 +194,10 @@ under the `constraints` key. The available constraints are:
* `description_should_contain` lets you specify a list of terms that should * `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 present in the posts descriptions. Typically, if you expect "parking" to
be in all the posts Flatisfy fetches for you, you can set 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 * `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 never occur in the posts descriptions. Typically, if you wish to avoid
"coloc" in the posts Flatisfy fetches for you, you can set "coloc" in the posts Flatisfy fetches for you, you can set

View File

@ -38,7 +38,8 @@ DEFAULT_CONFIG = {
"rooms": (None, None), # (min, max) "rooms": (None, None), # (min, max)
"bedrooms": (None, None), # (min, max) "bedrooms": (None, None), # (min, max)
"minimum_nb_photos": None, # min number of photos "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": [ "description_should_not_contain": [
"vendu", "vendu",
"Vendu", "Vendu",
@ -187,7 +188,11 @@ def validate_config(config, check_with_data):
assert isinstance(constraint["description_should_contain"], list) assert isinstance(constraint["description_should_contain"], list)
if constraint["description_should_contain"]: if constraint["description_should_contain"]:
for term in 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 "description_should_not_contain" in constraint
assert isinstance(constraint["description_should_not_contain"], list) assert isinstance(constraint["description_should_not_contain"], list)

View File

@ -117,13 +117,20 @@ def refine_with_details_criteria(flats_list, constraint):
is_ok[i] = False is_ok[i] = False
for term in constraint["description_should_contain"]: 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( LOGGER.info(
("Description for flat %s does not contain required term '%s'."), ("Description for flat %s does not contain required term '%s'."),
flat["id"], flat["id"],
term, term,
) )
is_ok[i] = False 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"]: for term in constraint["description_should_not_contain"]:
if term.lower() in flat["text"].lower(): if term.lower() in flat["text"].lower():
LOGGER.info( LOGGER.info(