mirror of
https://github.com/OCA/knowledge.git
synced 2025-07-26 02:18:40 -06:00
Can move or delete file after import, md5 verification OK
This commit is contained in:
parent
dec1aff9f7
commit
d9479ea945
@ -32,6 +32,7 @@
|
|||||||
""",
|
""",
|
||||||
'depends': [
|
'depends': [
|
||||||
'base',
|
'base',
|
||||||
|
'ir_attachment_metadata',
|
||||||
],
|
],
|
||||||
'data': [
|
'data': [
|
||||||
'attachment_view.xml',
|
'attachment_view.xml',
|
||||||
|
@ -13,7 +13,8 @@ class AbstractTask(object):
|
|||||||
'datas': b64encode(data),
|
'datas': b64encode(data),
|
||||||
'datas_fname': filename,
|
'datas_fname': filename,
|
||||||
'task_id': self.task.id,
|
'task_id': self.task.id,
|
||||||
'location_id': self.task.location_id.id
|
'location_id': self.task.location_id.id,
|
||||||
|
'external_hash': self.ext_hash
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
return ir_attachment_id
|
return ir_attachment_id
|
||||||
|
@ -25,8 +25,6 @@
|
|||||||
<field name="task_id"/>
|
<field name="task_id"/>
|
||||||
<field name="location_id"/>
|
<field name="location_id"/>
|
||||||
<field name="type"/>
|
<field name="type"/>
|
||||||
<field name="company_id" groups="base.group_multi_company"/>
|
|
||||||
<field name="create_uid"/>
|
|
||||||
<field name="create_date"/>
|
<field name="create_date"/>
|
||||||
<field name="state"/>
|
<field name="state"/>
|
||||||
</tree>
|
</tree>
|
||||||
|
@ -36,6 +36,9 @@ class Task(models.Model):
|
|||||||
location_id = fields.Many2one('external.file.location', string='Location')
|
location_id = fields.Many2one('external.file.location', string='Location')
|
||||||
attachment_ids = fields.One2many('ir.attachment', 'task_id',
|
attachment_ids = fields.One2many('ir.attachment', 'task_id',
|
||||||
string='Attachment')
|
string='Attachment')
|
||||||
|
delete_file = fields.Boolean(string='Delete file')
|
||||||
|
move_file = fields.Boolean(string='Move file')
|
||||||
|
move_path = fields.Char(string='Move path')
|
||||||
|
|
||||||
def _get_method(self):
|
def _get_method(self):
|
||||||
res = []
|
res = []
|
||||||
@ -69,6 +72,9 @@ class Task(models.Model):
|
|||||||
'path': self.filepath,
|
'path': self.filepath,
|
||||||
'attachment_ids': self.attachment_ids,
|
'attachment_ids': self.attachment_ids,
|
||||||
'task': self,
|
'task': self,
|
||||||
|
'move_path': self.move_path,
|
||||||
|
'delete_file': self.delete_file,
|
||||||
|
'move_file': self.move_file,
|
||||||
}
|
}
|
||||||
conn = method_class(self.env, config)
|
conn = method_class(self.env, config)
|
||||||
conn.run()
|
conn.run()
|
||||||
|
@ -16,6 +16,9 @@
|
|||||||
<field name="location_id" colspan="2"/>
|
<field name="location_id" colspan="2"/>
|
||||||
<field name="filename" colspan="4"/>
|
<field name="filename" colspan="4"/>
|
||||||
<field name="filepath" colspan="4"/>
|
<field name="filepath" colspan="4"/>
|
||||||
|
<field name="move_file" colspan="2"/>
|
||||||
|
<field name="move_path" colspan="2"/>
|
||||||
|
<field name="delete_file" colspan="2"/>
|
||||||
<separator string="Attachments" colspan="4"/>
|
<separator string="Attachments" colspan="4"/>
|
||||||
<field name="attachment_ids" colspan="4" nolabel="1"/>
|
<field name="attachment_ids" colspan="4" nolabel="1"/>
|
||||||
<button name="run" type="object" string="Run" icon="gtk-execute"/>
|
<button name="run" type="object" string="Run" icon="gtk-execute"/>
|
||||||
@ -33,6 +36,7 @@
|
|||||||
<field name="method"/>
|
<field name="method"/>
|
||||||
<field name="filename"/>
|
<field name="filename"/>
|
||||||
<field name="filepath"/>
|
<field name="filepath"/>
|
||||||
|
<button name="run" type="object" string="Run" icon="gtk-execute"/>
|
||||||
</tree>
|
</tree>
|
||||||
</field>
|
</field>
|
||||||
</record>
|
</record>
|
||||||
|
@ -25,6 +25,8 @@ import ftputil
|
|||||||
import ftputil.session
|
import ftputil.session
|
||||||
from ftputil.error import FTPIOError
|
from ftputil.error import FTPIOError
|
||||||
import logging
|
import logging
|
||||||
|
from base64 import b64encode
|
||||||
|
import hashlib
|
||||||
_logger = logging.getLogger(__name__)
|
_logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@ -44,9 +46,11 @@ class FtpTask(AbstractTask):
|
|||||||
self.file_name = config.get('file_name', '')
|
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.move_path = config.get('move_path', '')
|
||||||
|
self.move_file = config.get('move_file', False)
|
||||||
self.delete_file = config.get('delete_file', False)
|
self.delete_file = config.get('delete_file', False)
|
||||||
self.attachment_ids = config.get('attachment_ids', False)
|
self.attachment_ids = config.get('attachment_ids', False)
|
||||||
self.task = config.get('task', False)
|
self.task = config.get('task', False)
|
||||||
|
self.ext_hash = False
|
||||||
|
|
||||||
|
|
||||||
class FtpImportTask(FtpTask):
|
class FtpImportTask(FtpTask):
|
||||||
@ -85,6 +89,11 @@ class FtpImportTask(FtpTask):
|
|||||||
_logger.info('Deleting file %s' % source)
|
_logger.info('Deleting file %s' % source)
|
||||||
ftp_conn.remove(source)
|
ftp_conn.remove(source)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _get_hash(file_name, ftp_conn):
|
||||||
|
with ftp_conn.open(file_name, 'rb') as f:
|
||||||
|
return hashlib.md5(f.read()).hexdigest()
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
port_session_factory = ftputil.session.session_factory(
|
port_session_factory = ftputil.session.session_factory(
|
||||||
port=self.port)
|
port=self.port)
|
||||||
@ -99,6 +108,7 @@ class FtpImportTask(FtpTask):
|
|||||||
source_name = self._source_name(path, self.file_name)
|
source_name = self._source_name(path, self.file_name)
|
||||||
if ftp_conn.path.isfile(source_name) and \
|
if ftp_conn.path.isfile(source_name) and \
|
||||||
ftpfile == self.file_name:
|
ftpfile == self.file_name:
|
||||||
|
self.ext_hash = self._get_hash(source_name, ftp_conn)
|
||||||
self._handle_new_source(
|
self._handle_new_source(
|
||||||
ftp_conn,
|
ftp_conn,
|
||||||
path,
|
path,
|
||||||
@ -110,7 +120,7 @@ class FtpImportTask(FtpTask):
|
|||||||
if self.delete_file:
|
if self.delete_file:
|
||||||
for ftpfile in downloaded_files:
|
for ftpfile in downloaded_files:
|
||||||
self._delete_file(ftp_conn,
|
self._delete_file(ftp_conn,
|
||||||
self._source_name(self.path,
|
self._source_name(path,
|
||||||
ftpfile))
|
ftpfile))
|
||||||
elif self.move_path:
|
elif self.move_path:
|
||||||
if not ftp_conn.path.exists(self.move_path):
|
if not ftp_conn.path.exists(self.move_path):
|
||||||
@ -118,7 +128,7 @@ class FtpImportTask(FtpTask):
|
|||||||
for ftpfile in downloaded_files:
|
for ftpfile in downloaded_files:
|
||||||
self._move_file(
|
self._move_file(
|
||||||
ftp_conn,
|
ftp_conn,
|
||||||
self._source_name(self.path, ftpfile),
|
self._source_name(path, ftpfile),
|
||||||
self._source_name(self.move_path, ftpfile))
|
self._source_name(self.move_path, ftpfile))
|
||||||
|
|
||||||
|
|
||||||
|
@ -23,6 +23,7 @@
|
|||||||
from openerp import models, fields, api, _
|
from openerp import models, fields, api, _
|
||||||
from openerp.exceptions import Warning
|
from openerp.exceptions import Warning
|
||||||
import hashlib
|
import hashlib
|
||||||
|
from base64 import b64decode
|
||||||
|
|
||||||
|
|
||||||
class IrAttachment(models.Model):
|
class IrAttachment(models.Model):
|
||||||
@ -34,7 +35,7 @@ class IrAttachment(models.Model):
|
|||||||
@api.depends('datas', 'external_hash')
|
@api.depends('datas', 'external_hash')
|
||||||
def _compute_hash(self):
|
def _compute_hash(self):
|
||||||
if self.datas:
|
if self.datas:
|
||||||
self.internal_hash = hashlib.md5(self.datas).hexdigest()
|
self.internal_hash = hashlib.md5(b64decode(self.datas)).hexdigest()
|
||||||
if self.external_hash and self.internal_hash != self.external_hash:
|
if self.external_hash and self.internal_hash != self.external_hash:
|
||||||
raise Warning(_('File corrupted'),
|
raise Warning(_('File corrupted'),
|
||||||
_("Something was wrong with the retreived file, "
|
_("Something was wrong with the retreived file, "
|
||||||
|
Loading…
Reference in New Issue
Block a user