Can move or delete file after import, md5 verification OK

This commit is contained in:
Valentin Chemiere 2015-03-10 16:49:40 +01:00
parent dec1aff9f7
commit d9479ea945
7 changed files with 27 additions and 6 deletions

View File

@ -32,6 +32,7 @@
""",
'depends': [
'base',
'ir_attachment_metadata',
],
'data': [
'attachment_view.xml',

View File

@ -13,7 +13,8 @@ class AbstractTask(object):
'datas': b64encode(data),
'datas_fname': filename,
'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

View File

@ -25,8 +25,6 @@
<field name="task_id"/>
<field name="location_id"/>
<field name="type"/>
<field name="company_id" groups="base.group_multi_company"/>
<field name="create_uid"/>
<field name="create_date"/>
<field name="state"/>
</tree>

View File

@ -36,6 +36,9 @@ class Task(models.Model):
location_id = fields.Many2one('external.file.location', string='Location')
attachment_ids = fields.One2many('ir.attachment', 'task_id',
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):
res = []
@ -69,6 +72,9 @@ class Task(models.Model):
'path': self.filepath,
'attachment_ids': self.attachment_ids,
'task': self,
'move_path': self.move_path,
'delete_file': self.delete_file,
'move_file': self.move_file,
}
conn = method_class(self.env, config)
conn.run()

View File

@ -16,6 +16,9 @@
<field name="location_id" colspan="2"/>
<field name="filename" 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"/>
<field name="attachment_ids" colspan="4" nolabel="1"/>
<button name="run" type="object" string="Run" icon="gtk-execute"/>
@ -33,6 +36,7 @@
<field name="method"/>
<field name="filename"/>
<field name="filepath"/>
<button name="run" type="object" string="Run" icon="gtk-execute"/>
</tree>
</field>
</record>

View File

@ -25,6 +25,8 @@ import ftputil
import ftputil.session
from ftputil.error import FTPIOError
import logging
from base64 import b64encode
import hashlib
_logger = logging.getLogger(__name__)
@ -44,9 +46,11 @@ class FtpTask(AbstractTask):
self.file_name = config.get('file_name', '')
self.path = config.get('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.attachment_ids = config.get('attachment_ids', False)
self.task = config.get('task', False)
self.ext_hash = False
class FtpImportTask(FtpTask):
@ -85,6 +89,11 @@ class FtpImportTask(FtpTask):
_logger.info('Deleting file %s' % 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):
port_session_factory = ftputil.session.session_factory(
port=self.port)
@ -99,6 +108,7 @@ class FtpImportTask(FtpTask):
source_name = self._source_name(path, self.file_name)
if ftp_conn.path.isfile(source_name) and \
ftpfile == self.file_name:
self.ext_hash = self._get_hash(source_name, ftp_conn)
self._handle_new_source(
ftp_conn,
path,
@ -110,7 +120,7 @@ class FtpImportTask(FtpTask):
if self.delete_file:
for ftpfile in downloaded_files:
self._delete_file(ftp_conn,
self._source_name(self.path,
self._source_name(path,
ftpfile))
elif self.move_path:
if not ftp_conn.path.exists(self.move_path):
@ -118,7 +128,7 @@ class FtpImportTask(FtpTask):
for ftpfile in downloaded_files:
self._move_file(
ftp_conn,
self._source_name(self.path, ftpfile),
self._source_name(path, ftpfile),
self._source_name(self.move_path, ftpfile))

View File

@ -23,6 +23,7 @@
from openerp import models, fields, api, _
from openerp.exceptions import Warning
import hashlib
from base64 import b64decode
class IrAttachment(models.Model):
@ -34,7 +35,7 @@ class IrAttachment(models.Model):
@api.depends('datas', 'external_hash')
def _compute_hash(self):
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:
raise Warning(_('File corrupted'),
_("Something was wrong with the retreived file, "