diff --git a/requirements.txt b/requirements.txt index 03e0e03e72..3d91a135c6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,4 +5,5 @@ paramiko pyftpdlib python-magic python_slugify +unicodecsv validators diff --git a/setup/storage_image_import/odoo/addons/storage_image_import b/setup/storage_image_import/odoo/addons/storage_image_import new file mode 120000 index 0000000000..c71818cdf2 --- /dev/null +++ b/setup/storage_image_import/odoo/addons/storage_image_import @@ -0,0 +1 @@ +../../../../storage_image_import \ No newline at end of file diff --git a/setup/storage_image_import/setup.py b/setup/storage_image_import/setup.py new file mode 100644 index 0000000000..28c57bb640 --- /dev/null +++ b/setup/storage_image_import/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) diff --git a/storage_image_import/README.rst b/storage_image_import/README.rst new file mode 100644 index 0000000000..a4582d7fd9 --- /dev/null +++ b/storage_image_import/README.rst @@ -0,0 +1,82 @@ +==================== +Storage Image Import +==================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:f68662397b0281f5612be18799e059c067aaee708405d05a4c799f97a751164f + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fstorage-lightgray.png?logo=github + :target: https://github.com/OCA/storage/tree/14.0/storage_image_import + :alt: OCA/storage +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/storage-14-0/storage-14-0-storage_image_import + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/storage&target_branch=14.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module allows to import images based on url (from csv file) + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +In a csv file, for each image per line only the field "imported_from_url" is mandatory. Since, the name is computed from the url, it could be usefull to add the "alt_name" field as a column. + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Akretion + +Contributors +~~~~~~~~~~~~ + +* Sébastien Beau +* Kévin Roche + +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +This module is part of the `OCA/storage `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/storage_image_import/__init__.py b/storage_image_import/__init__.py new file mode 100644 index 0000000000..0650744f6b --- /dev/null +++ b/storage_image_import/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/storage_image_import/__manifest__.py b/storage_image_import/__manifest__.py new file mode 100644 index 0000000000..ab9ded3c55 --- /dev/null +++ b/storage_image_import/__manifest__.py @@ -0,0 +1,20 @@ +# Copyright 2021 Akretion (https://www.akretion.com). +# @author Sébastien BEAU +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + + +{ + "name": "Storage Image Import", + "summary": "Add the possibility to import image for csv base on url", + "version": "15.0.1.0.0", + "category": "Storage", + "website": "https://github.com/OCA/storage", + "author": " Akretion, Odoo Community Association (OCA)", + "license": "AGPL-3", + "application": False, + "installable": True, + "external_dependencies": { + "python": ["unicodecsv"], + }, + "depends": ["storage_image"], +} diff --git a/storage_image_import/i18n/storage_image_import.pot b/storage_image_import/i18n/storage_image_import.pot new file mode 100644 index 0000000000..8b8f09d070 --- /dev/null +++ b/storage_image_import/i18n/storage_image_import.pot @@ -0,0 +1,54 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * storage_image_import +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 14.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: storage_image_import +#: model:ir.model.fields,field_description:storage_image_import.field_image_relation_abstract__display_name +#: model:ir.model.fields,field_description:storage_image_import.field_storage_image__display_name +msgid "Display Name" +msgstr "" + +#. module: storage_image_import +#: code:addons/storage_image_import/models/image_relation_abstract.py:0 +#, python-format +msgid "Fail to import image {} check if the url is valid" +msgstr "" + +#. module: storage_image_import +#: model:ir.model.fields,field_description:storage_image_import.field_image_relation_abstract__id +#: model:ir.model.fields,field_description:storage_image_import.field_storage_image__id +msgid "ID" +msgstr "" + +#. module: storage_image_import +#: model:ir.model,name:storage_image_import.model_image_relation_abstract +msgid "Image Relation Abstract" +msgstr "" + +#. module: storage_image_import +#: model:ir.model.fields,field_description:storage_image_import.field_image_relation_abstract__import_from_url +#: model:ir.model.fields,field_description:storage_image_import.field_storage_image__imported_from_url +msgid "Imported From Url" +msgstr "" + +#. module: storage_image_import +#: model:ir.model.fields,field_description:storage_image_import.field_image_relation_abstract____last_update +#: model:ir.model.fields,field_description:storage_image_import.field_storage_image____last_update +msgid "Last Modified on" +msgstr "" + +#. module: storage_image_import +#: model:ir.model,name:storage_image_import.model_storage_image +msgid "Storage Image" +msgstr "" diff --git a/storage_image_import/models/__init__.py b/storage_image_import/models/__init__.py new file mode 100644 index 0000000000..afdeb71ca5 --- /dev/null +++ b/storage_image_import/models/__init__.py @@ -0,0 +1,2 @@ +from . import image_relation_abstract +from . import storage_image diff --git a/storage_image_import/models/image_relation_abstract.py b/storage_image_import/models/image_relation_abstract.py new file mode 100644 index 0000000000..d985e299cb --- /dev/null +++ b/storage_image_import/models/image_relation_abstract.py @@ -0,0 +1,79 @@ +# Copyright 2020 Akretion (https://www.akretion.com). +# @author Sébastien BEAU +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +import base64 +import logging +import os +import urllib +from urllib.parse import urlparse + +from odoo import _, api, fields, models +from odoo.exceptions import ValidationError + +_logger = logging.getLogger(__name__) + + +class ImageRelationAbstract(models.AbstractModel): + _inherit = "image.relation.abstract" + + import_from_url = fields.Char(related="image_id.imported_from_url") + + def _create_image_from_url(self, url): + try: + data = urllib.request.urlopen(url).read() + return self.env["storage.image"].create( + { + "name": os.path.basename(urlparse(url).path), + "data": base64.b64encode(data), + "imported_from_url": url, + } + ) + except Exception as e: + _logger.error(e) + raise ValidationError( + _("Fail to import image {} check if the url is valid").format(url) + ) from e + + def _get_existing_image_from_url(self, url): + return self.env["storage.image"].search([("imported_from_url", "=", url)]) + + def _process_import_from_url(self, vals): + if vals.get("import_from_url"): + url = vals.pop("import_from_url") + image = self._get_existing_image_from_url(url) + if not image: + image = self._create_image_from_url(url) + vals["image_id"] = image.id + + def _get_domain_for_existing_relation(self, vals): + return [] + + def _get_existing_relation(self, vals): + domain = self._get_domain_for_existing_relation(vals) + if domain: + return self.search(domain) + else: + return self + + @api.model_create_multi + def create(self, vals_list): + vals_to_create = [] + records = self + for vals in vals_list: + record = None + if "import_from_url" in vals: + record = self._get_existing_relation(vals) + if record: + vals.pop("import_from_url") + record.write(vals) + records |= record + else: + vals_to_create.append(vals) + for vals in vals_to_create: + self._process_import_from_url(vals) + return records | super().create(vals_to_create) + + def write(self, vals): + self._process_import_from_url(vals) + return super().write(vals) diff --git a/storage_image_import/models/storage_image.py b/storage_image_import/models/storage_image.py new file mode 100644 index 0000000000..a291eda80e --- /dev/null +++ b/storage_image_import/models/storage_image.py @@ -0,0 +1,11 @@ +# Copyright 2020 Akretion (https://www.akretion.com). +# @author Sébastien BEAU +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class StorageImage(models.Model): + _inherit = "storage.image" + + imported_from_url = fields.Char() diff --git a/storage_image_import/readme/CONTRIBUTORS.rst b/storage_image_import/readme/CONTRIBUTORS.rst new file mode 100644 index 0000000000..2491c3d62c --- /dev/null +++ b/storage_image_import/readme/CONTRIBUTORS.rst @@ -0,0 +1,2 @@ +* Sébastien Beau +* Kévin Roche diff --git a/storage_image_import/readme/DESCRIPTION.rst b/storage_image_import/readme/DESCRIPTION.rst new file mode 100644 index 0000000000..5d1e74e237 --- /dev/null +++ b/storage_image_import/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +This module allows to import images based on url (from csv file) diff --git a/storage_image_import/readme/USAGE.rst b/storage_image_import/readme/USAGE.rst new file mode 100644 index 0000000000..39399bcfb4 --- /dev/null +++ b/storage_image_import/readme/USAGE.rst @@ -0,0 +1 @@ +In a csv file, for each image per line only the field "imported_from_url" is mandatory. Since, the name is computed from the url, it could be usefull to add the "alt_name" field as a column. diff --git a/storage_image_import/static/description/icon.png b/storage_image_import/static/description/icon.png new file mode 100644 index 0000000000..3a0328b516 Binary files /dev/null and b/storage_image_import/static/description/icon.png differ diff --git a/storage_image_import/static/description/index.html b/storage_image_import/static/description/index.html new file mode 100644 index 0000000000..1c2e533669 --- /dev/null +++ b/storage_image_import/static/description/index.html @@ -0,0 +1,427 @@ + + + + + + +Storage Image Import + + + +
+

Storage Image Import

+ + +

Beta License: AGPL-3 OCA/storage Translate me on Weblate Try me on Runboat

+

This module allows to import images based on url (from csv file)

+

Table of contents

+ +
+

Usage

+

In a csv file, for each image per line only the field “imported_from_url” is mandatory. Since, the name is computed from the url, it could be usefull to add the “alt_name” field as a column.

+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Akretion
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

This module is part of the OCA/storage project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/storage_image_import/tests/__init__.py b/storage_image_import/tests/__init__.py new file mode 100644 index 0000000000..281408ca63 --- /dev/null +++ b/storage_image_import/tests/__init__.py @@ -0,0 +1 @@ +from . import test_import diff --git a/storage_image_import/tests/models/__init__.py b/storage_image_import/tests/models/__init__.py new file mode 100644 index 0000000000..1d4641a1d1 --- /dev/null +++ b/storage_image_import/tests/models/__init__.py @@ -0,0 +1 @@ +from . import fake_image_relation diff --git a/storage_image_import/tests/models/fake_image_relation.py b/storage_image_import/tests/models/fake_image_relation.py new file mode 100644 index 0000000000..21d0678443 --- /dev/null +++ b/storage_image_import/tests/models/fake_image_relation.py @@ -0,0 +1,11 @@ +# Copyright 2021 Camptocamp (http://www.camptocamp.com). +# @author Iván Todorovich +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import models + + +class FakeImageRelation(models.Model): + _name = "fake.image.relation" + _inherit = ["image.relation.abstract"] + _description = "Fake Image Relation model used in tests" diff --git a/storage_image_import/tests/static/akretion-logo.png b/storage_image_import/tests/static/akretion-logo.png new file mode 100644 index 0000000000..12b824c034 Binary files /dev/null and b/storage_image_import/tests/static/akretion-logo.png differ diff --git a/storage_image_import/tests/static/oca-logo.png b/storage_image_import/tests/static/oca-logo.png new file mode 100644 index 0000000000..864458411d Binary files /dev/null and b/storage_image_import/tests/static/oca-logo.png differ diff --git a/storage_image_import/tests/test_import.py b/storage_image_import/tests/test_import.py new file mode 100644 index 0000000000..88a581c63a --- /dev/null +++ b/storage_image_import/tests/test_import.py @@ -0,0 +1,72 @@ +# Copyright 2020 Akretion (https://www.akretion.com). +# Copyright 2021 Camptocamp (http://www.camptocamp.com). +# @author Sébastien BEAU +# @author Iván Todorovich +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +import os +import urllib +from contextlib import contextmanager +from unittest.mock import patch + +from odoo_test_helper import FakeModelLoader + +from odoo.addons.storage_image.tests.common import StorageImageCommonCase + + +class ImportImageCase(StorageImageCommonCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + # Load fake image relation model + cls.loader = FakeModelLoader(cls.env, cls.__module__) + cls.loader.backup_registry() + from .models.fake_image_relation import FakeImageRelation + + cls.loader.update_registry((FakeImageRelation,)) + # Create some permissions for our fake model + model = ( + cls.env["ir.model"].sudo().search([("model", "=", "fake.image.relation")]) + ) + cls.env["ir.model.access"].sudo().create( + { + "name": "access.tester", + "model_id": model.id, + "perm_read": 1, + "perm_write": 1, + "perm_create": 1, + "perm_unlink": 1, + } + ) + # Load test image + cls.base_path = os.path.dirname(os.path.abspath(__file__)) + cls.oca_image = cls._create_storage_image_from_file("static/oca-logo.png") + cls.logo_image = cls._create_storage_image_from_file("static/akretion-logo.png") + # Some other data + cls.product_tmpl = cls.env.ref("product.product_product_4_product_template") + + @classmethod + def tearDownClass(cls): + cls.loader.restore_registry() + return super().tearDownClass() + + @contextmanager + def _patched_request(self, filename, base_path=None): + path = base_path or self.base_path + with open(os.path.join(path, filename), "rb") as file: + with patch.object(urllib.request, "urlopen", return_value=file): + yield + + def test_import_from_url(self): + # Case 1: Create image from url + vals = {"import_from_url": "https://www.example.com/oca-logo.png"} + with self._patched_request("static/oca-logo.png"): + relation = self.env["fake.image.relation"].create(vals) + self.assertEqual(relation.image_id.name, self.oca_image.name) + self.assertEqual(relation.image_id.data, self.oca_image.data) + # Case 2: Write image from url + vals = {"import_from_url": "https://www.example.com/akretion-logo.png"} + with self._patched_request("static/akretion-logo.png"): + relation.write(vals) + self.assertEqual(relation.image_id.name, self.logo_image.name) + self.assertEqual(relation.image_id.data, self.logo_image.data)