Skip to content

Commit

Permalink
[master][#1786][lib/dictization]: Fix relationships getting lost when…
Browse files Browse the repository at this point in the history
… editing the package.
  • Loading branch information
David Read committed Feb 7, 2012
1 parent ed0d130 commit fe6829e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
12 changes: 8 additions & 4 deletions ckan/lib/dictization/model_save.py
Expand Up @@ -281,10 +281,14 @@ def package_dict_save(pkg_dict, context):
package_tag_list_save(pkg_dict.get("tags", []), pkg, context)
package_membership_list_save(pkg_dict.get("groups", []), pkg, context)

subjects = pkg_dict.get('relationships_as_subject', [])
relationship_list_save(subjects, pkg, 'relationships_as_subject', context)
objects = pkg_dict.get('relationships_as_object', [])
relationship_list_save(subjects, pkg, 'relationships_as_object', context)
# relationships are not considered 'part' of the package, so only
# process this if the key is provided
if 'relationships_as_subject' in pkg_dict:
subjects = pkg_dict.get('relationships_as_subject', [])
relationship_list_save(subjects, pkg, 'relationships_as_subject', context)
if 'relationships_as_object' in pkg_dict:
objects = pkg_dict.get('relationships_as_object', [])
relationship_list_save(objects, pkg, 'relationships_as_object', context)

extras = package_extras_save(pkg_dict.get("extras", []), pkg, context)

Expand Down
27 changes: 27 additions & 0 deletions ckan/tests/functional/test_package.py
Expand Up @@ -1016,6 +1016,33 @@ def test_edit_indexerror(self):
plugins.unload('synchronous_search')
SolrSettings.init(solr_url)

def test_edit_pkg_with_relationships(self):
# 1786
try:
# add a relationship to a package
pkg = model.Package.by_name(self.editpkg_name)
anna = model.Package.by_name(u'annakarenina')
model.repo.new_revision()
pkg.add_relationship(u'depends_on', anna)
model.repo.commit_and_remove()

# check relationship before the test
rels = model.Package.by_name(self.editpkg_name).get_relationships()
assert_equal(str(rels), '[<*PackageRelationship editpkgtest depends_on annakarenina>]')

# edit the package
self.offset = url_for(controller='package', action='edit', id=self.editpkg_name)
self.res = self.app.get(self.offset)
fv = self.res.forms['dataset-edit']
fv['title'] = u'New Title'
res = fv.submit('save')

# check relationship still exists
rels = model.Package.by_name(self.editpkg_name).get_relationships()
assert_equal(str(rels), '[<*PackageRelationship editpkgtest depends_on annakarenina>]')

finally:
self._reset_data()

class TestNew(TestPackageForm):
pkg_names = []
Expand Down
2 changes: 1 addition & 1 deletion doc/apiv3.rst
Expand Up @@ -150,7 +150,7 @@ license_id "cc-by" ID of the licens
extras []
tags ["government-spending"] List of tags associated with this dataset.
groups ["spending", "country-uk"] List of groups this dataset is a member of.
relationships_as_subject [] List of relationships (edit this only using relationship specific command). The 'type' of the relationship is described in terms of this package being the subject and the related package being the object.
relationships_as_subject [] List of relationships. The 'type' of the relationship is described in terms of this package being the subject and the related package being the object.
state active May be ``deleted`` or other custom states like ``pending``.
revision_id "f645243a-7334-44e2-b87c-64231700a9a6" (Read-only) ID of the last revision for the core package object was (doesn't include tags, groups, extra fields, relationships).
revision_timestamp "2010-12-21T15:26:17.345502" (Read-only) Time and date when the last revision for the core package object was (doesn't include tags, groups, extra fields, relationships). ISO format. UTC timezone assumed.
Expand Down

0 comments on commit fe6829e

Please sign in to comment.