如何将文件上传到CKAN扩展包中?

时间:2019-05-08 12:03:21

标签: ckan

我打算在CKAN中执行以下操作:当添加带有Excel的新程序包时,我将各个工作表转换为.csv并将其添加到程序包中。

我通过使用notify函数使用插件来执行此操作。执行了扩展代码,并正确添加了.csv文件,但是仍然收到我无法解决的错误:

ckan          | 2019-05-08 11:44:09,640 ERROR [ckan.model.modification] 'NoneType' object has no attribute 'transaction'
ckan          | Traceback (most recent call last):
ckan          |   File "/usr/lib/ckan/venv/src/ckan/ckan/model/modification.py", line 77, in notify
ckan          |     observer.notify(entity, operation)
ckan          |   File "/usr/lib/ckan/venv/lib/python2.7/site-packages/ckanext_pxconverter-0.0.1-py2.7.egg/ckanext/pxconverter/plugin.py", line 53, in notify
ckan          |     convert_px_data(entity, resources)
ckan          |   File "/usr/lib/ckan/venv/lib/python2.7/site-packages/ckanext_pxconverter-0.0.1-py2.7.egg/ckanext/pxconverter/converter.py", line 134, in convert_px_data
ckan          |     upload_file(registry, os.path.join(tmpdir, "px_run_info.csv"), api_key, package_id=package.id, name="px_run_info.csv")
ckan          |   File "/usr/lib/ckan/venv/lib/python2.7/site-packages/ckanext_pxconverter-0.0.1-py2.7.egg/ckanext/pxconverter/converter.py", line 56, in upload_file
ckan          |     name=data['name'])
ckan          |   File "/usr/lib/ckan/venv/local/lib/python2.7/site-packages/ckanapi/common.py", line 50, in action
ckan          |     files=files)
ckan          |   File "/usr/lib/ckan/venv/local/lib/python2.7/site-packages/ckanapi/localckan.py", line 71, in call_action
ckan          |     return self._get_action(action)(context, data_dict)
ckan          |   File "/usr/lib/ckan/venv/src/ckan/ckan/logic/__init__.py", line 464, in wrapped
ckan          |     result = _action(context, data_dict, **kw)
ckan          |   File "/usr/lib/ckan/venv/src/ckan/ckan/logic/action/create.py", line 329, in resource_create
ckan          |     model.repo.commit()
ckan          |   File "/usr/lib/ckan/venv/local/lib/python2.7/site-packages/vdm/sqlalchemy/tools.py", line 107, in commit
ckan          |     self.session.commit()
ckan          |   File "/usr/lib/ckan/venv/local/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py", line 157, in do
ckan          |     return getattr(self.registry(), name)(*args, **kwargs)
ckan          |   File "/usr/lib/ckan/venv/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 906, in commit
ckan          |     self.transaction.commit()
ckan          |   File "/usr/lib/ckan/venv/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 461, in commit
ckan          |     self._prepare_impl()
ckan          |   File "/usr/lib/ckan/venv/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 432, in _prepare_impl
ckan          |     stx = self.session.transaction
ckan          | AttributeError: 'NoneType' object has no attribute 'transaction'

我在“ convert_xlsx”函数中使用以下代码上传CSV文件:

from ckanapi import LocalCKAN

registry = LocalCKAN()

registry.action.resource_create(
        package_id=data['package_id'],
        url='dummy-value',
        upload=open(csv_file_path, 'rb'),
        name=data['name'])

要在plugin.py中进行编码,如下所示:

def notify(self, entity, operation=None):
    if isinstance(entity, model.Package):
        if operation != 'deleted':
            resources = entity.resources_all
            log.debug(resources)
            convert_xlsx(entity, resources)

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

添加新软件包时,CKAN将创建Package对象。它启动了一个事务(vdm.sqlalchemy.new_revision()),但是在提交该事务之前,它为所有侦听器调用observer.notify(),就像我们在堆栈跟踪中看到的那样。在此期间,您的代码将调用resource_create(),再次 会启动一个事务(错误),然后提交该事务。在这里的某个地方,事务/会话/ vdm混乱了,您看到的错误是:

File "/usr/lib/ckan/venv/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 432, in _prepare_impl
     stx = self.session.transaction
 AttributeError: 'NoneType' object has no attribute 'transaction'

解决方案在notify()中使后台作业排队,该作业执行resource_create。因此,您需要在单独的会话/事务中对模型进行更改。 https://docs.ckan.org/en/2.8/maintaining/background-tasks.html