[ckan-changes] commit/ckan: 4 new changesets

Bitbucket commits-noreply at bitbucket.org
Mon Jun 6 14:51:03 UTC 2011


4 new changesets in ckan:

http://bitbucket.org/okfn/ckan/changeset/76024cdd1974/
changeset:   76024cdd1974
branches:    
user:        kindly
date:        2011-05-26 02:38:44
summary:     [model] add migrate for moderated edits
affected #:  1 file (14.6 KB)

--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ckan/migration/versions/039_add_expired_id_and_dates.py	Thu May 26 01:38:44 2011 +0100
@@ -0,0 +1,196 @@
+from migrate import *
+import uuid
+import datetime
+
+def upgrade(migrate_engine):
+
+    id = uuid.uuid4()
+
+    make_missing_revisions = '''
+
+-- make sure all tables have an entry in the revision_table
+
+insert into revision values ('%(id)s' , '%(timestamp)s', 'admin', 'Admin: make sure every object has a row in a revision table', 'active');
+
+insert into package_tag_revision (id,package_id,tag_id,revision_id,state,continuity_id) select id,package_id,tag_id, '%(id)s' ,state, id from package_tag where package_tag.id not in (select id from package_tag_revision);
+
+insert into resource_revision (id,resource_group_id,url,format,description,position,revision_id,hash,state,extras,continuity_id) select id,resource_group_id,url,format,description,position, '%(id)s' ,hash,state,extras, id from resource where resource.id not in (select id from resource_revision);
+
+insert into group_extra_revision (id,group_id,key,value,state,revision_id,continuity_id) select id,group_id,key,value,state, '%(id)s' , id from group_extra where group_extra.id not in (select id from group_extra_revision);
+
+insert into resource_group_revision (id,package_id,label,sort_order,extras,state,revision_id,continuity_id) select id,package_id,label,sort_order,extras,state, '%(id)s', id from resource_group where resource_group.id not in (select id from resource_group_revision);
+
+insert into package_extra_revision (id,package_id,key,value,revision_id,state,continuity_id) select id,package_id,key,value, '%(id)s',state, id from package_extra where package_extra.id not in (select id from package_extra_revision);
+
+insert into package_relationship_revision (id,subject_package_id,object_package_id,type,comment,revision_id,state,continuity_id) select id,subject_package_id,object_package_id,type,comment, '%(id)s',state, id from package_relationship where package_relationship.id not in (select id from package_relationship_revision);
+                           
+insert into group_revision (id,name,title,description,created,state,revision_id,continuity_id) select id,name,title,description,created,state, '%(id)s', id from "group" where "group".id not in (select id from group_revision);
+
+insert into package_revision (id,name,title,url,notes,license_id,revision_id,version,author,author_email,maintainer,maintainer_email,state,continuity_id) select id,name,title,url,notes,license_id, '%(id)s',version,author,author_email,maintainer,maintainer_email,state, id from package where package.id not in (select id from package_revision);
+
+''' % dict(id=id, timestamp=datetime.datetime.now().isoformat())
+
+
+    update_schema = '''
+ALTER TABLE package_revision
+	ADD COLUMN expired_id text,
+	ADD COLUMN revision_timestamp timestamp without time zone,
+	ADD COLUMN expired_timestamp timestamp without time zone;
+
+ALTER TABLE package_extra_revision
+	ADD COLUMN expired_id text,
+	ADD COLUMN revision_timestamp timestamp without time zone,
+	ADD COLUMN expired_timestamp timestamp without time zone;
+
+ALTER TABLE group_revision
+	ADD COLUMN expired_id text,
+	ADD COLUMN revision_timestamp timestamp without time zone,
+	ADD COLUMN expired_timestamp timestamp without time zone;
+
+ALTER TABLE group_extra_revision
+	ADD COLUMN expired_id text,
+	ADD COLUMN revision_timestamp timestamp without time zone,
+	ADD COLUMN expired_timestamp timestamp without time zone;
+
+
+ALTER TABLE package_group_revision
+	ADD COLUMN expired_id text,
+	ADD COLUMN revision_timestamp timestamp without time zone,
+	ADD COLUMN expired_timestamp timestamp without time zone;
+
+ALTER TABLE package_tag_revision
+	ADD COLUMN expired_id text,
+	ADD COLUMN revision_timestamp timestamp without time zone,
+	ADD COLUMN expired_timestamp timestamp without time zone;
+
+ALTER TABLE resource_group_revision
+	ADD COLUMN expired_id text,
+	ADD COLUMN revision_timestamp timestamp without time zone,
+	ADD COLUMN expired_timestamp timestamp without time zone;
+
+ALTER TABLE resource_revision
+	ADD COLUMN expired_id text,
+	ADD COLUMN revision_timestamp timestamp without time zone,
+	ADD COLUMN expired_timestamp timestamp without time zone;
+
+ALTER TABLE package_relationship_revision 
+	ADD COLUMN expired_id text,
+	ADD COLUMN revision_timestamp timestamp without time zone,
+	ADD COLUMN expired_timestamp timestamp without time zone;
+
+create table tmp_expired_id(id text, revision_id text, revision_timestamp timestamp, expired_timestamp timestamp, expired_id text);
+create index id_exp on tmp_expired_id(id, revision_id);
+
+--package revision
+truncate tmp_expired_id;
+insert into tmp_expired_id select pr.id, revision_id, timestamp, lead(timestamp, 1, '9999-12-31') over (partition by pr.id order by timestamp), lead(pr.revision_id) over (partition by pr.id order by timestamp) from package_revision pr join revision r on pr.revision_id = r.id;
+update package_revision pr set revision_timestamp = (select revision_timestamp from tmp_expired_id tmp where tmp.revision_id = pr.revision_id and tmp.id = pr.id),
+                               expired_timestamp = (select expired_timestamp from tmp_expired_id tmp where tmp.revision_id = pr.revision_id and tmp.id = pr.id),
+                               expired_id = (select expired_id from tmp_expired_id tmp where tmp.revision_id = pr.revision_id and tmp.id = pr.id);
+
+create index idx_package_period on package_revision(revision_timestamp, expired_timestamp, id);
+create index idx_package_expired on package_revision(expired_timestamp);
+
+--package extra revision 
+truncate tmp_expired_id;
+insert into tmp_expired_id select pr.id, revision_id, timestamp, lead(timestamp, 1, '9999-12-31') over (partition by pr.id order by timestamp), lead(pr.revision_id) over (partition by pr.id order by timestamp) from package_extra_revision pr join revision r on pr.revision_id = r.id;
+update package_extra_revision pr set revision_timestamp = (select revision_timestamp from tmp_expired_id tmp where tmp.revision_id = pr.revision_id and tmp.id = pr.id),
+                               expired_timestamp = (select expired_timestamp from tmp_expired_id tmp where tmp.revision_id = pr.revision_id and tmp.id = pr.id),
+                               expired_id = (select expired_id from tmp_expired_id tmp where tmp.revision_id = pr.revision_id and tmp.id = pr.id);
+
+create index idx_package_extra_period on package_extra_revision(revision_timestamp, expired_timestamp, id);
+create index idx_package_extra_period_package on package_extra_revision(revision_timestamp, expired_timestamp, package_id);
+create index idx_package_extra_expired on package_extra_revision(expired_timestamp);
+
+--package group revision
+truncate tmp_expired_id;
+insert into tmp_expired_id select pr.id, revision_id, timestamp, lead(timestamp, 1, '9999-12-31') over (partition by pr.id order by timestamp), lead(pr.revision_id) over (partition by pr.id order by timestamp) from package_group_revision pr join revision r on pr.revision_id = r.id;
+update package_group_revision pr set revision_timestamp = (select revision_timestamp from tmp_expired_id tmp where tmp.revision_id = pr.revision_id and tmp.id = pr.id),
+                               expired_timestamp = (select expired_timestamp from tmp_expired_id tmp where tmp.revision_id = pr.revision_id and tmp.id = pr.id),
+                               expired_id = (select expired_id from tmp_expired_id tmp where tmp.revision_id = pr.revision_id and tmp.id = pr.id);
+
+create index idx_package_group_period_package_group on package_group_revision(revision_timestamp, expired_timestamp, package_id, group_id);
+create index idx_package_group_expired on package_group_revision(expired_timestamp);
+
+
+-- package_tags
+truncate tmp_expired_id;
+insert into tmp_expired_id select pr.id, revision_id, timestamp, lead(timestamp, 1, '9999-12-31') over (partition by pr.id order by timestamp), lead(pr.revision_id) over (partition by pr.id order by timestamp) from package_tag_revision pr join revision r on pr.revision_id = r.id;
+update package_tag_revision pr set revision_timestamp = (select revision_timestamp from tmp_expired_id tmp where tmp.revision_id = pr.revision_id and tmp.id = pr.id),
+                               expired_timestamp = (select expired_timestamp from tmp_expired_id tmp where tmp.revision_id = pr.revision_id and tmp.id = pr.id),
+                               expired_id = (select expired_id from tmp_expired_id tmp where tmp.revision_id = pr.revision_id and tmp.id = pr.id);
+
+create index idx_period_package_tag on package_tag_revision(revision_timestamp, expired_timestamp, package_id, tag_id);
+create index idx_package_tag_expired on package_tag_revision(expired_timestamp);
+
+-- package relationship
+truncate tmp_expired_id;
+insert into tmp_expired_id select pr.id, revision_id, timestamp, lead(timestamp, 1, '9999-12-31') over (partition by pr.id order by timestamp), lead(pr.revision_id) over (partition by pr.id order by timestamp) from package_relationship_revision pr join revision r on pr.revision_id = r.id;
+update package_relationship_revision pr set revision_timestamp = (select revision_timestamp from tmp_expired_id tmp where tmp.revision_id = pr.revision_id and tmp.id = pr.id),
+                               expired_timestamp = (select expired_timestamp from tmp_expired_id tmp where tmp.revision_id = pr.revision_id and tmp.id = pr.id),
+                               expired_id = (select expired_id from tmp_expired_id tmp where tmp.revision_id = pr.revision_id and tmp.id = pr.id);
+
+create index idx_period_package_relationship on package_relationship_revision(revision_timestamp, expired_timestamp, object_package_id, subject_package_id);
+create index idx_package_relationship_expired on package_relationship_revision(expired_timestamp);
+
+-- resource revision
+truncate tmp_expired_id;
+insert into tmp_expired_id select pr.id, revision_id, timestamp, lead(timestamp, 1, '9999-12-31') over (partition by pr.id order by timestamp), lead(pr.revision_id) over (partition by pr.id order by timestamp) from resource_revision pr join revision r on pr.revision_id = r.id;
+update resource_revision pr set revision_timestamp = (select revision_timestamp from tmp_expired_id tmp where tmp.revision_id = pr.revision_id and tmp.id = pr.id),
+                               expired_timestamp = (select expired_timestamp from tmp_expired_id tmp where tmp.revision_id = pr.revision_id and tmp.id = pr.id),
+                               expired_id = (select expired_id from tmp_expired_id tmp where tmp.revision_id = pr.revision_id and tmp.id = pr.id);
+
+create index idx_resource_period on resource_revision(revision_timestamp, expired_timestamp, id);
+create index idx_resource_period_resource_group on resource_revision(revision_timestamp, expired_timestamp, resource_group_id);
+create index idx_resource_expired on resource_revision(expired_timestamp);
+
+-- resource group revision;
+truncate tmp_expired_id;
+insert into tmp_expired_id select pr.id, revision_id, timestamp, lead(timestamp, 1, '9999-12-31') over (partition by pr.id order by timestamp), lead(pr.revision_id) over (partition by pr.id order by timestamp) from resource_group_revision pr join revision r on pr.revision_id = r.id;
+update resource_group_revision pr set revision_timestamp = (select revision_timestamp from tmp_expired_id tmp where tmp.revision_id = pr.revision_id and tmp.id = pr.id),
+                               expired_timestamp = (select expired_timestamp from tmp_expired_id tmp where tmp.revision_id = pr.revision_id and tmp.id = pr.id),
+                               expired_id = (select expired_id from tmp_expired_id tmp where tmp.revision_id = pr.revision_id and tmp.id = pr.id);
+
+create index idx_resource_group_period on resource_group_revision(revision_timestamp, expired_timestamp, id);
+create index idx_resource_group_period_package on resource_group_revision(revision_timestamp, expired_timestamp, package_id);
+create index idx_resource_group_expired on resource_group_revision(expired_timestamp);
+
+--group revision;
+truncate tmp_expired_id;
+insert into tmp_expired_id select pr.id, revision_id, timestamp, lead(timestamp, 1, '9999-12-31') over (partition by pr.id order by timestamp), lead(pr.revision_id) over (partition by pr.id order by timestamp) from group_revision pr join revision r on pr.revision_id = r.id;
+update group_revision pr set revision_timestamp = (select revision_timestamp from tmp_expired_id tmp where tmp.revision_id = pr.revision_id and tmp.id = pr.id),
+                               expired_timestamp = (select expired_timestamp from tmp_expired_id tmp where tmp.revision_id = pr.revision_id and tmp.id = pr.id),
+                               expired_id = (select expired_id from tmp_expired_id tmp where tmp.revision_id = pr.revision_id and tmp.id = pr.id);
+
+create index idx_group_period on group_revision(revision_timestamp, expired_timestamp, id);
+create index idx_group_expired on group_revision(expired_timestamp);
+
+--group extra revision 
+truncate tmp_expired_id;
+insert into tmp_expired_id select pr.id, revision_id, timestamp, lead(timestamp, 1, '9999-12-31') over (partition by pr.id order by timestamp), lead(pr.revision_id) over (partition by pr.id order by timestamp) from group_extra_revision pr join revision r on pr.revision_id = r.id;
+update group_extra_revision pr set revision_timestamp = (select revision_timestamp from tmp_expired_id tmp where tmp.revision_id = pr.revision_id and tmp.id = pr.id),
+                               expired_timestamp = (select expired_timestamp from tmp_expired_id tmp where tmp.revision_id = pr.revision_id and tmp.id = pr.id),
+                               expired_id = (select expired_id from tmp_expired_id tmp where tmp.revision_id = pr.revision_id and tmp.id = pr.id);
+
+create index idx_group_extra_period on group_extra_revision(revision_timestamp, expired_timestamp, id);
+create index idx_group_extra_period_group on group_extra_revision(revision_timestamp, expired_timestamp, group_id);
+create index idx_group_extra_expired on group_extra_revision(expired_timestamp);
+
+drop table tmp_expired_id;
+'''
+    
+    migrate_engine.execute('begin;  ' + make_missing_revisions + update_schema + ' commit;')
+    
+    for table in ['package', 'resource', 'resource_group', 'package_extra', 
+                  'package_tag', 'package_relationship', 'group', 'group_extra']:
+        print table
+        count = migrate_engine.execute('''select count(*) from "%s"''' % table).first()[0]
+        revision_expired_id_count = migrate_engine.execute('''select count(*) from %s_revision where %s_revision.expired_id is null''' % (table, table)).first()[0]
+        revision_expired_data_count = migrate_engine.execute('''select count(*) from %s_revision where %s_revision.expired_timestamp = '9999-12-31' ''' % (table, table)).first()[0]
+        assert count == revision_expired_id_count
+        assert count == revision_expired_data_count
+
+
+    
+    package_count = migrate_engine.execute('''select count(*) from package''').first()[0]
+    resource_count = migrate_engine.execute('''select count(*) from resource''').first()[0]


http://bitbucket.org/okfn/ckan/changeset/76d2c432f5ef/
changeset:   76d2c432f5ef
branches:    
user:        kindly
date:        2011-05-26 18:02:36
summary:     [model] add approved timestamp to revision
affected #:  2 files (83 bytes)

--- a/ckan/migration/versions/039_add_expired_id_and_dates.py	Thu May 26 01:38:44 2011 +0100
+++ b/ckan/migration/versions/039_add_expired_id_and_dates.py	Thu May 26 17:02:36 2011 +0100
@@ -78,6 +78,9 @@
 	ADD COLUMN revision_timestamp timestamp without time zone,
 	ADD COLUMN expired_timestamp timestamp without time zone;
 
+ALTER TABLE revision
+	ADD COLUMN approved_timestamp timestamp without time zone;
+
 create table tmp_expired_id(id text, revision_id text, revision_timestamp timestamp, expired_timestamp timestamp, expired_id text);
 create index id_exp on tmp_expired_id(id, revision_id);
 
@@ -177,20 +180,20 @@
 create index idx_group_extra_expired on group_extra_revision(expired_timestamp);
 
 drop table tmp_expired_id;
+
+-- change state of revision tables
+
+update revision set state = 'active', approved_timestamp = timestamp;
 '''
     
     migrate_engine.execute('begin;  ' + make_missing_revisions + update_schema + ' commit;')
     
     for table in ['package', 'resource', 'resource_group', 'package_extra', 
                   'package_tag', 'package_relationship', 'group', 'group_extra']:
-        print table
         count = migrate_engine.execute('''select count(*) from "%s"''' % table).first()[0]
         revision_expired_id_count = migrate_engine.execute('''select count(*) from %s_revision where %s_revision.expired_id is null''' % (table, table)).first()[0]
         revision_expired_data_count = migrate_engine.execute('''select count(*) from %s_revision where %s_revision.expired_timestamp = '9999-12-31' ''' % (table, table)).first()[0]
         assert count == revision_expired_id_count
         assert count == revision_expired_data_count
 
-
     
-    package_count = migrate_engine.execute('''select count(*) from package''').first()[0]
-    resource_count = migrate_engine.execute('''select count(*) from resource''').first()[0]


--- a/ckan/model/core.py	Thu May 26 01:38:44 2011 +0100
+++ b/ckan/model/core.py	Thu May 26 17:02:36 2011 +0100
@@ -6,6 +6,7 @@
 
 ## VDM-specific tables
 revision_table = vdm.sqlalchemy.make_revision_table(metadata)
+revision_table.append_column(Column('approved_timestamp', DateTime))
 
 class System(DomainObject):
     


http://bitbucket.org/okfn/ckan/changeset/17dbec3235d1/
changeset:   17dbec3235d1
branches:    
user:        kindly
date:        2011-06-01 01:06:47
summary:     [logic] remove references to vdm collections
affected #:  12 files (5.4 KB)

--- a/ckan/lib/dictization/__init__.py	Thu May 26 17:02:36 2011 +0100
+++ b/ckan/lib/dictization/__init__.py	Wed Jun 01 00:06:47 2011 +0100
@@ -111,6 +111,10 @@
             continue
         setattr(obj, key, value)
 
+    if context.get('pending'):
+        if session.is_modified(obj, include_collections=False):
+            obj.state = 'pending'
+
     session.add(obj)
 
     return obj


--- a/ckan/lib/dictization/model_save.py	Thu May 26 17:02:36 2011 +0100
+++ b/ckan/lib/dictization/model_save.py	Wed Jun 01 00:06:47 2011 +0100
@@ -32,20 +32,83 @@
         else:
             obj.extras[key] = value
 
+    if context.get('pending'):
+        if session.is_modified(obj, include_collections=False):
+            obj.state = 'pending'
+
     session.add(obj)
 
     return obj
 
-def resource_list_save(res_dicts, context):
+def package_resource_list_save(res_dicts, package, context):
+
+    pending = context.get('pending')
+
+    resource_list = package.resource_groups_all[0].resources_all
+    old_list = package.resource_groups_all[0].resources_all[:]
 
     obj_list = []
     for res_dict in res_dicts:
         obj = resource_dict_save(res_dict, context)
         obj_list.append(obj)
 
-    return obj_list
+    resource_list[:] = obj_list
 
-def extras_save(extras_dicts, context):
+    for resource in set(old_list) - set(obj_list):
+        if pending and resource.state <> 'deleted':
+            resource.state = 'pending-deleted'
+        else:
+            resource.state = 'deleted'
+        resource_list.append(resource)
+    tag_package_tag = dict((package_tag.tag, package_tag) 
+                            for package_tag in
+                            package.package_tag_all)
+
+
+def package_extras_save(extra_dicts, obj, context):
+
+    allow_partial_update = context.get("allow_partial_update", False)
+    if not extra_dicts and allow_partial_update:
+        return
+    model = context["model"]
+    session = context["session"]
+
+    extras_as_string = context.get("extras_as_string", False)
+    extras_list = obj.extras_list
+    old_extras = dict((extra.key, extra) for extra in extras_list)
+
+    new_extras = {}
+    for extra_dict in extra_dicts:
+        if extra_dict.get("deleted"):
+            continue
+        if extras_as_string:
+            new_extras[extra_dict["key"]] = extra_dict["value"]
+        else:
+            new_extras[extra_dict["key"]] = json.loads(extra_dict["value"])
+    #new
+    for key in set(new_extras.keys()) - set(old_extras.keys()):
+        state = 'pending' if context.get('pending') else '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:
+            continue
+        state = 'pending' if context.get('pending') else '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 = 'pending-deleted' if context.get('pending') else 'delete'
+        extra.state = state
+
+def group_extras_save(extras_dicts, context):
 
     model = context["model"]
     session = context["session"]
@@ -62,25 +125,53 @@
 
     return result_dict
 
+def package_tag_list_save(tag_dicts, package, context):
 
-def tag_list_save(tag_dicts, context):
+    allow_partial_update = context.get("allow_partial_update", False)
+    if not tag_dicts and allow_partial_update:
+        return
 
     model = context["model"]
     session = context["session"]
+    pending = context.get('pending')
 
-    tag_list = []
-    for table_dict in tag_dicts:
-        obj = table_dict_save(table_dict, model.Tag, context)
-        tag_list.append(obj)
+    tag_package_tag = dict((package_tag.tag, package_tag) 
+                            for package_tag in
+                            package.package_tag_all)
 
-    return list(set(tag_list))
+    tags = set()
+    for tag_dict in tag_dicts:
+        obj = table_dict_save(tag_dict, model.Tag, context)
+        tags.add(obj)
 
-def group_list_save(group_dicts, context):
+    for tag in set(tag_package_tag.keys()) - tags:
+        package_tag = tag_package_tag[tag]
+        if pending and package_tag.state <> 'deleted':
+            package_tag.state = 'pending-deleted'
+        else:
+            package_tag.state = 'deleted'
+
+    for tag in tags - set(tag_package_tag.keys()):
+        package_tag_obj = model.PackageTag(package, tag, 'active')
+        session.add(package_tag_obj)
+        tag_package_tag[tag] = package_tag_obj
+
+    package.package_tag_all[:] = tag_package_tag.values()
+
+def package_group_list_save(group_dicts, package, context):
+
+    allow_partial_update = context.get("allow_partial_update", False)
+    if not group_dicts and allow_partial_update:
+        return
 
     model = context["model"]
     session = context["session"]
+    pending = context.get('pending')
 
-    group_list = []
+    group_package_group = dict((package_group.group, package_group) 
+                               for package_group in
+                               package.package_group_all)
+    groups = set()
     for group_dict in group_dicts:
         id = group_dict.get("id")
         name = group_dict.get("name")
@@ -88,23 +179,56 @@
             group = session.query(model.Group).get(id)
         else:
             group = session.query(model.Group).filter_by(name=name).first()
+        groups.add(group)
 
-        group_list.append(group)
+    for group in groups - set(group_package_group.keys()):
+        package_group_obj = model.PackageGroup(package = package,
+                                               group = group,
+                                               state = 'active')
+        session.add(package_group_obj)
+        group_package_group[group] = package_group_obj
 
-    return group_list
+    for group in set(group_package_group.keys()) - groups:
+        group_package_group.pop(group)
+        continue
+        ### this is alternate behavioiur below which is correct
+        ### but not compatible with old behaviour
+        package_group = group_package_group[group]
+        if pending and package_group.state <> 'deleted':
+            package_group.state = 'pending-deleted'
+        else:
+            package_group.state = 'deleted'
+
+
+    package.package_group_all[:] = group_package_group.values()
+
     
-def relationship_list_save(relationship_dicts, context):
+def relationship_list_save(relationship_dicts, package, attr, context):
 
+    allow_partial_update = context.get("allow_partial_update", False)
+    if not relationship_dicts and allow_partial_update:
+        return
     model = context["model"]
     session = context["session"]
+    pending = context.get('pending')
 
-    relationship_list = []
+    relationship_list = getattr(package, attr)
+    old_list = relationship_list[:]
+
+    relationships = []
     for relationship_dict in relationship_dicts:
         obj = table_dict_save(relationship_dict, 
                               model.PackageRelationship, context)
-        relationship_list.append(obj)
+        relationships.append(obj)
 
-    return relationship_list
+    relationship_list[:] = relationships
+
+    for relationship in set(old_list) - set(relationship_list):
+        if pending and relationship.state <> 'deleted':
+            relationship.state = 'pending-deleted'
+        else:
+            relationship.state = 'deleted'
+        relationship_list.append(relationship)
 
 def package_dict_save(pkg_dict, context):
 
@@ -117,33 +241,16 @@
 
     pkg = table_dict_save(pkg_dict, Package, context)
 
-    resources = resource_list_save(pkg_dict.get("resources", []), context)
-    if resources:
-        pkg.resources[:] = resources
+    package_resource_list_save(pkg_dict.get("resources", []), pkg, context)
+    package_tag_list_save(pkg_dict.get("tags", []), pkg, context)
+    package_group_list_save(pkg_dict.get("groups", []), pkg, context)
 
-    tags = tag_list_save(pkg_dict.get("tags", []), context)
-    if tags or not allow_partial_update:
-        pkg.tags[:] = tags
+    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)
 
-    groups = group_list_save(pkg_dict.get("groups", []), context)
-    if groups or not allow_partial_update:
-        pkg.groups[:] = groups
-
-    subjects = pkg_dict.get("relationships_as_subject", [])
-    if subjects or not allow_partial_update:
-        pkg.relationships_as_subject[:] = relationship_list_save(subjects, context)
-    objects = pkg_dict.get("relationships_as_object", [])
-    if objects or not allow_partial_update:
-        pkg.relationships_as_object[:] = relationship_list_save(objects, context)
-
-    extras = extras_save(pkg_dict.get("extras", {}), context)
-    if extras or not allow_partial_update:
-        old_extras = set(pkg.extras.keys())
-        new_extras = set(extras.keys())
-        for key in old_extras - new_extras:
-            del pkg.extras[key]
-        for key in new_extras:
-            pkg.extras[key] = extras[key] 
+    extras = package_extras_save(pkg_dict.get("extras", []), pkg, context)
 
     return pkg
 
@@ -161,7 +268,7 @@
         group_dict["id"] = group.id 
 
     group = table_dict_save(group_dict, Group, context)
-    extras = extras_save(group_dict.get("extras", {}), context)
+    extras = group_extras_save(group_dict.get("extras", {}), context)
     if extras or not allow_partial_update:
         old_extras = set(group.extras.keys())
         new_extras = set(extras.keys())


--- a/ckan/lib/package_saver.py	Thu May 26 17:02:36 2011 +0100
+++ b/ckan/lib/package_saver.py	Wed Jun 01 00:06:47 2011 +0100
@@ -45,7 +45,10 @@
         c.pkg_maintainer_link = cls._person_email_link(c.pkg.maintainer, c.pkg.maintainer_email, "Maintainer")
         c.package_relationships = pkg.get_relationships_printable()
         c.pkg_extras = []
-        for k, v in sorted(pkg.extras.items()):
+        for extra in sorted(pkg.extras_list):
+            if extra.state == 'deleted':
+                continue
+            k, v = extra.key, extra.value
             if k in g.package_hide_extras:
                 continue
             if isinstance(v, (list, tuple)):


--- a/ckan/model/domain_object.py	Thu May 26 17:02:36 2011 +0100
+++ b/ckan/model/domain_object.py	Wed Jun 01 00:06:47 2011 +0100
@@ -1,8 +1,9 @@
 import datetime
 
+from sqlalchemy import orm
 from sqlalchemy.util import OrderedDict
 
-from meta import *
+from meta import Session 
 
 class Enum(set):
     '''Simple enumeration


--- a/ckan/model/group.py	Thu May 26 17:02:36 2011 +0100
+++ b/ckan/model/group.py	Wed Jun 01 00:06:47 2011 +0100
@@ -123,7 +123,8 @@
     'packages': relation(Package, secondary=package_group_table,
         backref='groups',
         order_by=package_table.c.name
-    )},
+    ),
+},
     extension=[vdm.sqlalchemy.Revisioner(group_revision_table),],
 )
 
@@ -133,7 +134,14 @@
         group_revision_table)
 
 
-mapper(PackageGroup, package_group_table,
+mapper(PackageGroup, package_group_table, properties={
+    'group': relation(Group,
+        backref='package_group_all',
+    ),
+    'package': relation(Package,
+        backref='package_group_all',
+    ),
+},
     extension=[vdm.sqlalchemy.Revisioner(package_group_revision_table),],
 )
 


--- a/ckan/model/meta.py	Thu May 26 17:02:36 2011 +0100
+++ b/ckan/model/meta.py	Wed Jun 01 00:06:47 2011 +0100
@@ -1,3 +1,4 @@
+from datetime import datetime
 """SQLAlchemy Metadata and Session object"""
 from sqlalchemy import MetaData, __version__ as sqav
 from sqlalchemy.orm import scoped_session, sessionmaker
@@ -28,6 +29,7 @@
         session._object_cache['deleted'].update(session.deleted)
         session._object_cache['changed'].update(changed)
 
+
     def before_commit(self, session):
         session.flush()
         try:
@@ -44,11 +46,17 @@
             if not hasattr(obj, '__revision_class__'):
                 continue
 
+            if 'pending' not in obj.state:
+                revision.approved_timestamp = datetime.now()
+
             revision_cls = obj.__revision_class__
             q = session.query(revision_cls)
             q = q.filter_by(expired_timestamp='9999-12-31', id=obj.id)
+            if 'pending' in obj.state:
+                q = q.filter(revision_cls.state.in_(
+                    'pending-active', 'pending-deleted'))
+                revision.state = 'pending'
             results = q.all()
-            assert len(results) <= 2
 
             for rev_obj in results:
                 if rev_obj.revision_id == revision.id:


--- a/ckan/model/package.py	Thu May 26 17:02:36 2011 +0100
+++ b/ckan/model/package.py	Wed Jun 01 00:06:47 2011 +0100
@@ -2,14 +2,15 @@
 from time import gmtime
 from calendar import timegm
 
-from sqlalchemy.sql import select, and_, union, expression
+from sqlalchemy.sql import select, and_, union, expression, or_
 from sqlalchemy.orm import eagerload_all
+from sqlalchemy import types, Column, Table
 from pylons import config
-from meta import *
+from meta import metadata, Session
 import vdm.sqlalchemy
 
 from types import make_uuid
-from core import *
+from core import make_revisioned_table, Revision, State
 from license import License, LicenseRegister
 from domain_object import DomainObject
 import ckan.misc


--- a/ckan/model/package_extra.py	Thu May 26 17:02:36 2011 +0100
+++ b/ckan/model/package_extra.py	Wed Jun 01 00:06:47 2011 +0100
@@ -33,6 +33,11 @@
             collection_class=orm.collections.attribute_mapped_collection(u'key'),
             cascade='all, delete, delete-orphan',
             ),
+        ),
+    'package_no_state': orm.relation(Package,
+        backref=orm.backref('extras_list',
+            cascade='all, delete, delete-orphan',
+            ),
         )
     },
     order_by=[package_extra_table.c.package_id, package_extra_table.c.key],


--- a/ckan/model/tag.py	Thu May 26 17:02:36 2011 +0100
+++ b/ckan/model/tag.py	Wed Jun 01 00:06:47 2011 +0100
@@ -103,12 +103,15 @@
 mapper(Tag, tag_table, properties={
     'package_tags':relation(PackageTag, backref='tag',
         cascade='all, delete, delete-orphan',
-        )
+        ),
     },
     order_by=tag_table.c.name,
     )
 
 mapper(PackageTag, package_tag_table, properties={
+    'pkg':relation(Package, backref='package_tag_all',
+        cascade='none',
+        )
     },
     order_by=package_tag_table.c.id,
     extension=[vdm.sqlalchemy.Revisioner(package_tag_revision_table),


--- a/ckan/tests/functional/api/model/test_package.py	Thu May 26 17:02:36 2011 +0100
+++ b/ckan/tests/functional/api/model/test_package.py	Wed Jun 01 00:06:47 2011 +0100
@@ -260,6 +260,7 @@
         # - url
         self.assert_equal(package.url, self.package_fixture_data['url'])
         # - extras
+
         self.assert_equal(len(package.extras), 4)
         for key, value in {u'key1':u'val1',
                            u'key3':u'val3',


--- a/ckan/tests/functional/test_authz.py	Thu May 26 17:02:36 2011 +0100
+++ b/ckan/tests/functional/test_authz.py	Wed Jun 01 00:06:47 2011 +0100
@@ -188,6 +188,7 @@
         res = func(offset, params=postparams,
                    extra_environ=environ,
                    expect_errors=True)
+        
         tests = {}
         tests['str_required (%s)' % str_required_in_response] = bool(str_required_in_response in res)
         tests['error string'] = bool('error' not in res)
@@ -313,7 +314,7 @@
         cls.groupreader = model.User.by_name(u'groupreader')
         cls.mrloggedin = model.User.by_name(name=u'mrloggedin')
         cls.visitor = model.User.by_name(name=model.PSEUDO_USER__VISITOR)
-        
+
     # Tests numbered by the use case
 
     def test_14_visitor_reads_stopped(self):


--- a/ckan/tests/functional/test_package.py	Thu May 26 17:02:36 2011 +0100
+++ b/ckan/tests/functional/test_package.py	Wed Jun 01 00:06:47 2011 +0100
@@ -893,6 +893,7 @@
             fv = res.forms['package-edit']
             fv[field_name] = False
             res = fv.submit('save', extra_environ={'REMOTE_USER':'russianfan'})
+            model.repo.commit_and_remove()
             pkg = model.Package.by_name(u'editpkgtest')
             assert len(pkg.groups) == 0
         finally:


http://bitbucket.org/okfn/ckan/changeset/697fa9384db4/
changeset:   697fa9384db4
branches:    
user:        kindly
date:        2011-06-06 16:50:46
summary:     [moderated edits] add command to make latest revision active and tests
affected #:  6 files (13.2 KB)

--- a/ckan/lib/dictization/model_dictize.py	Wed Jun 01 00:06:47 2011 +0100
+++ b/ckan/lib/dictization/model_dictize.py	Mon Jun 06 15:50:46 2011 +0100
@@ -52,7 +52,7 @@
 
     result_dict = table_dictize(pkg, context)
 
-    result_dict["resources"] = resource_list_dictize(pkg.resources, context)
+    result_dict["resources"] = resource_list_dictize(pkg.resource_groups[0].resources_all, context)
 
     result_dict["tags"] = obj_list_dictize(
         pkg.tags, context, lambda x: x["name"])


--- a/ckan/lib/dictization/model_save.py	Wed Jun 01 00:06:47 2011 +0100
+++ b/ckan/lib/dictization/model_save.py	Mon Jun 06 15:50:46 2011 +0100
@@ -127,6 +127,7 @@
 
 def package_tag_list_save(tag_dicts, package, context):
 
+    
     allow_partial_update = context.get("allow_partial_update", False)
     if not tag_dicts and allow_partial_update:
         return
@@ -152,7 +153,8 @@
             package_tag.state = 'deleted'
 
     for tag in tags - set(tag_package_tag.keys()):
-        package_tag_obj = model.PackageTag(package, tag, 'active')
+        state = 'pending' if pending else 'active'
+        package_tag_obj = model.PackageTag(package, tag, state)
         session.add(package_tag_obj)
         tag_package_tag[tag] = package_tag_obj
 


--- a/ckan/logic/action/update.py	Wed Jun 01 00:06:47 2011 +0100
+++ b/ckan/logic/action/update.py	Mon Jun 06 15:50:46 2011 +0100
@@ -1,5 +1,6 @@
 import logging
 import re
+import datetime
 
 import ckan.authz
 from ckan.plugins import PluginImplementations, IGroupController, IPackageController
@@ -67,8 +68,73 @@
     for group in groups:
         check_access(group, model.Action.EDIT, context)
 
+def _make_latest_rev_active(context, obj_list):
+
+    latest_rev = sorted(obj_list, key=lambda x: x.revision_timestamp)[-1]
+    if latest_rev.state == 'pending-deleted':
+        latest_rev.state = 'deleted'
+    else:
+        latest_rev.state = 'active'
+    for obj_rev in obj_list:
+        if obj_rev == latest_rev:
+            continue
+
+        obj_rev.expired_id = latest_rev.revision_id
+        obj_rev.expired_timestamp = latest_rev.revision_timestamp
+        context['model'].Session.add(obj_rev)
+        
+        ##this is just a way to get the latest revision that changed
+        ##in order to timestamp
+        old_latest = context.get('latest_revision_date')
+        if old_latest:
+            if latest_rev.revision_timestamp > old_latest:
+                context['latest_revision_date'] = latest_rev.revision_timestamp
+                context['latest_revision'] = latest_rev.revision_id
+
+def make_latest_pending_package_active(context):
+
+    model = context['model']
+    session = model.Session
+    id = context["id"]
+    pkg = model.Package.get(id)
+
+    check_access(pkg, model.Action.EDIT, context)
+
+    #packages
+    q = session.query(model.PackageRevision)
+    pkgrevs = q.filter_by(id=id, expired_timestamp='9999-12-31').all()
+    _make_latest_rev_active(context, pkgrevs)
+
+    #resources
+    for resource in pkg.resource_groups_all[0].resources_all:
+        res_revs = session.query(model.ResourceRevision).filter_by(
+            id=resource.id, expired_timestamp='9999-12-31').all()
+        _make_latest_rev_active(context, res_revs)
+
+    #tags
+    for tag in pkg.package_tag_all:
+        tags_revs = session.query(model.PackageTagRevision).filter_by(
+            id=tag.id, expired_timestamp='9999-12-31').all()
+        _make_latest_rev_active(context, tags_revs)
+
+    #extras
+    for extra in pkg.extras_list:
+        extras_revs = session.query(model.PackageExtraRevision).filter_by(
+            id=extra.id, expired_timestamp='9999-12-31').all()
+        _make_latest_rev_active(context, extras_revs)
+
+    latest_revision = context.get('latest_revision')
+    if not latest_revision:
+        return
+
+    q = session.query(model.ResourceRevision).filter_by(id=latest_revision)
+    revision = q.first()
+    revision.approved_timestamp = datetime.datetime.now()
+    session.add(revision)
+    model.repo.commit()        
+
+
 def package_update(data_dict, context):
-
     model = context['model']
     user = context['user']
     id = context["id"]
@@ -156,8 +222,6 @@
     comment = data_dict.get('comment', u'')
     return _update_package_relationship(entity, comment, context)
 
-
-
 def group_update(data_dict, context):
 
     model = context['model']


--- a/ckan/model/meta.py	Wed Jun 01 00:06:47 2011 +0100
+++ b/ckan/model/meta.py	Mon Jun 06 15:50:46 2011 +0100
@@ -53,8 +53,8 @@
             q = session.query(revision_cls)
             q = q.filter_by(expired_timestamp='9999-12-31', id=obj.id)
             if 'pending' in obj.state:
-                q = q.filter(revision_cls.state.in_(
-                    'pending-active', 'pending-deleted'))
+                q = q.filter(revision_cls.state.in_([
+                    'pending', 'pending-deleted']))
                 revision.state = 'pending'
             results = q.all()
 


--- a/ckan/tests/functional/test_package.py	Wed Jun 01 00:06:47 2011 +0100
+++ b/ckan/tests/functional/test_package.py	Mon Jun 06 15:50:46 2011 +0100
@@ -293,6 +293,7 @@
         for txt in txt_order_non_deterministic:
             for pkg_ in (pkg_by_name_main, pkg_by_id_main):
                 pkg_ = pkg_.replace(txt, 'placeholder')
+        print pkg_by_name_main
         res_diff = self.diff_html(pkg_by_name_main, pkg_by_id_main)
         assert not res_diff, res_diff.encode('utf8')
         # not true as language selection link return url differs: 


--- a/ckan/tests/lib/test_dictization.py	Wed Jun 01 00:06:47 2011 +0100
+++ b/ckan/tests/lib/test_dictization.py	Mon Jun 06 15:50:46 2011 +0100
@@ -13,13 +13,13 @@
                                                 package_to_api1,
                                                 package_to_api2,
                                                )
-
 from ckan.lib.dictization.model_save import (package_dict_save,
                                              resource_dict_save,
                                              group_dict_save,
                                              package_api_to_dict,
                                              group_api_to_dict,
                                             )
+from ckan.logic.action.update import make_latest_pending_package_active
 
 class TestBasicDictize:
     @classmethod
@@ -31,6 +31,11 @@
         model.repo.rebuild_db()
         model.Session.remove()
 
+    def teardonwn(self):
+        model.Session.remove()
+
+
+
     def remove_changable_columns(self, dict):
         for key, value in dict.items():
             if key.endswith('id') and key <> 'license_id':
@@ -54,10 +59,9 @@
     def test_01_dictize_main_objects_simple(self):
         
         context = {"model": model,
-                 "session": model.Session}
+                   "session": model.Session}
 
         ## package
-
         pkg = model.Session.query(model.Package).filter_by(name='annakarenina').first()
         result = table_dictize(pkg, context)
         self.remove_changable_columns(result)
@@ -112,6 +116,7 @@
         context = {"model": model,
                  "session": model.Session}
 
+        model.Session.remove()
         pkg = model.Session.query(model.Package).filter_by(name='annakarenina').first()
 
         result = package_dictize(pkg, context)
@@ -121,7 +126,11 @@
         assert result ==\
             {'author': None,
              'author_email': None,
-             'extras': [{'key': u'original media', 'state': u'active', 'value': u'"book"'}],
+             'extras': [
+                {'key': u'genre',
+                 'state': u'active',
+                 'value': '"romantic novel"'},
+                {'key': u'original media', 'state': u'active', 'value': u'"book"'}],
              'groups': [{'description': u'These are books that David likes.',
                          'name': u'david',
                          'state': u'active',
@@ -257,10 +266,12 @@
     def test_08_package_save(self):
 
         context = {"model": model,
-                 "session": model.Session}
+                   "session": model.Session}
 
         anna1 = model.Session.query(model.Package).filter_by(name='annakarenina').one()
 
+        
+
         anna_dictized = self.remove_changable_columns(package_dictize(anna1, context))
 
         anna_dictized["name"] = u'annakarenina3' 
@@ -275,12 +286,10 @@
 
         anna_original = pformat(anna_dictized)
         anna_after_save = pformat(package_dictized)
-        print anna_original
-        print anna_after_save
 
         assert self.remove_changable_columns(package_dictize(pkg, context)) == anna_dictized, "\n".join(unified_diff(anna_original.split("\n"), anna_after_save.split("\n")))
 
-    def test_10_package_alter(self):
+    def test_09_package_alter(self):
 
         context = {"model": model,
                  "session": model.Session}
@@ -307,11 +316,237 @@
         print anna_original
         print anna_after_save
 
+
         assert self.remove_revision_id(anna_dictized) == self.remove_revision_id(package_dictized),\
                 "\n".join(unified_diff(anna_original.split("\n"), anna_after_save.split("\n")))
 
+    def test_10_package_alter_pending(self):
 
-    def test_11_resource_no_id(self):
+        context = {'model': model,
+                   'session': model.Session,
+                   'pending': True}
+
+        anna1 = model.Session.query(model.Package).filter_by(name='annakarenina_changed').one()
+
+        anna_dictized = package_dictize(anna1, context)
+
+        anna_dictized['name'] = u'annakarenina_changed2' 
+        anna_dictized['resources'][0]['url'] = u'new_url2' 
+        anna_dictized['tags'][0]['name'] = u'new_tag' 
+        anna_dictized['tags'][0].pop('id') #test if 
+        anna_dictized['extras'][0]['value'] = u'"new_value"' 
+
+        model.repo.new_revision()
+        package_dict_save(anna_dictized, context)
+        model.Session.commit()
+        model.Session.remove()
+
+        pkgrevisions = model.Session.query(model.PackageRevision).filter_by(id=anna1.id).all()
+
+        sorted_packages = sorted(pkgrevisions, key=lambda x:x.revision_timestamp)[::-1]
+
+        assert len(sorted_packages) == 3
+        assert sorted_packages[0].state == 'pending'
+        assert sorted_packages[1].state == 'active'
+        assert sorted_packages[2].state == 'active'
+
+        assert str(sorted_packages[0].expired_timestamp) == '9999-12-31 00:00:00'
+        assert str(sorted_packages[1].expired_timestamp) == '9999-12-31 00:00:00'
+        assert str(sorted_packages[2].expired_timestamp) != '9999-12-31 00:00:00'
+
+        resources_revisions = model.Session.query(model.ResourceRevision).filter_by(resource_group_id=anna1.resource_groups[0].id).all()
+
+        sorted_resources = sorted(resources_revisions, key=lambda x: (x.revision_timestamp, x.url))[::-1]
+
+        assert len(sorted_resources) == 4
+        assert sorted_resources[0].state == 'pending'
+        assert sorted_resources[1].state == 'active'
+        assert sorted_resources[2].state == 'active'
+        assert sorted_resources[3].state == 'active'
+
+        assert str(sorted_resources[0].expired_timestamp) == '9999-12-31 00:00:00'
+        assert str(sorted_resources[1].expired_timestamp) == '9999-12-31 00:00:00'
+        assert str(sorted_resources[2].expired_timestamp) == '9999-12-31 00:00:00'
+        assert str(sorted_resources[3].expired_timestamp) != '9999-12-31 00:00:00'
+
+        tag_revisions = model.Session.query(model.PackageTagRevision).filter_by(package_id=anna1.id).all()
+
+        sorted_tags = sorted(tag_revisions, key=lambda x: (x.revision_timestamp, x.tag.name))[::-1]
+
+        print [(tag.state, tag.tag.name) for tag in sorted_tags]
+
+        assert len(sorted_tags) == 4, len(sorted_tags)
+        assert sorted_tags[0].state == 'pending-deleted'
+        assert sorted_tags[1].state == 'pending'
+        assert sorted_tags[2].state == 'active'
+        assert sorted_tags[3].state == 'active'
+
+        assert str(sorted_tags[0].expired_timestamp) == '9999-12-31 00:00:00'
+        assert str(sorted_tags[1].expired_timestamp) == '9999-12-31 00:00:00'
+        assert str(sorted_tags[2].expired_timestamp) == '9999-12-31 00:00:00'
+        assert str(sorted_tags[3].expired_timestamp) == '9999-12-31 00:00:00'
+
+        extras_revisions = model.Session.query(model.PackageExtraRevision).filter_by(package_id=anna1.id).all()
+
+        sorted_extras = sorted(extras_revisions, 
+                               key=lambda x: (x.revision_timestamp, x.key))[::-1]
+
+        assert sorted_extras[0].state == 'pending'
+        assert sorted_extras[1].state == 'active'
+        assert sorted_extras[2].state == 'active'
+
+        assert str(sorted_extras[0].expired_timestamp) == '9999-12-31 00:00:00'
+        assert str(sorted_extras[1].expired_timestamp) == '9999-12-31 00:00:00'
+        assert str(sorted_extras[2].expired_timestamp) == '9999-12-31 00:00:00'
+
+
+    def test_11_add_pending(self):
+
+        context = {'model': model,
+                   'session': model.Session,
+                   'pending': True}
+
+        anna1 = model.Session.query(model.Package).filter_by(name='annakarenina_changed2').one()
+        anna_dictized = package_dictize(anna1, context)
+
+
+        anna_dictized['resources'].append({
+                            'format': u'plain text',
+                            'url': u'newurl'}
+                            )
+        anna_dictized['tags'].append({'name': u'newnew_tag'})
+        anna_dictized['extras'].append({'key': 'david', 
+                                        'value': u'"new_value"'})
+
+        model.repo.new_revision()
+        package_dict_save(anna_dictized, context)
+        model.Session.commit()
+        model.Session.remove()
+
+        resources_revisions = model.Session.query(model.ResourceRevision).filter_by(resource_group_id=anna1.resource_groups[0].id).all()
+
+        sorted_resources = sorted(resources_revisions, key=lambda x: (x.revision_timestamp, x.url))[::-1]
+
+        assert len(sorted_resources) == 5
+        assert sorted_resources[0].state == 'pending'
+        assert sorted_resources[1].state == 'pending'
+        assert sorted_resources[2].state == 'active'
+        assert sorted_resources[3].state == 'active'
+        assert sorted_resources[4].state == 'active'
+
+        assert str(sorted_resources[0].expired_timestamp) == '9999-12-31 00:00:00'
+        assert str(sorted_resources[1].expired_timestamp) == '9999-12-31 00:00:00'
+        assert str(sorted_resources[2].expired_timestamp) == '9999-12-31 00:00:00'
+        assert str(sorted_resources[3].expired_timestamp) == '9999-12-31 00:00:00'
+        assert str(sorted_resources[4].expired_timestamp) != '9999-12-31 00:00:00'
+
+        tag_revisions = model.Session.query(model.PackageTagRevision).filter_by(package_id=anna1.id).all()
+
+        sorted_tags = sorted(tag_revisions, key=lambda x: (x.revision_timestamp, x.tag.name))[::-1]
+
+        print [(tag.state, tag.tag.name) for tag in sorted_tags]
+
+        assert len(sorted_tags) == 6, len(sorted_tags)
+        assert sorted_tags[0].state == 'pending'
+        assert sorted_tags[1].state == 'pending-deleted'
+        assert sorted_tags[2].state == 'pending-deleted'
+        assert sorted_tags[3].state == 'pending'
+        assert sorted_tags[4].state == 'active'
+        assert sorted_tags[5].state == 'active'
+
+        assert str(sorted_tags[0].expired_timestamp) == '9999-12-31 00:00:00'
+        assert str(sorted_tags[1].expired_timestamp) == '9999-12-31 00:00:00'
+        assert str(sorted_tags[2].expired_timestamp) == '9999-12-31 00:00:00'
+        assert str(sorted_tags[3].expired_timestamp) != '9999-12-31 00:00:00'
+        assert str(sorted_tags[4].expired_timestamp) == '9999-12-31 00:00:00'
+
+        extras_revisions = model.Session.query(model.PackageExtraRevision).filter_by(package_id=anna1.id).all()
+
+        sorted_extras = sorted(extras_revisions, 
+                               key=lambda x: (x.revision_timestamp, x.key))[::-1]
+
+        print [(extra.state, extra.key, extra.value) for extra in sorted_extras]
+
+        assert sorted_extras[0].state == 'pending-deleted'
+        assert sorted_extras[1].state == 'pending'
+        assert sorted_extras[2].state == 'pending'
+        assert sorted_extras[3].state == 'active'
+        assert sorted_extras[4].state == 'active'
+
+        assert str(sorted_extras[0].expired_timestamp) == '9999-12-31 00:00:00'
+        assert str(sorted_extras[1].expired_timestamp) == '9999-12-31 00:00:00'
+        assert str(sorted_extras[2].expired_timestamp) != '9999-12-31 00:00:00'
+        assert str(sorted_extras[3].expired_timestamp) == '9999-12-31 00:00:00'
+        assert str(sorted_extras[4].expired_timestamp) == '9999-12-31 00:00:00'
+
+    def test_12_make_active(self):
+
+        anna1 = model.Session.query(model.Package).filter_by(name='annakarenina_changed2').one()
+        context = {"model": model,
+                   "session": model.Session,
+                   'user': 'testsysadmin',
+                   "id": anna1.id}
+        make_latest_pending_package_active(context)
+
+        resources_revisions = model.Session.query(model.ResourceRevision).filter_by(resource_group_id=anna1.resource_groups[0].id).all()
+        sorted_resources = sorted(resources_revisions, key=lambda x: (x.revision_timestamp, x.url))[::-1]
+
+        assert len(sorted_resources) == 5
+        for res in sorted_resources:
+            print res.id, res.revision_timestamp, res.expired_timestamp, res.state
+        assert sorted_resources[0].state == 'active'
+        assert sorted_resources[1].state == 'active'
+        assert sorted_resources[2].state == 'active'
+        assert sorted_resources[3].state == 'active'
+        assert sorted_resources[4].state == 'active'
+
+        assert str(sorted_resources[0].expired_timestamp) == '9999-12-31 00:00:00'
+        assert str(sorted_resources[1].expired_timestamp) == '9999-12-31 00:00:00'
+        assert str(sorted_resources[2].expired_timestamp) != '9999-12-31 00:00:00'
+        assert str(sorted_resources[3].expired_timestamp) == '9999-12-31 00:00:00'
+        assert str(sorted_resources[4].expired_timestamp) != '9999-12-31 00:00:00'
+
+        tag_revisions = model.Session.query(model.PackageTagRevision).filter_by(package_id=anna1.id).all()
+
+        sorted_tags = sorted(tag_revisions, key=lambda x: (x.revision_timestamp, x.tag.name))[::-1]
+
+        print [(tag.state, tag.tag.name) for tag in sorted_tags]
+
+        assert len(sorted_tags) == 6, len(sorted_tags)
+        assert sorted_tags[0].state == 'active'
+        assert sorted_tags[1].state == 'deleted'
+        assert sorted_tags[2].state == 'deleted'
+        assert sorted_tags[3].state == 'pending'
+        assert sorted_tags[4].state == 'active'
+        assert sorted_tags[5].state == 'active'
+
+        assert str(sorted_tags[0].expired_timestamp) == '9999-12-31 00:00:00'
+        assert str(sorted_tags[1].expired_timestamp) == '9999-12-31 00:00:00'
+        assert str(sorted_tags[2].expired_timestamp) == '9999-12-31 00:00:00'
+        assert str(sorted_tags[3].expired_timestamp) != '9999-12-31 00:00:00'
+        assert str(sorted_tags[4].expired_timestamp) == '9999-12-31 00:00:00'
+
+        extras_revisions = model.Session.query(model.PackageExtraRevision).filter_by(package_id=anna1.id).all()
+
+        sorted_extras = sorted(extras_revisions, 
+                               key=lambda x: (x.revision_timestamp, x.key))[::-1]
+
+        print [(extra.state, extra.key, extra.value) for extra in sorted_extras]
+
+        assert sorted_extras[0].state == 'deleted'
+        assert sorted_extras[1].state == 'active'
+        assert sorted_extras[2].state == 'pending'
+        assert sorted_extras[3].state == 'active'
+        assert sorted_extras[4].state == 'active'
+
+        assert str(sorted_extras[0].expired_timestamp) == '9999-12-31 00:00:00'
+        assert str(sorted_extras[1].expired_timestamp) == '9999-12-31 00:00:00'
+        assert str(sorted_extras[2].expired_timestamp) != '9999-12-31 00:00:00'
+        assert str(sorted_extras[3].expired_timestamp) == '9999-12-31 00:00:00'
+        assert str(sorted_extras[4].expired_timestamp) != '9999-12-31 00:00:00'
+
+
+    def test_12_resource_no_id(self):
 
         context = {"model": model,
                  "session": model.Session}
@@ -337,15 +572,11 @@
 
         res = model.Session.query(model.Resource).filter_by(url=u'test').one()
 
-
         res_dictized = self.remove_changable_columns(resource_dictize(res, context))
 
-        pprint(res_dictized)
-        pprint(new_resource)
-
         assert res_dictized == new_resource, res_dictized 
 
-    def test_12_api_to_dictize(self):
+    def test_13_api_to_dictize(self):
 
         context = {"model": model,
                  "session": model.Session}
@@ -410,9 +641,8 @@
         pkg = model.Session.query(model.Package).filter_by(name=u'testpkg').one()
 
         package_dictized = self.remove_changable_columns(package_dictize(pkg, context))
-        pprint(package_dictized)
 
-    def test_13_group_dictized(self):
+    def test_14_group_dictized(self):
 
         context = {"model": model,
                   "session": model.Session}
@@ -488,9 +718,6 @@
 
 
         assert group_api_to_dict(api_group, context) == {'description': u'Great group!',
-                                                           'name': u'testgroup',
-                                                           'packages': [{'id': u'annakarenina'}, {'id': u'warandpeace'}],
-                                                           'title': u'Some Group Title'}, pformat(group_api1_to_dict(api_group, context))
-
-
-
+                                                         'name': u'testgroup',
+                                                         'packages': [{'id': u'annakarenina'}, {'id': u'warandpeace'}],
+                                                         'title': u'Some Group Title'}, pformat(group_api_to_dict(api_group, context))

Repository URL: https://bitbucket.org/okfn/ckan/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.




More information about the ckan-changes mailing list