Skip to content

Commit

Permalink
Rename metadata schema -> metadata standard
Browse files Browse the repository at this point in the history
Part 1/2 of a big rename; "standard" is more appropriate for this type of object. Also removed the XSD column since we're doing everything in CKAN using JSON.
  • Loading branch information
mark-saeon committed Aug 21, 2018
1 parent 64f62ce commit 6dc11f0
Show file tree
Hide file tree
Showing 24 changed files with 716 additions and 743 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 @@ -78,9 +78,9 @@ def metadata_model_dictize(metadata_model, context):
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):
return _object_dictize(metadata_standard, ckanext_model.MetadataStandard, ckanext_model.MetadataStandardRevision,
ckanext_model.metadata_standard_revision_table, context)


def workflow_state_dictize(workflow_state, 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 @@ -72,9 +72,9 @@ def metadata_model_dict_save(metadata_model_dict, context):
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):
return _object_dict_save(metadata_standard_dict, 'metadata_standard', ckanext_model.MetadataStandard,
ckanext_model.metadata_standard_table, context)


def workflow_state_dict_save(workflow_state_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 @@ -12,7 +12,7 @@
'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'},
}
58 changes: 28 additions & 30 deletions ckanext/metadata/logic/action/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,62 +15,60 @@
# nullable params must be supplied but may be empty
# all other params must be supplied and must not be empty

def metadata_schema_create(context, data_dict):
def metadata_standard_create(context, data_dict):
"""
Create a new metadata schema.
Create a new metadata standard.
You must be authorized to create metadata schemas.
You must be authorized to create metadata standards.
:param id: the id of the metadata schema (optional - only sysadmins can set this)
:param id: the id of the metadata standard (optional - only sysadmins can set this)
:type id: string
:param name: the name of the new metadata schema (optional - auto-generated if not supplied);
:param name: the name of the new metadata standard (optional - auto-generated if not supplied);
must conform to standard naming rules
:type name: string
:param title: the title of the metadata schema (optional)
:param title: the title of the metadata standard (optional)
:type title: string
:param description: the description of the metadata schema (optional)
:param description: the description of the metadata standard (optional)
:type description: string
:param schema_name: the name of the metadata schema
:type schema_name: string
:param schema_version: the version of the metadata schema (nullable)
:type schema_version: string
:param schema_xsd: the XSD document defining the schema (nullable)
:type schema_xsd: string
:param base_schema_id: the id or name of the metadata schema from which this schema is derived (nullable)
:type base_schema_id: string
: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)
:param standard_name: the name of the metadata standard
:type standard_name: string
:param standard_version: the version of the metadata standard (nullable)
:type standard_version: string
:param parent_standard_id: the id or name of the metadata standard from which this standard is derived (nullable)
:type parent_standard_id: string
:returns: the newly created metadata standard (unless 'return_id_only' is set to True
in the context, in which case just the metadata standard id will be returned)
:rtype: dictionary
"""
log.info("Creating metadata schema: %r", data_dict)
tk.check_access('metadata_schema_create', context, data_dict)
log.info("Creating metadata standard: %r", data_dict)
tk.check_access('metadata_standard_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_schema_create_schema(), context)
data, errors = tk.navl_validate(data_dict, schema.metadata_standard_create_schema(), context)
if errors:
session.rollback()
raise tk.ValidationError(errors)

metadata_schema = model_save.metadata_schema_dict_save(data, context)
metadata_standard = model_save.metadata_standard_dict_save(data, context)

rev = model.repo.new_revision()
rev.author = user
if 'message' in context:
rev.message = context['message']
else:
rev.message = _(u'REST API: Create metadata schema %s') % metadata_schema.id
rev.message = _(u'REST API: Create metadata standard %s') % metadata_standard.id

if not defer_commit:
model.repo.commit()

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


Expand All @@ -81,7 +79,7 @@ def metadata_model_create(context, data_dict):
You must be authorized to create metadata models.
A model must be one and only one of the following:
- the default for the given schema (no organization or infrastructure)
- the default for the given metadata standard (no organization or infrastructure)
- associated with an organization
- associated with an infrastructure
Expand All @@ -96,8 +94,8 @@ def metadata_model_create(context, data_dict):
:type title: string
:param description: the description of the metadata model (optional)
:type description: string
:param metadata_schema_id: the id or name of the metadata schema from which this model is derived
:type metadata_schema_id: string
:param metadata_standard_id: the id or name of the metadata standard from which this model is derived
:type metadata_standard_id: string
:param model_json: the JSON dictionary defining the model (nullable)
:type model_json: string
:param organization_id: the id or name of the associated organization (nullable)
Expand Down Expand Up @@ -281,8 +279,8 @@ def metadata_record_create(context, data_dict):
:param infrastructures: the infrastructures associated with the record (nullable - may be an empty list);
list of dictionaries each with key ``'id'`` (string, the id or name of the infrastructure)
:type infrastructures: list of dictionaries
:param metadata_schema_id: the id or name of the metadata schema that describes the record's structure
:type metadata_schema_id: string
:param metadata_standard_id: the id or name of the metadata standard that describes the record's structure
:type metadata_standard_id: string
:param metadata_json: JSON dictionary of metadata record content (nullable)
:type metadata_json: string
:param metadata_raw: original unmodified metadata (nullable)
Expand Down
52 changes: 26 additions & 26 deletions ckanext/metadata/logic/action/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,39 @@
log = logging.getLogger(__name__)


def metadata_schema_delete(context, data_dict):
def metadata_standard_delete(context, data_dict):
"""
Delete a metadata schema.
Delete a metadata standard.
You must be authorized to delete the metadata schema.
You must be authorized to delete the metadata standard.
:param id: the id or name of the metadata schema to delete
:param id: the id or name of the metadata standard to delete
:type id: string
"""
log.info("Deleting metadata schema: %r", data_dict)
log.info("Deleting metadata standard: %r", data_dict)

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

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
metadata_standard_id = tk.get_or_bust(data_dict, 'id')
metadata_standard = ckanext_model.MetadataStandard.get(metadata_standard_id)
if metadata_standard is not None:
metadata_standard_id = metadata_standard.id
else:
raise tk.ObjectNotFound('%s: %s' % (_('Not found'), _('Metadata Schema')))
raise tk.ObjectNotFound('%s: %s' % (_('Not found'), _('Metadata Standard')))

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

if session.query(model.Package) \
.join(model.PackageExtra, model.Package.id == model.PackageExtra.package_id) \
.filter(model.PackageExtra.key == 'metadata_schema_id') \
.filter(model.PackageExtra.value == metadata_schema_id) \
.filter(model.PackageExtra.key == 'metadata_standard_id') \
.filter(model.PackageExtra.value == metadata_standard_id) \
.filter(model.Package.type == 'metadata_record') \
.filter(model.Package.state != 'deleted') \
.count() > 0:
raise tk.ValidationError(_('Metadata schema has dependent metadata records'))
raise tk.ValidationError(_('Metadata standard has dependent metadata records'))

cascade_context = {
'model': model,
Expand All @@ -52,30 +52,30 @@ def metadata_schema_delete(context, data_dict):
'defer_commit': True,
}

# clear the base_schema_id on any referencing metadata schemas - implying that
# such schemas are now 'root' schemas, no longer derived from this one
referencing_schemas = session.query(ckanext_model.MetadataSchema) \
.filter(ckanext_model.MetadataSchema.base_schema_id == metadata_schema_id) \
.filter(ckanext_model.MetadataSchema.state != 'deleted') \
# clear the parent_standard_id on any child metadata standards - implying that
# such standards are now 'root' standards, no longer derived from this one
child_standards = session.query(ckanext_model.MetadataStandard) \
.filter(ckanext_model.MetadataStandard.parent_standard_id == metadata_standard_id) \
.filter(ckanext_model.MetadataStandard.state != 'deleted') \
.all()
for referencing_schema in referencing_schemas:
referencing_schema_dict = model_dictize.metadata_schema_dictize(referencing_schema, cascade_context)
referencing_schema_dict['base_schema_id'] = ''
tk.get_action('metadata_schema_update')(cascade_context, referencing_schema_dict)
for child_standard in child_standards:
child_standard_dict = model_dictize.metadata_standard_dictize(child_standard, cascade_context)
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_schema_id == metadata_schema_id) \
.filter(ckanext_model.MetadataModel.metadata_standard_id == metadata_standard_id) \
.filter(ckanext_model.MetadataModel.state != 'deleted') \
.all()
for (metadata_model_id,) in metadata_model_ids:
tk.get_action('metadata_model_delete')(cascade_context, {'id': metadata_model_id})

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

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

Expand Down
56 changes: 28 additions & 28 deletions ckanext/metadata/logic/action/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,57 +17,57 @@


@tk.side_effect_free
def metadata_schema_show(context, data_dict):
def metadata_standard_show(context, data_dict):
"""
Return the details of a metadata schema.
Return the details of a metadata standard.
:param id: the id or name of the metadata schema
:param id: the id or name of the metadata standard
:type id: string
:rtype: dictionary
"""
log.debug("Retrieving metadata schema: %r", data_dict)
log.debug("Retrieving metadata standard: %r", data_dict)

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
metadata_standard_id = tk.get_or_bust(data_dict, 'id')
metadata_standard = ckanext_model.MetadataStandard.get(metadata_standard_id)
if metadata_standard is not None:
metadata_standard_id = metadata_standard.id
else:
raise tk.ObjectNotFound('%s: %s' % (_('Not found'), _('Metadata Schema')))
raise tk.ObjectNotFound('%s: %s' % (_('Not found'), _('Metadata Standard')))

tk.check_access('metadata_schema_show', context, data_dict)
tk.check_access('metadata_standard_show', context, data_dict)

context['metadata_schema'] = metadata_schema
metadata_schema_dict = model_dictize.metadata_schema_dictize(metadata_schema, context)
context['metadata_standard'] = metadata_standard
metadata_standard_dict = model_dictize.metadata_standard_dictize(metadata_standard, context)

result_dict, errors = tk.navl_validate(metadata_schema_dict, schema.metadata_schema_show_schema(), context)
result_dict, errors = tk.navl_validate(metadata_standard_dict, schema.metadata_standard_show_schema(), context)
return result_dict


@tk.side_effect_free
def metadata_schema_list(context, data_dict):
def metadata_standard_list(context, data_dict):
"""
Return a list of names of the site's metadata schemas.
Return a list of names of the site's metadata standards.
:param all_fields: return dictionaries instead of just names (optional, default: ``False``)
:type all_fields: boolean
:rtype: list of strings
"""
log.debug("Retrieving metadata schema list: %r", data_dict)
tk.check_access('metadata_schema_list', context, data_dict)
log.debug("Retrieving metadata standard list: %r", data_dict)
tk.check_access('metadata_standard_list', context, data_dict)

session = context['session']
all_fields = asbool(data_dict.get('all_fields'))

metadata_schemas = session.query(ckanext_model.MetadataSchema.id, ckanext_model.MetadataSchema.name) \
metadata_standards = session.query(ckanext_model.MetadataStandard.id, ckanext_model.MetadataStandard.name) \
.filter_by(state='active') \
.all()
result = []
for (id_, name) in metadata_schemas:
for (id_, name) in metadata_standards:
if all_fields:
data_dict['id'] = id_
result += [tk.get_action('metadata_schema_show')(context, data_dict)]
result += [tk.get_action('metadata_standard_show')(context, data_dict)]
else:
result += [name]

Expand Down Expand Up @@ -160,8 +160,8 @@ def metadata_model_dependent_record_list(context, data_dict):
q = session.query(model.Package.id) \
.join(model.PackageExtra) \
.filter(model.Package.state == 'active') \
.filter(model.PackageExtra.key == 'metadata_schema_id') \
.filter(model.PackageExtra.value == metadata_model.metadata_schema_id)
.filter(model.PackageExtra.key == 'metadata_standard_id') \
.filter(model.PackageExtra.value == metadata_model.metadata_standard_id)

if metadata_model.organization_id:
q = q.filter(model.Package.owner_org == metadata_model.organization_id)
Expand Down Expand Up @@ -446,9 +446,9 @@ def metadata_record_validation_model_list(context, data_dict):
Return a list of metadata models to be used for validating a metadata record.
This comprises the following:
1. The default model defined for the record's metadata schema.
2. A model for that schema (optionally) defined for the owner organization.
3. Any models (optionally) defined for that schema for infrastructures linked to the record.
1. The default model defined for the record's metadata standard.
2. A model for that standard (optionally) defined for the owner organization.
3. Any models (optionally) defined for that standard for infrastructures linked to the record.
:param id: the id or name of the metadata record
:type id: string
Expand Down Expand Up @@ -485,12 +485,12 @@ def metadata_record_validation_model_list(context, data_dict):
.filter(model.Member.state == 'active') \
.all()
infrastructure_ids = [infra_id for (infra_id,) in infrastructure_ids] + [None]
metadata_schema_id = session.query(model.PackageExtra.value) \
.filter_by(package_id=metadata_record_id, key='metadata_schema_id').scalar()
metadata_standard_id = session.query(model.PackageExtra.value) \
.filter_by(package_id=metadata_record_id, key='metadata_standard_id').scalar()

MetadataModel = ckanext_model.MetadataModel
metadata_model_names = session.query(MetadataModel.name) \
.filter_by(metadata_schema_id=metadata_schema_id, state='active') \
.filter_by(metadata_standard_id=metadata_standard_id, state='active') \
.filter(or_(MetadataModel.organization_id == organization_id, MetadataModel.organization_id == None)) \
.filter(or_(MetadataModel.infrastructure_id == infra_id for infra_id in infrastructure_ids)) \
.all()
Expand Down
Loading

0 comments on commit 6dc11f0

Please sign in to comment.