From b55855816d818de75cbac0e01ea2b7aafd357a02 Mon Sep 17 00:00:00 2001 From: Vladislav Shepilov Date: Fri, 8 Nov 2019 16:44:32 +0300 Subject: [PATCH] [MIG] base_multi_image: Migration to 12.0 --- base_multi_image/README.rst | 4 +- base_multi_image/__manifest__.py | 2 +- base_multi_image/models/image.py | 36 +++++++------ base_multi_image/models/owner.py | 1 + base_multi_image/readme/CONFIGURE.rst | 66 ++++++++++++++++++++++++ base_multi_image/readme/CONTRIBUTORS.rst | 6 +++ base_multi_image/readme/CREDITS.rst | 4 ++ base_multi_image/readme/DESCRIPTION.rst | 2 + base_multi_image/readme/INSTALL.rst | 4 ++ base_multi_image/readme/ROADMAP.rst | 3 ++ base_multi_image/views/image_view.xml | 14 ++--- 11 files changed, 118 insertions(+), 24 deletions(-) create mode 100644 base_multi_image/readme/CONFIGURE.rst create mode 100644 base_multi_image/readme/CONTRIBUTORS.rst create mode 100644 base_multi_image/readme/CREDITS.rst create mode 100644 base_multi_image/readme/DESCRIPTION.rst create mode 100644 base_multi_image/readme/INSTALL.rst create mode 100644 base_multi_image/readme/ROADMAP.rst diff --git a/base_multi_image/README.rst b/base_multi_image/README.rst index c7d557683..ad9d53064 100644 --- a/base_multi_image/README.rst +++ b/base_multi_image/README.rst @@ -58,8 +58,8 @@ To develop a module based on this one: * If the model you are extending already had an image field, and you want to trick Odoo to make those images to multi-image mode, you will need to make - use of the provided :meth:`~.hooks.pre_init_hook_for_submodules` and - :meth:`~.hooks.uninstall_hook_for_submodules`, like the + use of the provided `~.hooks.pre_init_hook_for_submodules` and + `~.hooks.uninstall_hook_for_submodules`, like the ``product_multi_image`` module does:: try: diff --git a/base_multi_image/__manifest__.py b/base_multi_image/__manifest__.py index 12dd7af96..b95d15f0e 100644 --- a/base_multi_image/__manifest__.py +++ b/base_multi_image/__manifest__.py @@ -8,7 +8,7 @@ { "name": "Multiple images base", "summary": "Allow multiple images for database objects", - "version": "10.0.1.0.0", + "version": "12.0.1.0.0", "author": "Tecnativa, " "Antiun IngenierĂ­a, S.L., Sodexis, " "LasLabs, " diff --git a/base_multi_image/models/image.py b/base_multi_image/models/image.py index 9876a6653..71b19f94f 100644 --- a/base_multi_image/models/image.py +++ b/base_multi_image/models/image.py @@ -5,7 +5,7 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). import base64 -import urllib +from urllib.request import urlretrieve import os import logging from odoo import models, fields, api, exceptions, _ @@ -17,6 +17,7 @@ _logger = logging.getLogger(__name__) class Image(models.Model): _name = "base_multi_image.image" _order = "sequence, owner_model, owner_id, id" + _description = """ image model for multiple image functionality """ _sql_constraints = [ ('uniq_name_owner', 'UNIQUE(owner_id, owner_model, name)', _('A document can have only one image with the same name.')), @@ -93,7 +94,8 @@ class Image(models.Model): def _compute_owner_ref_id(self): """Get a reference field based on the split model and id fields.""" for s in self: - s.owner_ref_id = "{0.owner_model},{0.owner_id}".format(s) + if s.owner_model: + s.owner_ref_id = "{0.owner_model},{0.owner_id}".format(s) @api.multi @api.depends('storage', 'path', 'file_db_store', 'url') @@ -142,7 +144,7 @@ class Image(models.Model): """Allow to download an image and cache it by its URL.""" if url: try: - (filename, header) = urllib.urlretrieve(url) + (filename, header) = urlretrieve(url) with open(filename, 'rb') as f: return base64.b64encode(f.read()) except: @@ -194,24 +196,28 @@ class Image(models.Model): @api.constrains('storage', 'url') def _check_url(self): - if self.storage == 'url' and not self.url: - raise exceptions.ValidationError( - _('You must provide an URL for the image.')) + for record in self: + if record.storage == 'url' and not record.url: + raise exceptions.ValidationError( + _('You must provide an URL for the image.')) @api.constrains('storage', 'path') def _check_path(self): - if self.storage == 'file' and not self.path: - raise exceptions.ValidationError( - _('You must provide a file path for the image.')) + for record in self: + if record.storage == 'file' and not record.path: + raise exceptions.ValidationError( + _('You must provide a file path for the image.')) @api.constrains('storage', 'file_db_store') def _check_store(self): - if self.storage == 'db' and not self.file_db_store: - raise exceptions.ValidationError( - _('You must provide an attached file for the image.')) + for record in self: + if record.storage == 'db' and not record.file_db_store: + raise exceptions.ValidationError( + _('You must provide an attached file for the image.')) @api.constrains('storage', 'attachment_id') def _check_attachment_id(self): - if self.storage == 'filestore' and not self.attachment_id: - raise exceptions.ValidationError( - _('You must provide an attachment for the image.')) + for record in self: + if record.storage == 'filestore' and not record.attachment_id: + raise exceptions.ValidationError( + _('You must provide an attachment for the image.')) diff --git a/base_multi_image/models/owner.py b/base_multi_image/models/owner.py index 11e5e90ce..89c55d708 100644 --- a/base_multi_image/models/owner.py +++ b/base_multi_image/models/owner.py @@ -9,6 +9,7 @@ from odoo import _, api, fields, models, tools class Owner(models.AbstractModel): _name = "base_multi_image.owner" + _description = """ Wizard for base multi image """ image_ids = fields.One2many( comodel_name='base_multi_image.image', diff --git a/base_multi_image/readme/CONFIGURE.rst b/base_multi_image/readme/CONFIGURE.rst new file mode 100644 index 000000000..6b3c4b0d7 --- /dev/null +++ b/base_multi_image/readme/CONFIGURE.rst @@ -0,0 +1,66 @@ +To manage all stored images, you need to: + +* Go to *Settings > Technical > Multi images*. + +... but you probably prefer to manage them from the forms supplied by +submodules that inherit this behavior. + +Development +=========== + +To develop a module based on this one: + +* See module ``product_multi_image`` as an example. + +* You have to inherit model ``base_multi_image.owner`` to the model that needs + the gallery:: + + class MyOwner(models.Model): + _name = "my.model.name" + _inherit = ["my.model.name", "base_multi_image.owner"] + + # If you need this, you will need ``pre_init_hook_for_submodules`` and + ``uninstall_hook_for_submodules`` as detailed below. + old_image_field = fields.Binary(related="image_main", store=False) + +* Somewhere in the owner view, add:: + + + +* If the model you are extending already had an image field, and you want to + trick Odoo to make those images to multi-image mode, you will need to make + use of the provided `~.hooks.pre_init_hook_for_submodules` and + `~.hooks.uninstall_hook_for_submodules`, like the + ``product_multi_image`` module does:: + + try: + from odoo.addons.base_multi_image.hooks import ( + pre_init_hook_for_submodules, + uninstall_hook_for_submodules, + ) + except ImportError: + pass + + + def pre_init_hook(cr): + """Transform single into multi images.""" + pre_init_hook_for_submodules(cr, "product.template", "image") + pre_init_hook_for_submodules(cr, "product.product", "image_variant") + + + def uninstall_hook(cr, registry): + """Remove multi images for models that no longer use them.""" + uninstall_hook_for_submodules(cr, registry, "product.template") + uninstall_hook_for_submodules(cr, registry, "product.product") + + +.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas + :alt: Try me on Runbot + :target: https://runbot.odoo-community.org/runbot/149/10.0 diff --git a/base_multi_image/readme/CONTRIBUTORS.rst b/base_multi_image/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..71fa9ab0b --- /dev/null +++ b/base_multi_image/readme/CONTRIBUTORS.rst @@ -0,0 +1,6 @@ +* Pedro M. Baeza +* Rafael Blasco +* Jairo Llopis +* Sodexis +* Dave Lasley +* Shepilov Vladislav diff --git a/base_multi_image/readme/CREDITS.rst b/base_multi_image/readme/CREDITS.rst new file mode 100644 index 000000000..3d3c26198 --- /dev/null +++ b/base_multi_image/readme/CREDITS.rst @@ -0,0 +1,4 @@ +Original implementation +----------------------- +This module is inspired in previous module *product_images* from OpenLabs +and Akretion. diff --git a/base_multi_image/readme/DESCRIPTION.rst b/base_multi_image/readme/DESCRIPTION.rst new file mode 100644 index 000000000..656cb78dc --- /dev/null +++ b/base_multi_image/readme/DESCRIPTION.rst @@ -0,0 +1,2 @@ +This module extends the functionality of any model to support multiple images +(a gallery) attached to it and allow you to manage them. diff --git a/base_multi_image/readme/INSTALL.rst b/base_multi_image/readme/INSTALL.rst new file mode 100644 index 000000000..e58d92fb5 --- /dev/null +++ b/base_multi_image/readme/INSTALL.rst @@ -0,0 +1,4 @@ +This module adds abstract models to work on. Its sole purpose is to serve as +base for other modules that implement galleries, so if you install this one +manually you will notice no change. You should install any other module based +on this one and this will get installed automatically. diff --git a/base_multi_image/readme/ROADMAP.rst b/base_multi_image/readme/ROADMAP.rst new file mode 100644 index 000000000..e360a6475 --- /dev/null +++ b/base_multi_image/readme/ROADMAP.rst @@ -0,0 +1,3 @@ +* *OS file* storage mode for images is meant to provide a path where Odoo has + read access and the image is already found, **not for making the module store + images there**. It would be nice to add that feature though. diff --git a/base_multi_image/views/image_view.xml b/base_multi_image/views/image_view.xml index d6551c118..0630346cb 100644 --- a/base_multi_image/views/image_view.xml +++ b/base_multi_image/views/image_view.xml @@ -91,6 +91,7 @@ base_multi_image.image + @@ -102,12 +103,13 @@ style="position: absolute; right: 0; padding: 4px; diplay: inline-block">X