Skip to content

Commit

Permalink
[#2006] Deleted more parts related to the current column and pending …
Browse files Browse the repository at this point in the history
…state.
  • Loading branch information
David Read committed Nov 4, 2014
1 parent bd0068e commit 0733c0d
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 159 deletions.
3 changes: 0 additions & 3 deletions ckan/controllers/package.py
Expand Up @@ -958,9 +958,6 @@ def _save_edit(self, name_or_id, context, package_type=None):
context['message'] = data_dict.get('log_message', '')
data_dict['id'] = name_or_id
pkg = get_action('package_update')(context, data_dict)
if request.params.get('save', '') == 'Approve':
get_action('make_latest_pending_package_active')(
context, data_dict)
c.pkg = context['package']
c.pkg_dict = pkg

Expand Down
19 changes: 5 additions & 14 deletions ckan/lib/dictization/model_dictize.py
Expand Up @@ -150,14 +150,9 @@ def _execute(q, table, context):

def _execute_with_revision(q, rev_table, context):
'''
Takes an SqlAlchemy query (q) that is (at its base) a Select on an
object revision table (rev_table), and normally it filters to the
'current' object revision (latest which has been moderated) and
returns that.
But you can provide revision_id or revision_date in the
context and it will filter to an earlier time or the latest unmoderated
object revision.
Takes an SqlAlchemy query (q) that is (at its base) a Select on an object
revision table (rev_table), and you provide revision_id or revision_date in
the context and it will filter the object revision(s) to an earlier time.
Raises NotFound if context['revision_id'] is provided, but the revision
ID does not exist.
Expand All @@ -177,12 +172,8 @@ def _execute_with_revision(q, rev_table, context):
raise logic.NotFound
revision_date = revision.timestamp

if revision_date:
q = q.where(rev_table.c.revision_timestamp <= revision_date)
q = q.where(rev_table.c.expired_timestamp > revision_date)
else:
# The most recent timestamp (caller will do .first())
q = q.order_by(rev_table.c.revision_timestamp.desc())
q = q.where(rev_table.c.revision_timestamp <= revision_date)
q = q.where(rev_table.c.expired_timestamp > revision_date)

return session.execute(q)

Expand Down
82 changes: 0 additions & 82 deletions ckan/logic/action/update.py
Expand Up @@ -5,7 +5,6 @@
import json

from pylons import config
from vdm.sqlalchemy.base import SQLAlchemySession
import paste.deploy.converters as converters

import ckan.plugins as plugins
Expand Down Expand Up @@ -36,86 +35,6 @@
ValidationError = logic.ValidationError
_get_or_bust = logic.get_or_bust

def _make_latest_rev_active(context, q):
session = context['model'].Session

old_current = q.filter_by(current=True).first()
if old_current:
old_current.current = False
session.add(old_current)

latest_rev = q.filter_by(expired_timestamp=datetime.datetime(9999, 12, 31)).one()
latest_rev.current = True
if latest_rev.state in ('deleted'):
latest_rev.state = 'deleted'
latest_rev.continuity.state = 'deleted'
else:
latest_rev.continuity.state = 'active'
latest_rev.state = 'active'

session.add(latest_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
else:
context['latest_revision_date'] = latest_rev.revision_timestamp
context['latest_revision'] = latest_rev.revision_id

def make_latest_pending_package_active(context, data_dict):
'''
.. todo:: What does this function do?
You must be authorized to update the dataset.
:param id: the name or id of the dataset, e.g. ``'warandpeace'``
:type id: string
'''
model = context['model']
session = model.Session
SQLAlchemySession.setattr(session, 'revisioning_disabled', True)
id = _get_or_bust(data_dict, "id")
pkg = model.Package.get(id)

_check_access('make_latest_pending_package_active', context, data_dict)

#packages
q = session.query(model.PackageRevision).filter_by(id=pkg.id)
_make_latest_rev_active(context, q)

#resources
for resource in pkg.resource_groups_all[0].resources_all:
q = session.query(model.ResourceRevision).filter_by(id=resource.id)
_make_latest_rev_active(context, q)

#tags
for tag in pkg.package_tag_all:
q = session.query(model.PackageTagRevision).filter_by(id=tag.id)
_make_latest_rev_active(context, q)

#extras
for extra in pkg.extras_list:
q = session.query(model.PackageExtraRevision).filter_by(id=extra.id)
_make_latest_rev_active(context, q)

latest_revision = context.get('latest_revision')
if not latest_revision:
return

q = session.query(model.Revision).filter_by(id=latest_revision)
revision = q.first()
revision.approved_timestamp = datetime.datetime.now()
session.add(revision)

if not context.get('defer_commit'):
session.commit()


def related_update(context, data_dict):
'''Update a related item.
Expand Down Expand Up @@ -183,7 +102,6 @@ def related_update(context, data_dict):
return model_dictize.related_dictize(related, context)



def resource_update(context, data_dict):
'''Update a resource.
Expand Down
3 changes: 0 additions & 3 deletions ckan/logic/auth/update.py
Expand Up @@ -7,9 +7,6 @@
from ckan.logic.auth.create import _check_group_auth


def make_latest_pending_package_active(context, data_dict):
return new_authz.is_authorized('package_update', context, data_dict)

@logic.auth_allow_anonymous_access
def package_update(context, data_dict):
user = context.get('user')
Expand Down
1 change: 0 additions & 1 deletion ckan/model/__init__.py
Expand Up @@ -396,7 +396,6 @@ def purge_revision(self, revision, leave_record=False):
if num == 0:
continue

obj.current = True
obj.expired_timestamp = datetime(9999, 12, 31)
self.session.add(obj)
break
Expand Down
2 changes: 2 additions & 0 deletions ckan/model/core.py
Expand Up @@ -42,4 +42,6 @@ def make_revisioned_table(table):
revision_table.append_column(Column('expired_timestamp', DateTime,
default=datetime.datetime(9999, 12, 31)))
revision_table.append_column(Column('current', Boolean))
# NB columns 'current' and 'expired_id' are deprecated and not used
# TODO remove them at a later version
return revision_table
7 changes: 3 additions & 4 deletions ckan/model/resource.py
Expand Up @@ -124,10 +124,9 @@ def as_dict(self, core_columns_only=False):

def get_package_id(self):
'''Returns the package id for a resource. '''
query = meta.Session.query(ResourceGroupRevision) \
.filter(and_(ResourceGroupRevision.id == self.resource_group_id,
ResourceGroupRevision.state == u'active',
ResourceGroupRevision.current == True))
query = meta.Session.query(ResourceGroup) \
.filter(and_(ResourceGroup.id == self.resource_group_id,
ResourceGroup.state == u'active'))
resource_group = query.first()
if resource_group is None:
return None
Expand Down
14 changes: 5 additions & 9 deletions ckan/model/tag.py
Expand Up @@ -193,10 +193,8 @@ def all(cls, vocab_id_or_name=None):
query = meta.Session.query(Tag).filter(Tag.vocabulary_id==vocab.id)
else:
query = meta.Session.query(Tag).filter(Tag.vocabulary_id == None)
query = query.distinct().join(_package.PackageTagRevision)
query = query.filter(and_(
_package.PackageTagRevision.state == 'active',
_package.PackageTagRevision.current == True))
query = query.distinct().join(PackageTag)
query = query.filter_by(state='active')
return query

@property
Expand All @@ -207,11 +205,9 @@ def packages(self):
'''
q = meta.Session.query(_package.Package)
q = q.join(_package.PackageTagRevision)
q = q.filter(_package.PackageTagRevision.tag_id == self.id)
q = q.filter(and_(
_package.PackageTagRevision.state == 'active',
_package.PackageTagRevision.current == True))
q = q.join(PackageTag)
q = q.filter_by(tag_id=self.id)
q = q.filter_by(state='active')
q = q.order_by(_package.Package.name)
packages = q.all()
return packages
Expand Down
50 changes: 7 additions & 43 deletions ckan/tests/lib/test_dictization.py
Expand Up @@ -23,7 +23,6 @@
group_api_to_dict,
package_tag_list_save,
)
from ckan.logic.action.update import make_latest_pending_package_active
import ckan.logic.action.get


Expand Down Expand Up @@ -325,10 +324,10 @@ def test_09_package_alter(self):

resources_revisions = model.Session.query(model.ResourceRevision).filter_by(resource_group_id=anna1.resource_groups_all[0].id).all()

sorted_resources = sorted(resources_revisions, key=lambda x: (x.revision_timestamp, x.url))[::-1]
for res in sorted_resources:
print res.id, res.revision_timestamp, res.expired_timestamp, res.state, res.current
assert len(sorted_resources) == 3
sorted_resource_revisions = sorted(resources_revisions, key=lambda x: (x.revision_timestamp, x.url))[::-1]
for res in sorted_resource_revisions:
print res.id, res.revision_timestamp, res.state
assert len(sorted_resource_revisions) == 3

anna_original = pformat(anna_dictized)
anna_after_save = pformat(package_dictized)
Expand All @@ -339,18 +338,9 @@ def test_09_package_alter(self):
assert self.remove_changable_columns(anna_dictized) == self.remove_changable_columns(package_dictized)
assert "\n".join(unified_diff(anna_original.split("\n"), anna_after_save.split("\n")))

def test_10_package_alter_pending(self):
"""
This test still exists as following tests depend on data it creates
"""
context = {'model': model,
'session': model.Session,
"user": 'testsysadmin'}

# changes to the package, relied upon by later tests
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'http://new_url2'
anna_dictized['tags'][0]['name'] = u'new_tag'
Expand All @@ -362,20 +352,8 @@ def test_10_package_alter_pending(self):
model.Session.commit()
model.Session.remove()


def test_11_add_pending(self):
"""
This test still exists as following tests depend on data it creates
"""

context = {'model': model,
'session': model.Session,
"user": 'testsysadmin'}

anna1 = model.Session.query(model.Package).filter_by(name='annakarenina_changed2').one()
anna_dictized = package_dictize(anna1, context)


anna_dictized['notes'] = 'wee'
anna_dictized['resources'].append({
'format': u'plain text',
Expand All @@ -390,20 +368,6 @@ def test_11_add_pending(self):
model.Session.commit()
model.Session.remove()

def test_12_make_active(self):
"""
This test still exists as following tests depend on data it creates
"""

model.repo.new_revision()
anna1 = model.Session.query(model.Package).filter_by(name='annakarenina_changed2').one()
context = {"model": model,
"session": model.Session,
'user': 'testsysadmin'}

make_latest_pending_package_active(context, {'id': anna1.id})



def test_13_get_package_in_past(self):

Expand All @@ -420,7 +384,7 @@ def test_13_get_package_in_past(self):
first_dictized = self.remove_changable_columns(package_dictize(anna1, context))
assert self.package_expected == first_dictized

context['revision_id'] = sorted_packages[1].revision_id #original state
context['revision_id'] = sorted_packages[1].revision_id

second_dictized = self.remove_changable_columns(package_dictize(anna1, context))

Expand All @@ -429,7 +393,7 @@ def test_13_get_package_in_past(self):

assert second_dictized == first_dictized

context['revision_id'] = sorted_packages[2].revision_id #original state
context['revision_id'] = sorted_packages[2].revision_id
third_dictized = self.remove_changable_columns(package_dictize(anna1, context))

second_dictized['name'] = u'annakarenina_changed2'
Expand Down

0 comments on commit 0733c0d

Please sign in to comment.