From a220ace8ba0cdb5979d5e21a95ddab6c457c7989 Mon Sep 17 00:00:00 2001 From: David Beal Date: Tue, 23 Feb 2016 20:38:59 +0100 Subject: [PATCH] [FIX] encode to utf8 user/password [FIX] prevent not installed class been evaluated [FIX] python header in akretion modules [FIX] check if class is in installed module, README --- external_file_location/__init__.py | 22 -------- external_file_location/__openerp__.py | 24 ++------- external_file_location/abstract_task.py | 5 +- external_file_location/attachment.py | 30 +++-------- external_file_location/helper.py | 60 ++++++++++++++------- external_file_location/location.py | 24 ++------- external_file_location/task.py | 45 ++++++---------- external_file_location/tasks/__init__.py | 22 -------- external_file_location/tasks/abstract_fs.py | 22 ++------ external_file_location/tasks/filestore.py | 22 ++------ external_file_location/tasks/ftp.py | 28 +++------- external_file_location/tasks/sftp.py | 22 ++------ external_file_location/tests/__init__.py | 22 -------- external_file_location/tests/mock_server.py | 24 ++------- external_file_location/tests/test_sftp.py | 24 ++------- 15 files changed, 95 insertions(+), 301 deletions(-) diff --git a/external_file_location/__init__.py b/external_file_location/__init__.py index 101a9f4ad..b1341a77b 100644 --- a/external_file_location/__init__.py +++ b/external_file_location/__init__.py @@ -1,25 +1,3 @@ -# -*- coding: utf-8 -*- -############################################################################### -# -# Module for OpenERP -# Copyright (C) 2014 Akretion (http://www.akretion.com). -# @author Sébastien BEAU -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################### - from . import attachment from . import location from . import task diff --git a/external_file_location/__openerp__.py b/external_file_location/__openerp__.py index 8d286d4e8..5dbe2813c 100644 --- a/external_file_location/__openerp__.py +++ b/external_file_location/__openerp__.py @@ -1,24 +1,6 @@ -# -*- coding: utf-8 -*- -############################################################################### -# -# Module for OpenERP -# Copyright (C) 2015 Akretion (http://www.akretion.com). -# @author Valentin CHEMIERE -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################### +# coding: utf-8 +# @ 2015 Valentin CHEMIERE @ Akretion +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). { 'name': 'external_file_location', diff --git a/external_file_location/abstract_task.py b/external_file_location/abstract_task.py index 15e2323e9..82216787b 100644 --- a/external_file_location/abstract_task.py +++ b/external_file_location/abstract_task.py @@ -1,5 +1,6 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- +# coding: utf-8 +# @ 2015 Valentin CHEMIERE @ Akretion +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from base64 import b64encode diff --git a/external_file_location/attachment.py b/external_file_location/attachment.py index 26925547b..49424bc43 100644 --- a/external_file_location/attachment.py +++ b/external_file_location/attachment.py @@ -1,24 +1,6 @@ -# -*- coding: utf-8 -*- -############################################################################### -# -# Module for OpenERP -# Copyright (C) 2015 Akretion (http://www.akretion.com). -# @author Valentin CHEMIERE -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################### +# coding: utf-8 +# @ 2015 Valentin CHEMIERE @ Akretion +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from openerp import models, fields @@ -34,6 +16,6 @@ class IrAttachmentMetadata(models.Model): ], 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', - related='task_id.location_id', store=True - ) + location_id = fields.Many2one( + 'external.file.location', string='Location', + related='task_id.location_id', store=True) diff --git a/external_file_location/helper.py b/external_file_location/helper.py index 6bfe08dcb..f19302033 100644 --- a/external_file_location/helper.py +++ b/external_file_location/helper.py @@ -1,23 +1,7 @@ -# -*- coding: utf-8 -*- -############################################################################## -# +# coding: utf-8 # Author: Joel Grand-Guillaume # Copyright 2011-2012 Camptocamp SA -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). def itersubclasses(cls, _seen=None): @@ -58,3 +42,43 @@ def itersubclasses(cls, _seen=None): yield sub for sub in itersubclasses(sub, _seen): yield sub + + +def _get_erp_module_name(module_path): + # see this PR for v9 https://github.com/odoo/odoo/pull/11084 + """ Extract the name of the Odoo module from the path of the + Python module. + + Taken from Odoo server: ``openerp.models.MetaModel`` + + The (Odoo) module name can be in the ``openerp.addons`` namespace + or not. For instance module ``sale`` can be imported as + ``openerp.addons.sale`` (the good way) or ``sale`` (for backward + compatibility). + """ + module_parts = module_path.split('.') + if len(module_parts) > 2 and module_parts[:2] == ['openerp', 'addons']: + module_name = module_parts[2] + else: + module_name = module_parts[0] + return module_name + + +def is_module_installed(env, module_name): + """ Check if an Odoo addon is installed. + + :param module_name: name of the addon + """ + # the registry maintains a set of fully loaded modules so we can + # lookup for our module there + return module_name in env.registry._init_modules + + +def get_erp_module(cls_or_func): + """ For a top level function or class, returns the + name of the Odoo module where it lives. + + So we will be able to filter them according to the modules + installation state. + """ + return _get_erp_module_name(cls_or_func.__module__) diff --git a/external_file_location/location.py b/external_file_location/location.py index 91d4ad2f8..5c0eb7985 100644 --- a/external_file_location/location.py +++ b/external_file_location/location.py @@ -1,24 +1,6 @@ -# -*- coding: utf-8 -*- -############################################################################### -# -# Module for OpenERP -# Copyright (C) 2015 Akretion (http://www.akretion.com). -# @author Valentin CHEMIERE -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################### +# coding: utf-8 +# @ 2015 Valentin CHEMIERE @ Akretion +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from openerp import models, fields, api from .abstract_task import AbstractTask diff --git a/external_file_location/task.py b/external_file_location/task.py index 81ca0502a..2e23ff421 100644 --- a/external_file_location/task.py +++ b/external_file_location/task.py @@ -1,27 +1,9 @@ -# -*- coding: utf-8 -*- -############################################################################### -# -# Module for OpenERP -# Copyright (C) 2015 Akretion (http://www.akretion.com). -# @author Valentin CHEMIERE -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################### +# coding: utf-8 +# @ 2015 Valentin CHEMIERE @ Akretion +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from openerp import models, fields, api -from .helper import itersubclasses +from .helper import itersubclasses, get_erp_module, is_module_installed from .abstract_task import AbstractTask @@ -52,9 +34,11 @@ class Task(models.Model): def _get_method(self): res = [] for cls in itersubclasses(AbstractTask): - if cls._synchronize_type \ - and ('protocol' not in self._context - or cls._key == self._context['protocol']): + if not is_module_installed(self.env, get_erp_module(cls)): + continue + if cls._synchronize_type and ( + 'protocol' not in self._context or + cls._key == self._context['protocol']): cls_info = (cls._key + '_' + cls._synchronize_type, cls._name + ' ' + cls._synchronize_type) res.append(cls_info) @@ -78,13 +62,16 @@ class Task(models.Model): @api.one def run(self): for cls in itersubclasses(AbstractTask): - if cls._synchronize_type and \ - cls._key + '_' + cls._synchronize_type == self.method: + if not is_module_installed(self.env, get_erp_module(cls)): + continue + cls_build = '%s_%s' % (cls._key, cls._synchronize_type) + if cls._synchronize_type and cls_build == self.method: method_class = cls config = { 'host': self.location_id.address, - 'user': self.location_id.login, - 'pwd': self.location_id.password, + # ftplib does not support unicode + 'user': self.location_id.login.encode('utf-8'), + 'pwd': self.location_id.password.encode('utf-8'), 'port': self.location_id.port, 'allow_dir_creation': False, 'file_name': self.filename, diff --git a/external_file_location/tasks/__init__.py b/external_file_location/tasks/__init__.py index 61548c1a2..49ace2611 100644 --- a/external_file_location/tasks/__init__.py +++ b/external_file_location/tasks/__init__.py @@ -1,25 +1,3 @@ -# -*- coding: utf-8 -*- -############################################################################### -# -# Module for OpenERP -# Copyright (C) 2015 Akretion (http://www.akretion.com). -# @author Valentin CHEMIERE -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################### - from . import abstract_fs from . import ftp from . import sftp diff --git a/external_file_location/tasks/abstract_fs.py b/external_file_location/tasks/abstract_fs.py index 05b8569c6..d8798e7a0 100644 --- a/external_file_location/tasks/abstract_fs.py +++ b/external_file_location/tasks/abstract_fs.py @@ -1,23 +1,7 @@ -# -*- coding: utf-8 -*- -############################################################################## -# +# coding: utf-8 # Copyright (C) 2014 initOS GmbH & Co. KG (). -# @author Valentin CHEMIERE -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## +# @ 2015 Valentin CHEMIERE @ Akretion +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from ..abstract_task import AbstractTask import logging diff --git a/external_file_location/tasks/filestore.py b/external_file_location/tasks/filestore.py index 5dd36db98..24e51e783 100644 --- a/external_file_location/tasks/filestore.py +++ b/external_file_location/tasks/filestore.py @@ -1,23 +1,7 @@ -# -*- coding: utf-8 -*- -############################################################################## -# +# coding: utf-8 # Copyright (C) 2014 initOS GmbH & Co. KG (). -# @author Valentin CHEMIERE -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## +# @ 2015 Valentin CHEMIERE @ Akretion +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from .abstract_fs import AbstractFSTask from base64 import b64decode diff --git a/external_file_location/tasks/ftp.py b/external_file_location/tasks/ftp.py index 3b4617292..aa01778d5 100644 --- a/external_file_location/tasks/ftp.py +++ b/external_file_location/tasks/ftp.py @@ -1,23 +1,7 @@ -# -*- coding: utf-8 -*- -############################################################################## -# +# coding: utf-8 # Copyright (C) 2014 initOS GmbH & Co. KG (). -# @author Valentin CHEMIERE -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## +# @ 2015 Valentin CHEMIERE @ Akretion +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from .abstract_fs import AbstractFSTask from base64 import b64decode @@ -43,19 +27,19 @@ class FtpImportTask(FtpTask): def run(self): att_ids = [] - with ftpfs.FTPFS(self.host, self.user, self.pwd, - port=self.port) as ftp_conn: + with ftpfs.FTPFS( + self.host, self.user, self.pwd, port=self.port) as ftp_conn: files_to_process = self._get_files(ftp_conn, self.path) for file_to_process in files_to_process: att_ids.append(self._process_file(ftp_conn, file_to_process)) return att_ids + class FtpExportTask(FtpTask): _synchronize_type = 'export' def run(self, async=True): - import ipdb; ipdb.set_trace() for attachment in self.attachment_ids: if attachment.state in ('pending', 'failed'): self.attachment_id = attachment diff --git a/external_file_location/tasks/sftp.py b/external_file_location/tasks/sftp.py index 41944d65b..9d57f2e92 100644 --- a/external_file_location/tasks/sftp.py +++ b/external_file_location/tasks/sftp.py @@ -1,23 +1,7 @@ -# -*- coding: utf-8 -*- -############################################################################## -# +# coding: utf-8 # Copyright (C) 2014 initOS GmbH & Co. KG (). -# @author Valentin CHEMIERE -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## +# @ 2015 Valentin CHEMIERE @ Akretion +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from .abstract_fs import AbstractFSTask from base64 import b64decode diff --git a/external_file_location/tests/__init__.py b/external_file_location/tests/__init__.py index d3d937e89..af79fd981 100644 --- a/external_file_location/tests/__init__.py +++ b/external_file_location/tests/__init__.py @@ -1,24 +1,2 @@ -# -*- coding: utf-8 -*- -############################################################################### -# -# Module for OpenERP -# Copyright (C) 2015 Akretion (http://www.akretion.com). -# @author Valentin CHEMIERE -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################### - from . import mock_server from . import test_sftp diff --git a/external_file_location/tests/mock_server.py b/external_file_location/tests/mock_server.py index 905893380..5589b7399 100644 --- a/external_file_location/tests/mock_server.py +++ b/external_file_location/tests/mock_server.py @@ -1,23 +1,7 @@ -# -*- coding: utf-8 -*- -############################################################################### -# -# Copyright (C) 2015 Akretion (http://www.akretion.com). -# @author Valentin CHEMIERE -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################### +# coding: utf-8 +# Copyright (C) 2014 initOS GmbH & Co. KG (). +# @ 2015 Valentin CHEMIERE @ Akretion +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). import mock from contextlib import contextmanager diff --git a/external_file_location/tests/test_sftp.py b/external_file_location/tests/test_sftp.py index aedda4c59..281251048 100644 --- a/external_file_location/tests/test_sftp.py +++ b/external_file_location/tests/test_sftp.py @@ -1,24 +1,6 @@ -# -*- coding: utf-8 -*- -############################################################################### -# -# Module for OpenERP -# Copyright (C) 2015 Akretion (http://www.akretion.com). -# @author Valentin CHEMIERE -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################### +# coding: utf-8 +# @ 2015 Valentin CHEMIERE @ Akretion +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). import openerp.tests.common as common from ..tasks.sftp import SftpImportTask