diff --git a/ckan/lib/dictization/model_save.py b/ckan/lib/dictization/model_save.py index 454159f41df..0b6b6e9bcbf 100644 --- a/ckan/lib/dictization/model_save.py +++ b/ckan/lib/dictization/model_save.py @@ -96,7 +96,7 @@ def package_resource_list_save(res_dicts, package, context): resource_list.append(resource) -def package_extras_save(extra_dicts, obj, context): +def package_extras_save(extra_dicts, pkg, context): allow_partial_update = context.get("allow_partial_update", False) if extra_dicts is None and allow_partial_update: return @@ -104,40 +104,14 @@ def package_extras_save(extra_dicts, obj, context): model = context["model"] session = context["session"] - extras_list = obj.extras_list - old_extras = dict((extra.key, extra) for extra in extras_list) - - new_extras = {} + obj_dict = {} for extra_dict in extra_dicts or []: - if extra_dict.get("deleted"): - continue + package_extra = d.table_dict_save(extra_dict, model.PackageExtra, context) + session.add(package_extra) + obj_dict[extra_dict["key"]] = package_extra + + pkg._extras = obj_dict - if extra_dict['value'] is None: - pass - else: - new_extras[extra_dict["key"]] = extra_dict["value"] - #new - for key in set(new_extras.keys()) - set(old_extras.keys()): - state = 'active' - extra = model.PackageExtra(state=state, key=key, value=new_extras[key]) - session.add(extra) - extras_list.append(extra) - #changed - for key in set(new_extras.keys()) & set(old_extras.keys()): - extra = old_extras[key] - if new_extras[key] == extra.value and extra.state != 'deleted': - continue - state = 'active' - extra.value = new_extras[key] - extra.state = state - session.add(extra) - #deleted - for key in set(old_extras.keys()) - set(new_extras.keys()): - extra = old_extras[key] - if extra.state == 'deleted': - continue - state = 'deleted' - extra.state = state def package_tag_list_save(tag_dicts, package, context): allow_partial_update = context.get("allow_partial_update", False) diff --git a/ckan/migration/versions/088_delete_extras_which_are_deleted_state.py b/ckan/migration/versions/088_delete_extras_which_are_deleted_state.py new file mode 100644 index 00000000000..131fbcff2f4 --- /dev/null +++ b/ckan/migration/versions/088_delete_extras_which_are_deleted_state.py @@ -0,0 +1,9 @@ +# encoding: utf-8 + + +def upgrade(migrate_engine): + migrate_engine.execute( + ''' + DELETE FROM "package_extra" WHERE 'state'='deleted'; + ''' + ) diff --git a/ckan/model/package_extra.py b/ckan/model/package_extra.py index 9d717779b33..957e96ce908 100644 --- a/ckan/model/package_extra.py +++ b/ckan/model/package_extra.py @@ -4,6 +4,7 @@ import vdm.sqlalchemy import vdm.sqlalchemy.stateful from sqlalchemy import orm, types, Column, Table, ForeignKey +from sqlalchemy.ext.associationproxy import association_proxy import meta import core @@ -58,11 +59,6 @@ def activity_stream_detail(self, activity_id, activity_type): cascade='all, delete, delete-orphan', ), ), - 'package_no_state': orm.relation(_package.Package, - backref=orm.backref('extras_list', - cascade='all, delete, delete-orphan', - ), - ) }, order_by=[package_extra_table.c.package_id, package_extra_table.c.key], extension=[vdm.sqlalchemy.Revisioner(extra_revision_table), @@ -79,8 +75,5 @@ def activity_stream_detail(self, activity_id, activity_type): def _create_extra(key, value): return PackageExtra(key=text_type(key), value=value) -_extras_active = vdm.sqlalchemy.stateful.DeferredProperty('_extras', - vdm.sqlalchemy.stateful.StatefulDict, base_modifier=lambda x: x.get_as_of()) -setattr(_package.Package, 'extras_active', _extras_active) -_package.Package.extras = vdm.sqlalchemy.stateful.OurAssociationProxy('extras_active', 'value', - creator=_create_extra) +_package.Package.extras = association_proxy( + '_extras', 'value', creator=_create_extra) diff --git a/ckan/model/resource.py b/ckan/model/resource.py index 52bdaaf31f0..43d99dbbd39 100644 --- a/ckan/model/resource.py +++ b/ckan/model/resource.py @@ -8,7 +8,6 @@ from sqlalchemy import orm from ckan.common import config import vdm.sqlalchemy -import vdm.sqlalchemy.stateful from sqlalchemy import types, func, Column, Table, ForeignKey, and_ import meta diff --git a/ckan/tests/legacy/models/test_extras.py b/ckan/tests/legacy/models/test_extras.py index d530af28cd8..a9801b0f2e7 100644 --- a/ckan/tests/legacy/models/test_extras.py +++ b/ckan/tests/legacy/models/test_extras.py @@ -5,11 +5,11 @@ import ckan.model as model class TestExtras: - @classmethod + @classmethod def setup_class(self): CreateTestData.create() - @classmethod + @classmethod def teardown_class(self): model.repo.rebuild_db() @@ -20,7 +20,7 @@ def test_1(self): rev = model.repo.new_revision() pkg._extras[u'country'] = model.PackageExtra(key=u'country', value='us') - pkg.extras_active[u'xxx'] = model.PackageExtra(key=u'xxx', value='yyy') + pkg.extras[u'xxx'] = u'yyy' pkg.extras[u'format'] = u'rdf' model.repo.commit_and_remove() @@ -28,37 +28,19 @@ def test_1(self): rev1 = model.repo.youngest_revision().id samepkg = model.Package.by_name(u'warandpeace') assert len(samepkg._extras) == 3, samepkg._extras - assert samepkg.extras_active[u'country'].value == 'us', samepkg.extras_active assert samepkg.extras[u'country'] == 'us' assert samepkg.extras[u'format'] == 'rdf' model.Session.remove() - # now delete and extras + # now delete an extra samepkg = model.Package.by_name(u'warandpeace') model.repo.new_revision() del samepkg.extras[u'country'] model.repo.commit_and_remove() samepkg = model.Package.by_name(u'warandpeace') - assert len(samepkg._extras) == 3 + assert len(samepkg._extras) == 2 assert len(samepkg.extras) == 2 extra = model.Session.query(model.PackageExtra).filter_by(key=u'country').first() - assert extra and extra.state == model.State.DELETED, extra + assert not extra, extra model.Session.remove() - - samepkg = model.Package.by_name(u'warandpeace') - samepkg.get_as_of(model.Session.query(model.Revision).get(rev1)) - assert len(samepkg.extras) == 3, len(samepkg.extras) - model.Session.remove() - - # now restore it ... - model.repo.new_revision() - samepkg = model.Package.by_name(u'warandpeace') - samepkg.extras[u'country'] = 'uk' - model.repo.commit_and_remove() - - samepkg = model.Package.by_name(u'warandpeace') - assert len(samepkg.extras) == 3 - assert len(samepkg._extras) == 3 - assert samepkg.extras[u'country'] == 'uk' -