mirror of
https://github.com/OCA/knowledge.git
synced 2025-07-25 18:08:42 -06:00
One task could have multiple attachment to handle
This commit is contained in:
parent
b39b495d8e
commit
277cd20055
@ -11,13 +11,9 @@ class AbstractTask(object):
|
||||
{
|
||||
'name': filename,
|
||||
'datas': b64encode(data),
|
||||
'datas_fname': filename
|
||||
'datas_fname': filename,
|
||||
'task_id': self.task.id,
|
||||
'location_id': self.task.location_id.id
|
||||
}
|
||||
)
|
||||
return ir_attachment_id
|
||||
|
||||
# def load_file(self, file_id):
|
||||
# f = self.session.browse('impexp.file', file_id)
|
||||
# if not f.attachment_id.datas:
|
||||
# return None
|
||||
# return b64decode(f.attachment_id.datas)
|
||||
|
@ -31,7 +31,7 @@ class IrAttachment(models.Model):
|
||||
('pending', 'Pending'),
|
||||
('failed', 'Failed'),
|
||||
('done', 'Done'),
|
||||
], readonly=True, required=True, default='pending')
|
||||
], readonly=False, required=True, default='pending')
|
||||
state_message = fields.Text()
|
||||
task_id = fields.Many2one('external.file.task', string='Task')
|
||||
location_id = fields.Many2one('external.file.location', string='Location',
|
||||
|
@ -67,7 +67,8 @@ class Task(models.Model):
|
||||
'allow_dir_creation': False,
|
||||
'file_name': self.filename,
|
||||
'path': self.filepath,
|
||||
'attachment_id': self.attachment_id,
|
||||
'attachment_ids': self.attachment_ids,
|
||||
'task': self,
|
||||
}
|
||||
conn = method_class(self.env, config)
|
||||
conn.run()
|
||||
|
@ -13,7 +13,8 @@
|
||||
<field name="location_id" colspan="4"/>
|
||||
<field name="filename" colspan="4"/>
|
||||
<field name="filepath" colspan="4"/>
|
||||
<field name="attachment_ids" colspan="4"/>
|
||||
<separator string="Attachments" colspan="4"/>
|
||||
<field name="attachment_ids" colspan="4" nolabel="1"/>
|
||||
<button name="run" type="object" string="Run"/>
|
||||
</group>
|
||||
</sheet>
|
||||
|
@ -42,10 +42,11 @@ class FtpTask(AbstractTask):
|
||||
self.port = config.get('port', '')
|
||||
self.allow_dir_creation = config.get('allow_dir_creation', '')
|
||||
self.file_name = config.get('file_name', '')
|
||||
self.path = config.get('path', './')
|
||||
self.path = config.get('path', '.')
|
||||
self.move_path = config.get('move_path', '')
|
||||
self.delete_file = config.get('delete_file', False)
|
||||
self.attachment_id = config.get('attachment_id', False)
|
||||
self.attachment_ids = config.get('attachment_ids', False)
|
||||
self.task = config.get('task', False)
|
||||
|
||||
|
||||
class FtpImportTask(FtpTask):
|
||||
@ -91,15 +92,16 @@ class FtpImportTask(FtpTask):
|
||||
self.pwd,
|
||||
session_factory=port_session_factory) as ftp_conn:
|
||||
|
||||
file_list = ftp_conn.listdir(self.path)
|
||||
path = self.path or '.'
|
||||
file_list = ftp_conn.listdir(path)
|
||||
downloaded_files = []
|
||||
for ftpfile in file_list:
|
||||
source_name = self._source_name(self.path, self.file_name)
|
||||
source_name = self._source_name(path, self.file_name)
|
||||
if ftp_conn.path.isfile(source_name) and \
|
||||
ftpfile == self.file_name:
|
||||
self._handle_new_source(
|
||||
ftp_conn,
|
||||
self.path,
|
||||
path,
|
||||
self.file_name,
|
||||
self.move_path)
|
||||
downloaded_files.append(self.file_name)
|
||||
@ -148,7 +150,7 @@ class FtpExportTask(FtpTask):
|
||||
return upload_directory + '/' + filename
|
||||
|
||||
def _upload_file(self, host, port, user, pwd, path, filename, filedata):
|
||||
upload_directory = path or './'
|
||||
upload_directory = path or '.'
|
||||
port_session_factory = ftputil.session.session_factory(port=port)
|
||||
with ftputil.FTPHost(host, user, pwd,
|
||||
session_factory=port_session_factory) as ftp_conn:
|
||||
@ -161,6 +163,10 @@ class FtpExportTask(FtpTask):
|
||||
self._handle_new_target(ftp_conn, target_name, filedata)
|
||||
|
||||
def run(self, async=True):
|
||||
self._upload_file(self.host, self.port, self.user, self.pwd, self.path,
|
||||
self.attachment_id.datas_fname,
|
||||
b64decode(self.attachment_id.datas))
|
||||
for attachment in self.attachment_ids:
|
||||
if attachment.state in ('pending', 'failed'):
|
||||
self.attachment_id = attachment
|
||||
self._upload_file(self.host, self.port, self.user, self.pwd,
|
||||
self.path,
|
||||
attachment.datas_fname,
|
||||
b64decode(attachment.datas))
|
||||
|
Loading…
Reference in New Issue
Block a user