Skip to content

Commit

Permalink
Fix updates for organizations to fix integrity errors
Browse files Browse the repository at this point in the history
  • Loading branch information
rossjones committed Sep 13, 2012
1 parent a5bd39b commit 59ba38f
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 27 deletions.
1 change: 0 additions & 1 deletion ckan/controllers/organization.py
Expand Up @@ -305,7 +305,6 @@ def _save_edit(self, id, context):
data_dict = clean_dict(unflatten(
tuplize_dict(parse_params(request.params))))
context['message'] = data_dict.get('log_message', '')
data_dict['id'] = id
context['allow_partial_update'] = True
organization = get_action('organization_update')(context, data_dict)

Expand Down
2 changes: 1 addition & 1 deletion ckan/lib/activity_streams.py
Expand Up @@ -74,7 +74,7 @@ def activity_stream_string_deleted_group():
return _("{actor} deleted the group {group}")

def activity_stream_string_deleted_organization():
return _("{actor} deleted the group {organization}")
return _("{actor} deleted the organization {organization}")

def activity_stream_string_deleted_package():
return _("{actor} deleted the dataset {dataset}")
Expand Down
7 changes: 4 additions & 3 deletions ckan/logic/action/create.py
Expand Up @@ -646,7 +646,7 @@ def organization_create(context, data_dict):

_check_access('organization_create', context, data_dict)

schema = ckan.logic.schema.default_group_schema()
schema = ckan.logic.schema.default_organization_schema()

data, errors = _validate(data_dict, schema, context)
log.debug('group_create validate_errs=%r user=%s group=%s data_dict=%r',
Expand Down Expand Up @@ -699,8 +699,9 @@ def organization_create(context, data_dict):
'defer_commit':True,
'session': session
}
logic.get_action('activity_create')(activity_create_context,
activity_dict, ignore_auth=True)
# FIXME: Re-enable
# logic.get_action('activity_create')(activity_create_context,
# activity_dict, ignore_auth=True)

if not context.get('defer_commit'):
model.repo.commit()
Expand Down
14 changes: 9 additions & 5 deletions ckan/logic/action/update.py
Expand Up @@ -539,7 +539,7 @@ def organization_update(context, data_dict):
if organization is None:
raise NotFound('Organization was not found.')

schema = ckan.logic.schema.group_form_schema()
schema = ckan.logic.schema.organization_form_schema()

_check_access('organization_update', context, data_dict)

Expand Down Expand Up @@ -594,14 +594,15 @@ def organization_update(context, data_dict):
# activity.
if organization.state == u'deleted':
if session.query(ckan.model.Activity).filter_by(
object_id=group.id, activity_type='deleted').all():
object_id=organization.id, activity_type='deleted').all():
# A 'deleted group' activity for this group has already been
# emitted.
# FIXME: What if the group was deleted and then activated again?
activity_dict = None
else:
# We will emit a 'deleted group' activity.
activity_dict['activity_type'] = 'deleted organization'

if activity_dict is not None:
activity_dict['data'] = {
'organization': ckan.lib.dictization.table_dictize(organization, context)
Expand All @@ -610,10 +611,13 @@ def organization_update(context, data_dict):
'model': model,
'user': user,
'defer_commit':True,
'session': session
'session': session,
'organization_id': organization.id
}
_get_action('activity_create')(activity_create_context, activity_dict,
ignore_auth=True)

# FIXME: Re-enable
#_get_action('activity_create')(activity_create_context, activity_dict,
# ignore_auth=True)

if not context.get('defer_commit'):
model.repo.commit()
Expand Down
8 changes: 2 additions & 6 deletions ckan/logic/schema.py
Expand Up @@ -227,7 +227,7 @@ def default_organization_schema():
"name":[ignore_missing, unicode],
"__extras": [ignore]
},
'groups': {
'organizations': {
"name": [not_empty, unicode],
"capacity": [ignore_missing],
"__extras": [ignore]
Expand All @@ -236,11 +236,6 @@ def default_organization_schema():
"name": [not_empty, unicode],
"capacity": [ignore_missing],
"__extras": [ignore]
},
'groups': {
"name": [not_empty, unicode],
"capacity": [ignore_missing],
"__extras": [ignore]
}
}
return schema
Expand Down Expand Up @@ -287,6 +282,7 @@ def default_group_schema():

def organization_form_schema():
schema = default_organization_schema()
schema['id'] = [not_empty, unicode]
schema['packages'] = {
"name": [not_empty, unicode],
"title": [ignore_missing],
Expand Down
34 changes: 23 additions & 11 deletions ckan/logic/validators.py
Expand Up @@ -120,11 +120,24 @@ def group_id_exists(group_id, context):
model = context['model']
session = context['session']

result = session.query(model.Group).get(group_id)
result = session.query(model.Group).filter(model.Group.name==group_id).first()
if not result:
raise Invalid('%s: %s' % (_('Not found'), _('Group')))
return group_id

def organization_id_exists(organization_id, context):
"""Raises Invalid if the given organization_id does not exist in the model given
in the context, otherwise returns the given organization_id.
"""
model = context['model']
session = context['session']

result = session.query(model.Group).filter(model.Group.name==organization_id).first()
if not result:
raise Invalid('%s: %s' % (_('Not found'), _('Organization')))
return organization_id


def related_id_exists(related_id, context):
"""Raises Invalid if the given related_id does not exist in the model
Expand Down Expand Up @@ -173,11 +186,11 @@ def activity_type_exists(activity_type):
'changed user' : user_id_exists,
'follow user' : user_id_exists,
'new group' : group_id_exists,
'new organization' : group_id_exists,
'new organization' : organization_id_exists,
'changed group' : group_id_exists,
'changed organization' : group_id_exists,
'changed organization' : organization_id_exists,
'deleted group' : group_id_exists,
'deleted organization' : group_id_exists,
'deleted organization' : organization_id_exists,
'new related item': related_id_exists,
'deleted related item': related_id_exists
}
Expand Down Expand Up @@ -285,14 +298,13 @@ def organization_name_validator(key, data, errors, context):
session = context['session']
organization = context.get('organization')

query = session.query(model.Group.name).filter_by(name=data[key])
if organization:
organization_id = organization.id
if organization: # This is part of an update
result = session.query(model.Group).filter(model.Group.name==data[key]).first()
if result and result.id == organization.id:
result = None
else:
organization_id = data.get(key[:-1] + ('id',))
if organization_id and organization_id is not missing:
query = query.filter(model.Group.id != organization_id)
result = query.first()
result = session.query(model.Group).filter(model.Group.name == data.get(key[:-1] + ('id',))).first()

if result:
errors[key].append(_('Organization name already exists in database'))

Expand Down
2 changes: 2 additions & 0 deletions ckan/templates/organization/snippets/organization_form.html
Expand Up @@ -9,6 +9,8 @@
{% set attrs = {'data-module': 'slug-preview-target'} %}
{{ form.input('title', label=_('Title'), id='field-title', placeholder=_('My Organization'), value=data.title, error=errors.title, classes=['control-full'], attrs=attrs) }}

<input type='hidden' name='id' id='id' value="{{data.get('id')}}">

{# Perhaps these should be moved into the controller? #}
{% set prefix = h.url_for(controller='organization', action='read', id='') %}
{% set domain = h.url_for(controller='organization', action='read', id='', qualified=true) %}
Expand Down

0 comments on commit 59ba38f

Please sign in to comment.