Skip to content

Commit a1f8961

Browse files
authored
Merge pull request #222 from TNRIS/dpp_suggestions_check
Improved dpp_suggestions check in jobs.py Will make minor tweaks after merging
2 parents 9bba0de + edae00a commit a1f8961

File tree

1 file changed

+46
-9
lines changed

1 file changed

+46
-9
lines changed

ckanext/datapusher_plus/jobs.py

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,21 +1215,58 @@ def _push_to_datastore(
12151215
package_id, scheming_yaml_type="dataset"
12161216
)
12171217

1218-
# check if package dpp_suggestions field does not exist
1219-
# and there are "suggestion_formula" keys in the scheming_yaml
1220-
if "dpp_suggestions" not in package:
1221-
# Check for suggestion_formula in dataset_fields
1222-
has_suggestion_formula = any(
1218+
# Check for suggestion_formula in dataset_fields
1219+
has_suggestion_formula = any(
1220+
isinstance(field, dict)
1221+
and any(key.startswith("suggestion_formula") for key in field.keys())
1222+
for field in scheming_yaml["dataset_fields"]
1223+
)
1224+
1225+
if has_suggestion_formula:
1226+
1227+
logger.info(
1228+
'Found suggestion formulae in schema'
1229+
)
1230+
1231+
# Check for "dpp_suggestions" in scheming_yaml
1232+
schema_has_dpp_suggestions = any(
12231233
isinstance(field, dict)
1224-
and any(key.startswith("suggestion_formula") for key in field.keys())
1234+
and field.get("field_name") == "dpp_suggestions"
12251235
for field in scheming_yaml["dataset_fields"]
12261236
)
1227-
1228-
if not has_suggestion_formula:
1237+
if not schema_has_dpp_suggestions:
12291238
logger.error(
1230-
'"dpp_suggestions" field required but not found in package to process Suggestion Formulae. Ensure that your scheming.yaml file contains the "dpp_suggestions" field as a json_object.'
1239+
'"dpp_suggestions" field required but not found in your schema. Ensure that your scheming.yaml file contains the "dpp_suggestions" field as a json_object.'
12311240
)
12321241
return
1242+
else:
1243+
logger.info(
1244+
'Found "dpp_suggestions" field in schema'
1245+
)
1246+
1247+
# add "dpp_suggestions" to package if it does not exist
1248+
if "dpp_suggestions" not in package:
1249+
1250+
logger.warning(
1251+
'Warning: "dpp_suggestions" field required to process Suggestion Formulae is not found in this package . Adding "dpp_suggestions" to package'
1252+
)
1253+
1254+
try:
1255+
package["dpp_suggestions"] = {}
1256+
dsu.patch_package(package)
1257+
logger.error(
1258+
'"dpp_suggestions" field added to package'
1259+
)
1260+
1261+
except Exception as e:
1262+
logger.error(
1263+
'Error adding "dpp_suggestions" field {}'.format( e )
1264+
)
1265+
return
1266+
else:
1267+
logger.info(
1268+
'No suggestion formulae found'
1269+
)
12331270

12341271
logger.trace(f"package: {package}")
12351272

0 commit comments

Comments
 (0)