Esempio n. 1
0
    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))
Esempio n. 2
0
    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))
Esempio n. 3
0
    def import_schemas(self, schema_filepath):
        from ckan import model
        from ckanext.dgu.model.schema_codelist import Schema

        # Load file with schemas
        schema_dicts = []
        with open(schema_filepath) as f:
            for line in f.readlines():
                if not line.strip():
                    continue
                schema_dict = json.loads(line)
                schema_dicts.append(schema_dict)

        # Create/update in the db
        for schema in schema_dicts:
            if 'id' in schema:
                existing_schema = Schema.get(schema['id'])
            else:
                existing_schema = Schema.by_title(schema['title'])
            if existing_schema:
                schema['id'] = existing_schema.id
                for k, v in schema.items():
                    setattr(existing_schema, k, v)
                schema_obj = existing_schema
            else:
                schema_obj = Schema(**schema)
                model.Session.add(schema_obj)
            model.Session.commit()
            # Print JSONL with ids, in case you want to save with IDs
            print json.dumps(schema_obj.as_dict())
        model.Session.remove()
Esempio n. 4
0
    def import_schemas(self, schema_filepath):
        from ckan import model
        from ckanext.dgu.model.schema_codelist import Schema

        # Load file with schemas
        schema_dicts = []
        with open(schema_filepath) as f:
            for line in f.readlines():
                if not line.strip():
                    continue
                schema_dict = json.loads(line)
                schema_dicts.append(schema_dict)

        # Create/update in the db
        for schema in schema_dicts:
            if 'id' in schema:
                existing_schema = Schema.get(schema['id'])
            else:
                existing_schema = Schema.by_title(schema['title'])
            if existing_schema:
                schema['id'] = existing_schema.id
                for k, v in schema.items():
                    setattr(existing_schema, k, v)
                schema_obj = existing_schema
            else:
                schema_obj = Schema(**schema)
                model.Session.add(schema_obj)
            model.Session.commit()
            # Print JSONL with ids, in case you want to save with IDs
            print json.dumps(schema_obj.as_dict())
        model.Session.remove()
Esempio n. 5
0
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()
Esempio n. 6
0
 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)
Esempio n. 7
0
 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)
Esempio n. 8
0
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