Skip to content

Commit

Permalink
[#2815] Fix tracking info and isopen being stripped by validate
Browse files Browse the repository at this point in the history
The 'tracking_summary' and 'isopen' fields were being stripped from
package dicts by validation. Only happened when there was a schema in use
that packages dict were being validated against, e.g. when there was an
IDatasetForm plugin with a db_to_form_schema() method.

When dictizing packages, 'tracking_summary' was being added to package
dicts as a sub-dict with two keys 'total' and 'recent', but
ckan/lib/navl/dictization_functions.py:validate() does not allow package
dicts to contain sub-dicts ("Only lists of dicts can be placed against
subschema"), they can contain only single values (e.g. strings, numbers,
bools) or lists of dicts (e.g. list of tag dicts) but not single dicts.
So change the dictization of packages to add two keys
'tracking_summary_total' and 'tracking_summary_recent' instead.

Add 'tracking_summary_total', 'tracking_summary_recent' and 'isopen' to
default_package_schema() so that they do not get stripped from package
dicts during validation.

Update a few tests now that package dicts have these three new keys.
  • Loading branch information
Sean Hammond committed Aug 8, 2012
1 parent f67c571 commit ecf4ec6
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 6 deletions.
3 changes: 2 additions & 1 deletion ckan/lib/dictization/model_dictize.py
Expand Up @@ -206,7 +206,8 @@ def package_dictize(pkg, context):
result_dict["extras"] = extras_list_dictize(result, context)
#tracking
tracking = model.TrackingSummary.get_for_package(pkg.id)
result_dict['tracking_summary'] = tracking
result_dict['tracking_summary_total'] = tracking['total']
result_dict['tracking_summary_recent'] = tracking['recent']
#groups
member_rev = model.member_revision_table
group = model.group_table
Expand Down
7 changes: 7 additions & 0 deletions ckan/lib/plugins.py
Expand Up @@ -262,6 +262,13 @@ def check_data_dict(self, data_dict, schema=None):
'extras_validation', 'save', 'return_to',
'resources', 'type']

# Tracking summary might not exist if tracking not enabled.
surplus_keys_schema.extend(['tracking_summary_total',
'tracking_summary_recent'])

# isopen might not exist.
surplus_keys_schema.extend(['isopen'])

if not schema:
schema = self.form_to_db_schema()
schema_keys = schema.keys()
Expand Down
5 changes: 4 additions & 1 deletion ckan/logic/schema.py
Expand Up @@ -133,7 +133,10 @@ def default_package_schema():
'name': [ignore_missing, unicode],
'title': [ignore_missing, unicode],
'__extras': [ignore],
}
},
'tracking_summary_total': [ignore_missing],
'tracking_summary_recent': [ignore_missing],
'isopen': [ignore_missing],
}
return schema

Expand Down
3 changes: 2 additions & 1 deletion ckan/model/package.py
Expand Up @@ -218,7 +218,8 @@ def as_dict(self, ref_package_by='name', ref_group_by='name'):
#tracking
import ckan.model as model
tracking = model.TrackingSummary.get_for_package(self.id)
_dict['tracking_summary'] = tracking
_dict['tracking_summary_total'] = tracking['total']
_dict['tracking_summary_recent'] = tracking['recent']
return _dict

def add_relationship(self, type_, related_package, comment=u''):
Expand Down
7 changes: 5 additions & 2 deletions ckan/tests/lib/test_dictization.py
Expand Up @@ -113,9 +113,12 @@ def setup_class(cls):
{'name': u'tolstoy', 'display_name': u'tolstoy',
'state': u'active'}],
'title': u'A Novel By Tolstoy',
'tracking_summary': {'total': 0, 'recent': 0},
'tracking_summary_total': 0,
'tracking_summary_recent': 0,
'url': u'http://www.annakarenina.com',
'version': u'0.7a'}
'version': u'0.7a',
'isopen': True,
}


@classmethod
Expand Down
6 changes: 5 additions & 1 deletion ckan/tests/lib/test_dictization_schema.py
Expand Up @@ -99,7 +99,11 @@ def test_1_package_schema(self):
{'name': u'tolstoy'}],
'title': u'A Novel By Tolstoy',
'url': u'http://www.annakarenina.com',
'version': u'0.7a'}, pformat(converted_data)
'version': u'0.7a',
'isopen': True,
'tracking_summary_total': 0,
'tracking_summary_recent': 0,
}, pformat(converted_data)



Expand Down

0 comments on commit ecf4ec6

Please sign in to comment.