Browse Source

Add a CLI argument to import only a given constraint. Drop the necessary constraint.

merge-requests/10/head
Lucas Verney 5 years ago
parent
commit
ed7e9dfc1a
  1. 4
      doc/0.getting_started.md
  2. 4
      flatisfy/__main__.py
  3. 12
      flatisfy/cmds.py
  4. 15
      flatisfy/config.py
  5. 15
      flatisfy/web/routes/api.py

4
doc/0.getting_started.md

@ -128,10 +128,6 @@ You can think of constraints as "a set of criterias to filter out flats". You @@ -128,10 +128,6 @@ You can think of constraints as "a set of criterias to filter out flats". You
can specify as many constraints as you want, in the configuration file,
provided that you name each of them uniquely.
Note that it is mandatory to keep a constraint called `default`, which will be
the default constraint use for refiltering flats already imported with a
constraint that you deleted after the import.
## Building the web assets

4
flatisfy/__main__.py

@ -55,6 +55,10 @@ def parse_args(argv=None): @@ -55,6 +55,10 @@ def parse_args(argv=None):
"-vv", action="store_true",
help="Debug logging output."
)
parent_parser.add_argument(
"--constraints", type=str,
help="Comma-separated list of constraints to consider."
)
# Subcommands
subparsers = parser.add_subparsers(

12
flatisfy/cmds.py

@ -40,9 +40,15 @@ def filter_flats_list(config, constraint_name, flats_list, fetch_details=True): @@ -40,9 +40,15 @@ def filter_flats_list(config, constraint_name, flats_list, fetch_details=True):
try:
constraint = config["constraints"][constraint_name]
except KeyError:
LOGGER.warning("Missing constraint %s. Using default one.",
constraint_name)
constraint = config["constraints"]["default"]
LOGGER.error(
"Missing constraint %s. Skipping filtering for these posts.",
constraint_name
)
return {
"new": [],
"duplicate": [],
"ignored": []
}
first_pass_result = collections.defaultdict(list)
second_pass_result = collections.defaultdict(list)

15
flatisfy/config.py

@ -97,9 +97,8 @@ def validate_config(config): @@ -97,9 +97,8 @@ def validate_config(config):
# message in the log output.
# pylint: disable=locally-disabled,line-too-long
# Ensure default constraint is here
assert "default" in config["constraints"]
# Ensure constraints are ok
assert len(config["constraints"]) > 0
for constraint in config["constraints"].values():
assert "type" in constraint
assert isinstance(constraint["type"], str)
@ -229,6 +228,18 @@ def load_config(args=None): @@ -229,6 +228,18 @@ def load_config(args=None):
"search_index"
)
# Handle constraints filtering
if args and getattr(args, "constraints", None) is not None:
LOGGER.info(
"Filtering constraints from config according to CLI argument."
)
constraints_filter = args.constraints.split(",")
config_data["constraints"] = {
k: v
for k, v in config_data["constraints"].items()
if k in constraints_filter
}
config_validation = validate_config(config_data)
if config_validation is True:
LOGGER.info("Config has been fully initialized.")

15
flatisfy/web/routes/api.py

@ -56,8 +56,7 @@ def flats_v1(config, db): @@ -56,8 +56,7 @@ def flats_v1(config, db):
postal_code_data = next(
x
for x in postal_codes.get(flat["flatisfy_constraint"],
postal_codes["default"])
for x in postal_codes.get(flat["flatisfy_constraint"], [])
if x.postal_code == flat["flatisfy_postal_code"]
)
flat["flatisfy_postal_code"] = {
@ -108,10 +107,6 @@ def flat_v1(flat_id, config, db): @@ -108,10 +107,6 @@ def flat_v1(flat_id, config, db):
"""
flat = db.query(flat_model.Flat).filter_by(id=flat_id).first()
constraint = config["constraints"].get(flat.flatisfy_constraint,
config["constraints"]["default"])
postal_codes = flatisfy.data.load_data(PostalCode, constraint, config)
if not flat:
return bottle.HTTPError(404, "No flat with id {}.".format(flat_id))
@ -120,6 +115,11 @@ def flat_v1(flat_id, config, db): @@ -120,6 +115,11 @@ def flat_v1(flat_id, config, db):
try:
assert flat["flatisfy_postal_code"]
constraint = config["constraints"].get(flat["flatisfy_constraint"],
None)
assert constraint is not None
postal_codes = flatisfy.data.load_data(PostalCode, constraint, config)
postal_code_data = next(
x
for x in postal_codes
@ -276,8 +276,7 @@ def search_v1(db, config): @@ -276,8 +276,7 @@ def search_v1(db, config):
postal_code_data = next(
x
for x in postal_codes.get(flat["flatisfy_constraint"],
postal_codes["default"])
for x in postal_codes.get(flat["flatisfy_constraint"], [])
if x.postal_code == flat["flatisfy_postal_code"]
)
flat["flatisfy_postal_code"] = {

Loading…
Cancel
Save