def add_schema(cls, pkg_dict): from ckanext.dgu.model.schema_codelist import Schema, Codelist try: schema_ids = json.loads(pkg_dict.get('schema') or '[]') except ValueError: log.error('Not valid JSON in schema field: %s %r', pkg_dict['name'], pkg_dict.get('schema')) schemas = None schemas = [] for schema_id in schema_ids: schemas.append(Schema.get(schema_id).title) pkg_dict['schema_multi'] = schemas log.debug('Schema: %s', ' '.join(schemas)) try: codelist_ids = json.loads(pkg_dict.get('codelist') or '[]') except ValueError: log.error('Not valid JSON in codelists field: %s %r', pkg_dict['name'], pkg_dict.get('codelist')) codelists = None codelists = [] for codelist_id in codelist_ids: codelists.append(Codelist.get(codelist_id).title) pkg_dict['codelist_multi'] = codelists log.debug('Code lists: %s', ' '.join(codelists))
def add_schema(cls, pkg_dict): from ckanext.dgu.model.schema_codelist import Schema, Codelist try: schema_ids = json.loads(pkg_dict.get('schema') or '[]') except ValueError: log.error('Not valid JSON in schema field: %s %r', pkg_dict['name'], pkg_dict.get('schema')) schemas = None schemas = [] for schema_id in schema_ids: schemas.append(Schema.get(schema_id).title) pkg_dict['schema_multi'] = schemas log.debug('Schema: %s', ' '.join(schemas)) try: codelist_ids = json.loads(pkg_dict.get('codelist') or '[]') except ValueError: log.error('Not valid JSON in codelists field: %s %r', pkg_dict['name'], pkg_dict.get('codelist')) codelists = None codelists = [] for codelist_id in codelist_ids: codelists.append(Codelist.get(codelist_id).title) pkg_dict['codelist_multi'] = codelists log.debug('Code lists: %s', ' '.join(codelists))
def id_to_dict(key, data, errors, context): from ckanext.dgu.model.schema_codelist import Schema, Codelist for i, id_ in enumerate(data[key]): if key == ('schema',): obj = Schema.get(id_) elif key == ('codelist',): obj = Codelist.get(id_) else: raise NotImplementedError('Bad key: %s' % key) if not obj: raise Invalid('%s id does not exist: %s' % (key, id_)) data[key][i] = obj.as_dict()
def schema_codelist_validator(key, data, errors, context): from ckanext.dgu.model.schema_codelist import Schema, Codelist for i, schema_ref in enumerate(data[key]): if not schema_ref: # drop-down has no selection - ignore continue # form gives an ID. API might give a title. if key == ('schema',): obj = Schema.get(schema_ref) or Schema.by_title(schema_ref) or \ Schema.by_url(schema_ref) elif key == ('codelist',): obj = Codelist.get(schema_ref) or Codelist.by_title(schema_ref) or\ Codelist.by_url(schema_ref) else: raise NotImplementedError('Bad key: %s' % key) if not obj: raise Invalid('%s id does not exist: %r' % (key[0], schema_ref)) # write the ID in case it came in via the API and was a URL or title data[key][i] = obj.id
@classmethod def add_schema(cls, pkg_dict): from ckanext.dgu.model.schema_codelist import Schema, Codelist try: schema_ids = json.loads(pkg_dict.get('schema') or '[]') except ValueError: log.error('Not valid JSON in schema field: %s %r', pkg_dict['name'], pkg_dict.get('schema')) schema_ids = None schemas = [] for schema_id in schema_ids: try: schemas.append(Schema.get(schema_id).title) except AttributeError, e: log.error('Invalid schema_id: %r %s', schema_id, e) pkg_dict['schema_multi'] = schemas #log.debug('Schema: %s', ' '.join(schemas)) try: codelist_ids = json.loads(pkg_dict.get('codelist') or '[]') except ValueError: log.error('Not valid JSON in codelists field: %s %r', pkg_dict['name'], pkg_dict.get('codelist')) codelists = None codelists = [] for codelist_id in codelist_ids: codelists.append(Codelist.get(codelist_id).title) pkg_dict['codelist_multi'] = codelists #log.debug('Code lists: %s', ' '.join(codelists))
@classmethod def add_schema(cls, pkg_dict): from ckanext.dgu.model.schema_codelist import Schema, Codelist try: schema_ids = json.loads(pkg_dict.get('schema') or '[]') except ValueError: log.error('Not valid JSON in schema field: %s %r', pkg_dict['name'], pkg_dict.get('schema')) schema_ids = None schemas = [] for schema_id in schema_ids: try: schemas.append(Schema.get(schema_id).title) except AttributeError, e: log.error('Invalid schema_id: %r %s', schema_id, e) pkg_dict['schema_multi'] = schemas log.debug('Schema: %s', ' '.join(schemas)) try: codelist_ids = json.loads(pkg_dict.get('codelist') or '[]') except ValueError: log.error('Not valid JSON in codelists field: %s %r', pkg_dict['name'], pkg_dict.get('codelist')) codelists = None codelists = [] for codelist_id in codelist_ids: codelists.append(Codelist.get(codelist_id).title) pkg_dict['codelist_multi'] = codelists log.debug('Code lists: %s', ' '.join(codelists))