Skip to content

Commit

Permalink
Removing more use of current flag
Browse files Browse the repository at this point in the history
  • Loading branch information
rossjones committed Oct 28, 2014
1 parent 0413e25 commit 699d226
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 265 deletions.
3 changes: 0 additions & 3 deletions ckan/controllers/package.py
Expand Up @@ -732,7 +732,6 @@ def edit(self, id, data=None, errors=None, error_summary=None):
context = {'model': model, 'session': model.Session,
'user': c.user or c.author, 'auth_user_obj': c.userobj,
'save': 'save' in request.params,
'moderated': config.get('moderated'),
'pending': True}

if context['save'] and not data:
Expand Down Expand Up @@ -958,8 +957,6 @@ def _save_edit(self, name_or_id, context, package_type=None):
del data_dict['_ckan_phase']
del data_dict['save']
context['message'] = data_dict.get('log_message', '')
if not context['moderated']:
context['pending'] = False
data_dict['id'] = name_or_id
pkg = get_action('package_update')(context, data_dict)
if request.params.get('save', '') == 'Approve':
Expand Down
11 changes: 2 additions & 9 deletions ckan/lib/dictization/__init__.py
Expand Up @@ -53,7 +53,7 @@ def table_dictize(obj, context, **kw):
result_dict.update(kw)

##HACK For optimisation to get metadata_modified created faster.

context['metadata_modified'] = max(result_dict.get('revision_timestamp', ''),
context.get('metadata_modified', ''))

Expand All @@ -72,7 +72,7 @@ def obj_list_dictize(obj_list, context, sort_key=lambda x:x):
dictized = table_dictize(obj, context, capacity=capacity)
else:
dictized = table_dictize(obj, context)
if active and obj.state not in ('active', 'pending'):
if active and obj.state not in ('active'):
continue
result_list.append(dictized)

Expand Down Expand Up @@ -138,13 +138,6 @@ def table_dict_save(table_dict, ModelClass, context):
continue
setattr(obj, key, value)

if context.get('pending'):
if session.is_modified(obj, include_collections=False, passive=True):
if table_dict.get('state', '') == 'deleted':
obj.state = 'pending-deleted'
else:
obj.state = 'pending'

session.add(obj)

return obj
6 changes: 2 additions & 4 deletions ckan/lib/dictization/model_dictize.py
Expand Up @@ -97,7 +97,7 @@ def extras_list_dictize(extras_list, context):
active = context.get('active', True)
for extra in extras_list:
dictized = d.table_dictize(extra, context)
if active and extra.state not in ('active', 'pending'):
if active and extra.state not in ('active'):
continue
value = dictized["value"]
result_list.append(dictized)
Expand Down Expand Up @@ -141,7 +141,7 @@ def _execute_with_revision(q, rev_table, context):
'current' object revision (latest which has been moderated) and
returns that.
But you can provide revision_id, revision_date or pending in the
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.
Expand All @@ -168,8 +168,6 @@ def _execute_with_revision(q, rev_table, context):
if revision_date:
q = q.where(rev_table.c.revision_timestamp <= revision_date)
q = q.where(rev_table.c.expired_timestamp > revision_date)
elif pending:
q = q.where(rev_table.c.expired_timestamp == datetime.datetime(9999, 12, 31))
else:
# TODO: Use the most recent timestamp.
q = q.where(rev_table.c.current == True)
Expand Down
39 changes: 10 additions & 29 deletions ckan/lib/dictization/model_save.py
Expand Up @@ -49,11 +49,7 @@ def resource_dict_save(res_dict, context):
for delete_me in extras_to_delete:
del obj.extras[delete_me]

if context.get('pending'):
if session.is_modified(obj, include_collections=False, passive=True):
obj.state = u'pending'
else:
obj.state = u'active'
obj.state = u'active'

session.add(obj)
return obj
Expand All @@ -63,8 +59,6 @@ def package_resource_list_save(res_dicts, package, context):
if res_dicts is None and allow_partial_update:
return

pending = context.get('pending')

resource_list = package.resource_groups_all[0].resources_all
old_list = package.resource_groups_all[0].resources_all[:]

Expand All @@ -76,10 +70,7 @@ def package_resource_list_save(res_dicts, package, context):
resource_list[:] = obj_list

for resource in set(old_list) - set(obj_list):
if pending and resource.state != 'deleted':
resource.state = 'pending-deleted'
else:
resource.state = 'deleted'
resource.state = 'deleted'
resource_list.append(resource)


Expand All @@ -105,17 +96,16 @@ def package_extras_save(extra_dicts, obj, context):
new_extras[extra_dict["key"]] = extra_dict["value"]
#new
for key in set(new_extras.keys()) - set(old_extras.keys()):
state = 'pending' if context.get('pending') else 'active'
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]
#dont change state to pending if nothing has changed
if new_extras[key] == extra.value and extra.state != 'deleted':
continue
state = 'pending' if context.get('pending') else 'active'
state = 'active'
extra.value = new_extras[key]
extra.state = state
session.add(extra)
Expand All @@ -124,7 +114,7 @@ def package_extras_save(extra_dicts, obj, context):
extra = old_extras[key]
if extra.state == 'deleted':
continue
state = 'pending-deleted' if context.get('pending') else 'deleted'
state = 'deleted'
extra.state = state

def group_extras_save(extras_dicts, context):
Expand All @@ -147,15 +137,14 @@ def package_tag_list_save(tag_dicts, package, context):

model = context["model"]
session = context["session"]
pending = context.get('pending')

tag_package_tag = dict((package_tag.tag, package_tag)
for package_tag in
package.package_tag_all)

tag_package_tag_inactive = dict(
[ (tag,pt) for tag,pt in tag_package_tag.items() if
pt.state in ['deleted', 'pending-deleted'] ]
pt.state in ['deleted'] ]
)

tag_name_vocab = set()
Expand All @@ -170,21 +159,18 @@ def package_tag_list_save(tag_dicts, package, context):
# case 1: currently active but not in new list
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'
package_tag.state = 'deleted'

# case 2: in new list but never used before
for tag in tags - set(tag_package_tag.keys()):
state = 'pending' if pending else 'active'
state = 'active'
package_tag_obj = model.PackageTag(package, tag, state)
session.add(package_tag_obj)
tag_package_tag[tag] = package_tag_obj

# case 3: in new list and already used but in deleted state
for tag in tags.intersection(set(tag_package_tag_inactive.keys())):
state = 'pending' if pending else 'active'
state = 'active'
package_tag = tag_package_tag[tag]
package_tag.state = state

Expand All @@ -199,7 +185,6 @@ def package_membership_list_save(group_dicts, package, context):
capacity = 'public'
model = context["model"]
session = context["session"]
pending = context.get('pending')
user = context.get('user')

members = session.query(model.Member) \
Expand Down Expand Up @@ -266,7 +251,6 @@ def relationship_list_save(relationship_dicts, package, attr, context):

model = context["model"]
session = context["session"]
pending = context.get('pending')

relationship_list = getattr(package, attr)
old_list = relationship_list[:]
Expand All @@ -280,10 +264,7 @@ def relationship_list_save(relationship_dicts, package, attr, context):
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.state = 'deleted'
relationship_list.append(relationship)

def package_dict_save(pkg_dict, context):
Expand Down
4 changes: 0 additions & 4 deletions ckan/logic/action/update.py
Expand Up @@ -37,10 +37,6 @@
_get_or_bust = logic.get_or_bust

def _make_latest_rev_active(context, q):
"""
This still happens, but we don't really care that it happens as we are
ignoring the current flag.
"""
session = context['model'].Session

old_current = q.filter_by(current=True).first()
Expand Down
11 changes: 6 additions & 5 deletions ckan/model/package.py
Expand Up @@ -22,7 +22,8 @@

__all__ = ['Package', 'package_table', 'package_revision_table',
'PACKAGE_NAME_MAX_LENGTH', 'PACKAGE_NAME_MIN_LENGTH',
'PACKAGE_VERSION_MAX_LENGTH', 'PackageTagRevision', 'PackageRevision']
'PACKAGE_VERSION_MAX_LENGTH', 'PackageTag', 'PackageTagRevision',
'PackageRevision']

PACKAGE_NAME_MAX_LENGTH = 100
PACKAGE_NAME_MIN_LENGTH = 2
Expand Down Expand Up @@ -156,10 +157,10 @@ def get_tags(self, vocab=None):
"""
import ckan.model as model
query = meta.Session.query(model.Tag)
query = query.join(PackageTag)
query = query.filter(PackageTag.tag_id == model.Tag.id)
query = query.filter(PackageTag.package_id == self.id)
query = query.filter(PackageTag.state == 'active')
query = query.join(model.PackageTag)
query = query.filter(model.PackageTag.tag_id == model.Tag.id)
query = query.filter(model.PackageTag.package_id == self.id)
query = query.filter(model.PackageTag.state == 'active')
if vocab:
query = query.filter(model.Tag.vocabulary_id == vocab.id)
else:
Expand Down

0 comments on commit 699d226

Please sign in to comment.