Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Conditions logic (optional) #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions ckanext/harvest_basket/harvesters/ckan_harvester.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ def import_stage(self, harvest_object):

self._transmute_content(package_dict)

self.handle_conditions(
package_dict,
self.config.get("conditions", []),
harvest_object.harvest_source_id)

harvest_object.content = json.dumps(package_dict)
super().import_stage(harvest_object)

Expand Down Expand Up @@ -73,3 +78,56 @@ def transmute_data(self, data, schema):
},
{"data": data, "schema": schema},
)

def handle_conditions(self, data, conditions, source_id):
'''
Specify conditions for fields, can be multiple conditions
"conditions": [
{"field": "FIELD_NAME",
"source": "harvester_config", If the field should be taken from Harvester Dataset
Otherwise try to take the value from the Data Dict
"case": "FIELD_VALUE_EQUAL",
"then": "FIELD_TO_MODIFY",
"value": "FIELD_TO_MODIFY_VALUE",
"otherwise": "FIELD_TO_DEFAULT" Default value if check not passed. Optional
},
EXAMPLE
{"field": "owner_org",
"source": "harvester_config",
"case": "77f83b6a-8541-4ae8-b439-011ed75b1564",
"then": "groups",
"value": [{"name":"society"}],
"otherwise": [{"id":"transport"}]
}
]
'''
source_dataset = tk.get_action('package_show')(
{
"model": model,
"session": model.Session,
"user": self._get_user_name(),
}, {'id': source_id}
)

for condition in conditions:
try:
field = condition['field']
source = condition.get('source')
case = condition['case']
then = condition['then']
value = condition['value']
otherwise = condition.get('otherwise')

if source and source == 'harvester_config':
val = source_dataset[field]
else:
val = data[field]

if otherwise:
data[then] = value if val == case else otherwise
elif val and val == case:
data[then] = value
except KeyError as e:
err_msg: str = f"Missing key: {e}"
log.error(err_msg)
raise AttributeError(*e.args)
Loading