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

How to upload files to package in CKAN extension?

我打算在 CKAN 中执行以下操作:当添加一个包含 Excel 的新包时,我将各个工作表转换为 .csv 并将它们添加到包中。

我用一个插件来做到这一点,使用通知功能。已执行扩展代码并正确添加了 .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)

有什么建议吗?

添加新包时,CKAN 会创建包对象。它启动一个事务 (vdm.sqlalchemy.new_revision()),但在提交之前,它会为任何侦听器调用 observer.notify(),正如我们在您的堆栈跟踪中看到的那样。在此通知期间,您的代码调用 resource_create(),再次 启动一个事务(错误)然后提交它。在这里的某个地方 transaction/session/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。因此,您在单独的 session/transaction 中对模型进行更改。 https://docs.ckan.org/en/2.8/maintaining/background-tasks.html