Skip to content

Commit

Permalink
Rename metadata model -> metadata schema
Browse files Browse the repository at this point in the history
Part 2/2 of a big rename; "schema" is more appropriate for this type of object.
  • Loading branch information
mark-saeon committed Aug 21, 2018
1 parent 6dc11f0 commit 2c387be
Show file tree
Hide file tree
Showing 24 changed files with 598 additions and 599 deletions.
6 changes: 3 additions & 3 deletions ckanext/metadata/lib/dictization/model_dictize.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ def metadata_record_activity_dictize(activity, context):
return activity_dict


def metadata_model_dictize(metadata_model, context):
return _object_dictize(metadata_model, ckanext_model.MetadataModel, ckanext_model.MetadataModelRevision,
ckanext_model.metadata_model_revision_table, context)
def metadata_schema_dictize(metadata_schema, context):
return _object_dictize(metadata_schema, ckanext_model.MetadataSchema, ckanext_model.MetadataSchemaRevision,
ckanext_model.metadata_schema_revision_table, context)


def metadata_standard_dictize(metadata_standard, context):
Expand Down
6 changes: 3 additions & 3 deletions ckanext/metadata/lib/dictization/model_save.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ def metadata_record_infrastructure_list_save(infrastructure_dicts, context):
session.add(member_obj)


def metadata_model_dict_save(metadata_model_dict, context):
return _object_dict_save(metadata_model_dict, 'metadata_model', ckanext_model.MetadataModel,
ckanext_model.metadata_model_table, context)
def metadata_schema_dict_save(metadata_schema_dict, context):
return _object_dict_save(metadata_schema_dict, 'metadata_schema', ckanext_model.MetadataSchema,
ckanext_model.metadata_schema_table, context)


def metadata_standard_dict_save(metadata_standard_dict, context):
Expand Down
2 changes: 1 addition & 1 deletion ckanext/metadata/logic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
'infrastructure': {'class': ckan_model.Group, 'desc': u'Infrastructure'},
'metadata_collection': {'class': ckan_model.Group, 'desc': u'Metadata Collection'},
'metadata_record': {'class': ckan_model.Package, 'desc': u'Metadata Record'},
'metadata_model': {'class': ckanext_model.MetadataModel, 'desc': u'Metadata Model'},
'metadata_schema': {'class': ckanext_model.MetadataSchema, 'desc': u'Metadata Schema'},
'metadata_standard': {'class': ckanext_model.MetadataStandard, 'desc': u'Metadata Standard'},
'workflow_state': {'class': ckanext_model.WorkflowState, 'desc': u'Workflow State'},
'workflow_transition': {'class': ckanext_model.WorkflowTransition, 'desc': u'Workflow Transition'},
Expand Down
48 changes: 24 additions & 24 deletions ckanext/metadata/logic/action/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,80 +72,80 @@ def metadata_standard_create(context, data_dict):
return output


def metadata_model_create(context, data_dict):
def metadata_schema_create(context, data_dict):
"""
Create a new metadata model.
Create a new metadata schema.
You must be authorized to create metadata models.
You must be authorized to create metadata schemas.
A model must be one and only one of the following:
A metadata schema must be one and only one of the following:
- the default for the given metadata standard (no organization or infrastructure)
- associated with an organization
- associated with an infrastructure
Any metadata records that are now dependent on this model are invalidated.
Any metadata records that are now dependent on this schema are invalidated.
:param id: the id of the metadata model (optional - only sysadmins can set this)
:param id: the id of the metadata schema (optional - only sysadmins can set this)
:type id: string
:param name: the name of the new metadata model (optional - auto-generated if not supplied);
:param name: the name of the new metadata schema (optional - auto-generated if not supplied);
must conform to standard naming rules
:type name: string
:param title: the title of the metadata model (optional)
:param title: the title of the metadata schema (optional)
:type title: string
:param description: the description of the metadata model (optional)
:param description: the description of the metadata schema (optional)
:type description: string
:param metadata_standard_id: the id or name of the metadata standard from which this model is derived
:param metadata_standard_id: the id or name of the metadata standard for which this schema is defined
:type metadata_standard_id: string
:param model_json: the JSON dictionary defining the model (nullable)
:type model_json: string
:param schema_json: the JSON dictionary defining the schema (nullable)
:type schema_json: string
:param organization_id: the id or name of the associated organization (nullable)
:type organization_id: string
:param infrastructure_id: the id or name of the associated infrastructure (nullable)
:type infrastructure_id: string
:returns: the newly created metadata model (unless 'return_id_only' is set to True
in the context, in which case just the metadata model id will be returned)
:returns: the newly created metadata schema (unless 'return_id_only' is set to True
in the context, in which case just the metadata schema id will be returned)
:rtype: dictionary
"""
log.info("Creating metadata model: %r", data_dict)
tk.check_access('metadata_model_create', context, data_dict)
log.info("Creating metadata schema: %r", data_dict)
tk.check_access('metadata_schema_create', context, data_dict)

model = context['model']
user = context['user']
session = context['session']
defer_commit = context.get('defer_commit', False)
return_id_only = context.get('return_id_only', False)

data, errors = tk.navl_validate(data_dict, schema.metadata_model_create_schema(), context)
data, errors = tk.navl_validate(data_dict, schema.metadata_schema_create_schema(), context)
if errors:
session.rollback()
raise tk.ValidationError(errors)

metadata_model = model_save.metadata_model_dict_save(data, context)
metadata_schema = model_save.metadata_schema_dict_save(data, context)

# creating the revision also flushes the session which gives us the new object id
rev = model.repo.new_revision()
rev.author = user
if 'message' in context:
rev.message = context['message']
else:
rev.message = _(u'REST API: Create metadata model %s') % metadata_model.id
rev.message = _(u'REST API: Create metadata schema %s') % metadata_schema.id

dependent_record_list = tk.get_action('metadata_model_dependent_record_list')(context, {'id': metadata_model.id})
dependent_record_list = tk.get_action('metadata_schema_dependent_record_list')(context, {'id': metadata_schema.id})
invalidate_context = context.copy()
invalidate_context.update({
'defer_commit': True,
'trigger_action': 'metadata_model_create',
'trigger_object_id': metadata_model.id,
'trigger_action': 'metadata_schema_create',
'trigger_object_id': metadata_schema.id,
})
for metadata_record_id in dependent_record_list:
tk.get_action('metadata_record_invalidate')(invalidate_context, {'id': metadata_record_id})

if not defer_commit:
model.repo.commit()

output = metadata_model.id if return_id_only \
else tk.get_action('metadata_model_show')(context, {'id': metadata_model.id})
output = metadata_schema.id if return_id_only \
else tk.get_action('metadata_schema_show')(context, {'id': metadata_schema.id})
return output


Expand Down
70 changes: 35 additions & 35 deletions ckanext/metadata/logic/action/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ def metadata_standard_delete(context, data_dict):
child_standard_dict['parent_standard_id'] = ''
tk.get_action('metadata_standard_update')(cascade_context, child_standard_dict)

# cascade delete to dependent metadata models
metadata_model_ids = session.query(ckanext_model.MetadataModel.id) \
.filter(ckanext_model.MetadataModel.metadata_standard_id == metadata_standard_id) \
.filter(ckanext_model.MetadataModel.state != 'deleted') \
# cascade delete to dependent metadata schemas
metadata_schema_ids = session.query(ckanext_model.MetadataSchema.id) \
.filter(ckanext_model.MetadataSchema.metadata_standard_id == metadata_standard_id) \
.filter(ckanext_model.MetadataSchema.state != 'deleted') \
.all()
for (metadata_model_id,) in metadata_model_ids:
tk.get_action('metadata_model_delete')(cascade_context, {'id': metadata_model_id})
for (metadata_schema_id,) in metadata_schema_ids:
tk.get_action('metadata_schema_delete')(cascade_context, {'id': metadata_schema_id})

rev = model.repo.new_revision()
rev.author = user
Expand All @@ -80,47 +80,47 @@ def metadata_standard_delete(context, data_dict):
model.repo.commit()


def metadata_model_delete(context, data_dict):
def metadata_schema_delete(context, data_dict):
"""
Delete a metadata model.
Delete a metadata schema.
You must be authorized to delete the metadata model.
You must be authorized to delete the metadata schema.
Any metadata records that were dependent on this model are invalidated.
Any metadata records that were dependent on this schema are invalidated.
:param id: the id or name of the metadata model to delete
:param id: the id or name of the metadata schema to delete
:type id: string
"""
log.info("Deleting metadata model: %r", data_dict)
log.info("Deleting metadata schema: %r", data_dict)

model = context['model']
user = context['user']
defer_commit = context.get('defer_commit', False)

metadata_model_id = tk.get_or_bust(data_dict, 'id')
metadata_model = ckanext_model.MetadataModel.get(metadata_model_id)
if metadata_model is not None:
metadata_model_id = metadata_model.id
metadata_schema_id = tk.get_or_bust(data_dict, 'id')
metadata_schema = ckanext_model.MetadataSchema.get(metadata_schema_id)
if metadata_schema is not None:
metadata_schema_id = metadata_schema.id
else:
raise tk.ObjectNotFound('%s: %s' % (_('Not found'), _('Metadata Model')))
raise tk.ObjectNotFound('%s: %s' % (_('Not found'), _('Metadata Schema')))

tk.check_access('metadata_model_delete', context, data_dict)
tk.check_access('metadata_schema_delete', context, data_dict)

rev = model.repo.new_revision()
rev.author = user
rev.message = _(u'REST API: Delete metadata model %s') % metadata_model_id
rev.message = _(u'REST API: Delete metadata schema %s') % metadata_schema_id

dependent_record_list = tk.get_action('metadata_model_dependent_record_list')(context, {'id': metadata_model_id})
dependent_record_list = tk.get_action('metadata_schema_dependent_record_list')(context, {'id': metadata_schema_id})
invalidate_context = context.copy()
invalidate_context.update({
'defer_commit': True,
'trigger_action': 'metadata_model_delete',
'trigger_object_id': metadata_model_id,
'trigger_action': 'metadata_schema_delete',
'trigger_object_id': metadata_schema_id,
})
for metadata_record_id in dependent_record_list:
tk.get_action('metadata_record_invalidate')(invalidate_context, {'id': metadata_record_id})

metadata_model.delete()
metadata_schema.delete()
if not defer_commit:
model.repo.commit()

Expand Down Expand Up @@ -159,19 +159,19 @@ def infrastructure_delete(context, data_dict):
.count() > 0:
raise tk.ValidationError(_('Infrastructure has dependent metadata records'))

# cascade delete to dependent metadata models
# cascade delete to dependent metadata schemas
cascade_context = {
'model': model,
'user': user,
'session': session,
'defer_commit': True,
}
metadata_model_ids = session.query(ckanext_model.MetadataModel.id) \
.filter(ckanext_model.MetadataModel.infrastructure_id == infrastructure_id) \
.filter(ckanext_model.MetadataModel.state != 'deleted') \
metadata_schema_ids = session.query(ckanext_model.MetadataSchema.id) \
.filter(ckanext_model.MetadataSchema.infrastructure_id == infrastructure_id) \
.filter(ckanext_model.MetadataSchema.state != 'deleted') \
.all()
for (metadata_model_id,) in metadata_model_ids:
tk.get_action('metadata_model_delete')(cascade_context, {'id': metadata_model_id})
for (metadata_schema_id,) in metadata_schema_ids:
tk.get_action('metadata_schema_delete')(cascade_context, {'id': metadata_schema_id})

data_dict['type'] = 'infrastructure'
context['invoked_api'] = 'infrastructure_delete'
Expand Down Expand Up @@ -400,13 +400,13 @@ def organization_delete(context, data_dict):
for (metadata_collection_id,) in metadata_collection_ids:
tk.get_action('metadata_collection_delete')(cascade_context, {'id': metadata_collection_id})

# cascade delete to dependent metadata models
metadata_model_ids = session.query(ckanext_model.MetadataModel.id) \
.filter(ckanext_model.MetadataModel.organization_id == organization_id) \
.filter(ckanext_model.MetadataModel.state != 'deleted') \
# cascade delete to dependent metadata schemas
metadata_schema_ids = session.query(ckanext_model.MetadataSchema.id) \
.filter(ckanext_model.MetadataSchema.organization_id == organization_id) \
.filter(ckanext_model.MetadataSchema.state != 'deleted') \
.all()
for (metadata_model_id,) in metadata_model_ids:
tk.get_action('metadata_model_delete')(cascade_context, {'id': metadata_model_id})
for (metadata_schema_id,) in metadata_schema_ids:
tk.get_action('metadata_schema_delete')(cascade_context, {'id': metadata_schema_id})

# delete membership relations
for member in session.query(model.Member) \
Expand Down
Loading

0 comments on commit 2c387be

Please sign in to comment.