Skip to content

Commit

Permalink
Add config option to control id to name conversion
Browse files Browse the repository at this point in the history
With reference to the previous commit 334734b and issue #32, unconditionally converting ids to names in output dicts broke several unit tests. This commit fixes this, and resolves the two problems outlined in the issue.
  • Loading branch information
mark-saeon committed Aug 2, 2018
1 parent 334734b commit 2868d40
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions ckanext/metadata/logic/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import jsonschema

import ckan.plugins.toolkit as tk
from ckan.common import _
from ckan.common import _, config
import ckan.lib.navl.dictization_functions as df
import ckanext.metadata.model as ckanext_model
from ckanext.metadata.logic.json_validator import JSONValidator
Expand Down Expand Up @@ -213,14 +213,19 @@ def callable_(key, data, errors, context):
def convert_id_to_name(model_name):
"""
Converts an object's id to its name.
Behaviour depends on the value of the config option 'ckan.metadata.convert_nested_ids_to_names'.
If True, the object id is converted to name; if False (the default) the id is left unaltered.
"""
convert_nested_ids_to_names = tk.asbool(config.get('ckan.metadata.convert_nested_ids_to_names', False))
model_class = model_map[model_name]['class']

def callable_(key, data, errors, context):
id_ = data.get(key)
obj = model_class.get(id_)
if obj:
data[key] = obj.name
if convert_nested_ids_to_names:
id_ = data.get(key)
obj = model_class.get(id_)
if obj:
data[key] = obj.name

return callable_

Expand Down

0 comments on commit 2868d40

Please sign in to comment.