diff --git a/fs_base_multi_image/README.rst b/fs_base_multi_image/README.rst new file mode 100644 index 0000000000..38929e8775 --- /dev/null +++ b/fs_base_multi_image/README.rst @@ -0,0 +1,35 @@ +**This file is going to be generated by oca-gen-addon-readme.** + +*Manual changes will be overwritten.* + +Please provide content in the ``readme`` directory: + +* **DESCRIPTION.rst** (required) +* INSTALL.rst (optional) +* CONFIGURE.rst (optional) +* **USAGE.rst** (optional, highly recommended) +* DEVELOP.rst (optional) +* ROADMAP.rst (optional) +* HISTORY.rst (optional, recommended) +* **CONTRIBUTORS.rst** (optional, highly recommended) +* CREDITS.rst (optional) + +Content of this README will also be drawn from the addon manifest, +from keys such as name, authors, maintainers, development_status, +and license. + +A good, one sentence summary in the manifest is also highly recommended. + + +Automatic changelog generation +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +`HISTORY.rst` can be auto generated using `towncrier `_. + +Just put towncrier compatible changelog fragments into `readme/newsfragments` +and the changelog file will be automatically generated and updated when a new fragment is added. + +Please refer to `towncrier` documentation to know more. + +NOTE: the changelog will be automatically generated when using `/ocabot merge $option`. +If you need to run it manually, refer to `OCA/maintainer-tools README `_. diff --git a/fs_base_multi_image/__init__.py b/fs_base_multi_image/__init__.py new file mode 100644 index 0000000000..0650744f6b --- /dev/null +++ b/fs_base_multi_image/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/fs_base_multi_image/__manifest__.py b/fs_base_multi_image/__manifest__.py new file mode 100644 index 0000000000..14a8b2686e --- /dev/null +++ b/fs_base_multi_image/__manifest__.py @@ -0,0 +1,24 @@ +# Copyright 2023 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +{ + "name": "Fs Base Multi Image", + "summary": """ + Mulitple Images from External File System""", + "version": "16.0.1.0.0", + "license": "AGPL-3", + "author": "ACSONE SA/NV,Odoo Community Association (OCA)", + "website": "https://github.com/OCA/storage", + "depends": [ + "fs_image", + ], + "data": [ + "security/res_groups.xml", + "security/fs_image.xml", + "views/fs_image.xml", + "views/fs_image_relation_mixin.xml", + ], + "demo": [], + "maintainers": ["lmignon"], + "development_status": "Alpha", +} diff --git a/fs_base_multi_image/models/__init__.py b/fs_base_multi_image/models/__init__.py new file mode 100644 index 0000000000..bada9ccdd2 --- /dev/null +++ b/fs_base_multi_image/models/__init__.py @@ -0,0 +1,2 @@ +from . import fs_image +from . import fs_image_relation_mixin diff --git a/fs_base_multi_image/models/fs_image.py b/fs_base_multi_image/models/fs_image.py new file mode 100644 index 0000000000..285995ae26 --- /dev/null +++ b/fs_base_multi_image/models/fs_image.py @@ -0,0 +1,29 @@ +# Copyright 2023 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import api, fields, models + +from odoo.addons.fs_image import fields as fs_fields + + +class FsImage(models.Model): + + _name = "fs.image" + _inherit = "fs.image.mixin" + _description = "Image" + _order = "name, id" + _rec_name = "name" + + image = fs_fields.FSImage(required=True) # makes field required + name = fields.Char(compute="_compute_name", store=True, index=True) + mimetype = fields.Char(compute="_compute_mimetype", store=True) + + @api.depends("image") + def _compute_name(self): + for record in self: + record.name = record.image.name if record.image else None + + @api.depends("image") + def _compute_mimetypes(self): + for record in self: + record.mimetype = record.image.mimetype if record.image else None diff --git a/fs_base_multi_image/models/fs_image_relation_mixin.py b/fs_base_multi_image/models/fs_image_relation_mixin.py new file mode 100644 index 0000000000..93181752c9 --- /dev/null +++ b/fs_base_multi_image/models/fs_image_relation_mixin.py @@ -0,0 +1,101 @@ +# Copyright 2023 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import _, api, fields, models +from odoo.exceptions import ValidationError + +from odoo.addons.fs_image import fields as fs_fields + + +class FsImageRelationMixin(models.AbstractModel): + + _name = "fs.image.relation.mixin" + _description = "Image Relation" + _order = "sequence, name" + _rec_name = "name" + + sequence = fields.Integer() + image_id = fields.Many2one( + comodel_name="fs.image", + string="Linked image", + ) + specific_image = fs_fields.FSImage("Specific Image") + # resized fields stored (as attachment) for performance + specific_image_medium = fs_fields.FSImage( + "Specific Image 128", + related="specific_image", + max_width=128, + max_height=128, + store=True, + ) + link_existing = fields.Boolean(default=False) + + image = fs_fields.FSImage( + "Image", compute="_compute_image", inverse="_inverse_image", store=False + ) + # resized fields stored (as attachment) for performance + image_medium = fs_fields.FSImage( + "Image 128", compute="_compute_image_medium", store=False + ) + + name = fields.Char(compute="_compute_name", store=True, index=True) + mimetype = fields.Char(compute="_compute_mimetype", store=True) + + @api.constrains("specific_image", "image_id") + def _check_image(self): + for record in self: + if not record.image_id and not record.specific_image: + raise ValidationError(_("You must set an image")) + + @api.depends("image") + def _compute_name(self): + for record in self: + record.name = record.image.name if record.image else None + + @api.depends("image") + def _compute_mimetypes(self): + for record in self: + record.mimetype = record.image.mimetype if record.image else None + + @api.depends("image_id", "specific_image", "link_existing") + def _compute_image(self): + for record in self: + if record.link_existing: + record.image = record.image_id.image + else: + record.image = record.specific_image + + @api.depends("image_id", "specific_image", "link_existing") + def _compute_image_medium(self): + for record in self: + if record.link_existing: + record.image_medium = record.image_id.image_medium + else: + record.image_medium = record.specific_image_medium + + def _inverse_image(self): + for record in self: + if record.link_existing: + raise ValueError(_("Cannot set image on a linked image")) + else: + record.specific_image = record.image + + @api.model + def _cleanup_vals(self, vals): + if ( + "link_existing" in vals + and vals["link_existing"] + and "specific_image" in vals + ): + vals["specific_image"] = False + return vals + + @api.model_create_multi + def create(self, vals_list): + for vals in vals_list: + self._cleanup_vals(vals) + return super().create(vals_list) + + def write(self, vals): + self._cleanup_vals(vals) + return super().write(vals) diff --git a/fs_base_multi_image/readme/CONTRIBUTORS.rst b/fs_base_multi_image/readme/CONTRIBUTORS.rst new file mode 100644 index 0000000000..172b2d223c --- /dev/null +++ b/fs_base_multi_image/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Laurent Mignon diff --git a/fs_base_multi_image/readme/DESCRIPTION.rst b/fs_base_multi_image/readme/DESCRIPTION.rst new file mode 100644 index 0000000000..3690804632 --- /dev/null +++ b/fs_base_multi_image/readme/DESCRIPTION.rst @@ -0,0 +1,14 @@ +This addon is a technical addon providing a set of models to ease the +creation of other models that need to be linked to multiple images stored +into external filesystems. + +The models provided by this addon are: + +* ``fs.image``: a model that stores a reference to an image stored into + an external filesystem. +* ``fs.image.relation.mixin``: an abstract model that can be used to + as base class for models created to store an image linked to a model. + This abstract model defines fields and methods to transparently handle + 2 cases: + * the image is specific to the model. + * the image is shared between multiple models and therefore is a ``fs.image`` instance linked to the mixin. diff --git a/fs_base_multi_image/readme/ROADMAP.rst b/fs_base_multi_image/readme/ROADMAP.rst new file mode 100644 index 0000000000..776f992b58 --- /dev/null +++ b/fs_base_multi_image/readme/ROADMAP.rst @@ -0,0 +1,2 @@ +* Add dedicated widget to ease the addition of new images to a model linked to + multiple images. (As it's the case in the *storage_image_product* addon) diff --git a/fs_base_multi_image/readme/USAGE.rst b/fs_base_multi_image/readme/USAGE.rst new file mode 100644 index 0000000000..7a4d226aa3 --- /dev/null +++ b/fs_base_multi_image/readme/USAGE.rst @@ -0,0 +1,3 @@ +To be able to create and or manages shared images, you must have the ``Image Manager`` +role. If you do not have this role, as an authenticated user, you can +only view the shared images. diff --git a/fs_base_multi_image/security/fs_image.xml b/fs_base_multi_image/security/fs_image.xml new file mode 100644 index 0000000000..b976e34f28 --- /dev/null +++ b/fs_base_multi_image/security/fs_image.xml @@ -0,0 +1,26 @@ + + + + + + fs.image access read + + + + + + + + + + fs.image access manage + + + + + + + + + diff --git a/fs_base_multi_image/security/res_groups.xml b/fs_base_multi_image/security/res_groups.xml new file mode 100644 index 0000000000..0986498120 --- /dev/null +++ b/fs_base_multi_image/security/res_groups.xml @@ -0,0 +1,10 @@ + + + + Image Manager + + + diff --git a/fs_base_multi_image/static/description/icon.png b/fs_base_multi_image/static/description/icon.png new file mode 100644 index 0000000000..3a0328b516 Binary files /dev/null and b/fs_base_multi_image/static/description/icon.png differ diff --git a/fs_base_multi_image/views/fs_image.xml b/fs_base_multi_image/views/fs_image.xml new file mode 100644 index 0000000000..26347cbab9 --- /dev/null +++ b/fs_base_multi_image/views/fs_image.xml @@ -0,0 +1,77 @@ + + + + + + fs.image.form (in fs_base_multi_image) + fs.image + +
+ + +
+
+
+ + + fs.image.search (in fs_base_multi_image) + fs.image + + + + + + + + + + + + + + + fs.image.tree (in fs_base_multi_image) + fs.image + + + + + + + + + + + Fs Image + fs.image + tree,form + [] + {} + + + + Fs Images + + + + +
diff --git a/fs_base_multi_image/views/fs_image_relation_mixin.xml b/fs_base_multi_image/views/fs_image_relation_mixin.xml new file mode 100644 index 0000000000..27bc8f3a3c --- /dev/null +++ b/fs_base_multi_image/views/fs_image_relation_mixin.xml @@ -0,0 +1,42 @@ + + + + + + fs.image.relation.mixin.form + fs.image.relation.mixin + +
+ + + + + + + + + + + + + +
+
+
+ +
diff --git a/fs_product_multi_image/README.rst b/fs_product_multi_image/README.rst new file mode 100644 index 0000000000..38929e8775 --- /dev/null +++ b/fs_product_multi_image/README.rst @@ -0,0 +1,35 @@ +**This file is going to be generated by oca-gen-addon-readme.** + +*Manual changes will be overwritten.* + +Please provide content in the ``readme`` directory: + +* **DESCRIPTION.rst** (required) +* INSTALL.rst (optional) +* CONFIGURE.rst (optional) +* **USAGE.rst** (optional, highly recommended) +* DEVELOP.rst (optional) +* ROADMAP.rst (optional) +* HISTORY.rst (optional, recommended) +* **CONTRIBUTORS.rst** (optional, highly recommended) +* CREDITS.rst (optional) + +Content of this README will also be drawn from the addon manifest, +from keys such as name, authors, maintainers, development_status, +and license. + +A good, one sentence summary in the manifest is also highly recommended. + + +Automatic changelog generation +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +`HISTORY.rst` can be auto generated using `towncrier `_. + +Just put towncrier compatible changelog fragments into `readme/newsfragments` +and the changelog file will be automatically generated and updated when a new fragment is added. + +Please refer to `towncrier` documentation to know more. + +NOTE: the changelog will be automatically generated when using `/ocabot merge $option`. +If you need to run it manually, refer to `OCA/maintainer-tools README `_. diff --git a/fs_product_multi_image/__init__.py b/fs_product_multi_image/__init__.py new file mode 100644 index 0000000000..0650744f6b --- /dev/null +++ b/fs_product_multi_image/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/fs_product_multi_image/__manifest__.py b/fs_product_multi_image/__manifest__.py new file mode 100644 index 0000000000..43d7a70013 --- /dev/null +++ b/fs_product_multi_image/__manifest__.py @@ -0,0 +1,25 @@ +# Copyright 2023 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +{ + "name": "Fs Product Multi Image", + "summary": """ + Manage multi images from extenal file system on product""", + "version": "16.0.1.0.0", + "license": "AGPL-3", + "author": "ACSONE SA/NV,Odoo Community Association (OCA)", + "website": "https://github.com/OCA/storage", + "depends": ["fs_base_multi_image", "product", "sales_team", "image_tag"], + "data": [ + "security/fs_product_category_image.xml", + "views/fs_product_category_image.xml", + "views/product_category.xml", + "security/fs_product_image.xml", + "views/fs_product_image.xml", + "views/product_product.xml", + "views/product_template.xml", + ], + "demo": [], + "maintainers": ["lmignon"], + "development_status": "Alpha", +} diff --git a/fs_product_multi_image/models/__init__.py b/fs_product_multi_image/models/__init__.py new file mode 100644 index 0000000000..17c75c0abd --- /dev/null +++ b/fs_product_multi_image/models/__init__.py @@ -0,0 +1,7 @@ +from . import fs_product_category_image +from . import fs_product_image +from . import image_tag +from . import product_category +from . import product_template +from . import product_product +from . import product_template_attribute_line diff --git a/fs_product_multi_image/models/fs_product_category_image.py b/fs_product_multi_image/models/fs_product_category_image.py new file mode 100644 index 0000000000..d0075cfd68 --- /dev/null +++ b/fs_product_multi_image/models/fs_product_category_image.py @@ -0,0 +1,24 @@ +# Copyright 2023 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class FsProductCategoryImage(models.Model): + _name = "fs.product.category.image" + _inherit = "fs.image.relation.mixin" + _description = "Product Category Image" + + product_categ_id = fields.Many2one( + comodel_name="product.category", + string="Product Category", + ondelete="cascade", + index=True, + ) + + tag_id = fields.Many2one( + "image.tag", + string="Tag", + domain=[("apply_on", "=", "category")], + index=True, + ) diff --git a/fs_product_multi_image/models/fs_product_image.py b/fs_product_multi_image/models/fs_product_image.py new file mode 100644 index 0000000000..e5584be3a5 --- /dev/null +++ b/fs_product_multi_image/models/fs_product_image.py @@ -0,0 +1,50 @@ +# Copyright 2023 ACSONE SA/NV +# Copyright 2018 Akretion (http://www.akretion.com). +# @author Raphaël Reverdy +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import api, fields, models + + +class FsProductImage(models.Model): + _name = "fs.product.image" + _inherit = "fs.image.relation.mixin" + _description = "Product Image" + + product_tmpl_id = fields.Many2one( + comodel_name="product.template", + string="Product Template", + ondelete="cascade", + index=True, + ) + attribute_value_ids = fields.Many2many( + "product.attribute.value", + string="Attributes", + domain="[('id', 'in', available_attribute_value_ids)]", + ) + # This field will list all attribute value used by the template + # in order to filter the attribute value available for the current image + available_attribute_value_ids = fields.Many2many( + "product.attribute.value", + string="Available Attributes", + compute="_compute_available_attribute", + ) + tag_id = fields.Many2one( + "image.tag", + string="Tag", + domain=[("apply_on", "=", "product")], + index=True, + ) + + @api.depends("product_tmpl_id.attribute_line_ids.value_ids") + def _compute_available_attribute(self): + for rec in self: + rec.available_attribute_value_ids = rec.product_tmpl_id.mapped( + "attribute_line_ids.value_ids" + ) + + def _match_variant(self, variant): + variant_attribute_values = variant.mapped( + "product_template_attribute_value_ids.product_attribute_value_id" + ) + return not bool(self.attribute_value_ids - variant_attribute_values) diff --git a/fs_product_multi_image/models/image_tag.py b/fs_product_multi_image/models/image_tag.py new file mode 100644 index 0000000000..e608eb21d2 --- /dev/null +++ b/fs_product_multi_image/models/image_tag.py @@ -0,0 +1,27 @@ +# Copyright 2023 ACSONE SA/NV +# Copyright 2018 Akretion (http://www.akretion.com). +# @author Raphaël Reverdy +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + + +from odoo import api, fields, models + + +class ImageTag(models.Model): + _inherit = "image.tag" + + @api.model + def _get_default_apply_on(self): + active_model = self.env.context.get("active_model") + return ( + "product" + if active_model == "product.image.relation" + else "category" + if active_model == "category.image.relation" + else super()._get_default_apply_on() + ) + + apply_on = fields.Selection( + selection_add=[("product", "Product"), ("category", "Category")], + ondelete={"product": "cascade", "category": "cascade"}, + ) diff --git a/fs_product_multi_image/models/product_category.py b/fs_product_multi_image/models/product_category.py new file mode 100644 index 0000000000..e40c91a35a --- /dev/null +++ b/fs_product_multi_image/models/product_category.py @@ -0,0 +1,19 @@ +# Copyright 2023 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + +from odoo.addons.fs_image.fields import FSImage + + +class ProductCategory(models.Model): + + _inherit = "product.category" + + image_ids = fields.One2many( + string="Images", + comodel_name="fs.product.category.image", + inverse_name="product_categ_id", + ) + image = FSImage(related="image_ids.image", readonly=True, store=False) + image_medium = FSImage(related="image_ids.image_medium", readonly=True, store=False) diff --git a/fs_product_multi_image/models/product_product.py b/fs_product_multi_image/models/product_product.py new file mode 100644 index 0000000000..7ea6e87b29 --- /dev/null +++ b/fs_product_multi_image/models/product_product.py @@ -0,0 +1,68 @@ +# Copyright 2017 Akretion (http://www.akretion.com). +# @author Sébastien BEAU +# Copyright 2021 Camptocamp SA (http://www.camptocamp.com) +# @author Simone Orsi +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +from odoo import api, fields, models + +from odoo.addons.fs_image.fields import FSImage + + +class ProductProduct(models.Model): + _inherit = "product.product" + + variant_image_ids = fields.Many2many( + "fs.product.image", + compute="_compute_variant_image_ids", + store=True, + string="Variant Images", + ) + main_image_id = fields.Many2one( + string="Main Image", + comodel_name="fs.product.image", + compute="_compute_main_image_id", + # Store it to improve perfs + store=True, + ) + image = FSImage(related="main_image_id.image", readonly=True, store=False) + image_medium = FSImage( + related="main_image_id.image_medium", readonly=True, store=False + ) + + @api.depends( + "product_tmpl_id.image_ids", + "product_tmpl_id.image_ids.sequence", + "product_tmpl_id.image_ids.attribute_value_ids", + "product_template_attribute_value_ids", + ) + def _compute_variant_image_ids(self): + for variant in self: + img_relations = set() + # Not sure sorting is needed here + sorted_image_relations = variant.image_ids.sorted( + key=lambda i: (i.sequence, i.id) + ) + for image_rel in sorted_image_relations: + if image_rel._match_variant(variant): + img_relations.add(image_rel.id) + variant.variant_image_ids = list(img_relations) if img_relations else False + + @api.depends("variant_image_ids", "variant_image_ids.sequence") + def _compute_main_image_id(self): + for record in self: + record.main_image_id = record._get_main_image() + + def _select_main_image(self, images): + return fields.first(images.sorted(key=lambda i: (i.sequence, i.id))).id + + def _get_main_image(self): + match_image = self.variant_image_ids.filtered( + lambda i: i.attribute_value_ids + == self.mapped( + "product_template_attribute_value_ids.product_attribute_value_id" + ) + ) + if match_image: + return self._select_main_image(match_image) + return self._select_main_image(self.variant_image_ids) diff --git a/fs_product_multi_image/models/product_template.py b/fs_product_multi_image/models/product_template.py new file mode 100644 index 0000000000..38b749d97a --- /dev/null +++ b/fs_product_multi_image/models/product_template.py @@ -0,0 +1,32 @@ +# Copyright 2023 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import api, fields, models + +from odoo.addons.fs_image.fields import FSImage + + +class ProductTemplate(models.Model): + + _inherit = "product.template" + + image_ids = fields.One2many( + string="Images", comodel_name="fs.product.image", inverse_name="product_tmpl_id" + ) + main_image_id = fields.Many2one( + string="Main Image", + comodel_name="fs.product.image", + compute="_compute_main_image_id", + # Store it to improve perfs + store=True, + ) + image = FSImage(related="main_image_id.image", readonly=True, store=False) + image_medium = FSImage( + related="main_image_id.image_medium", readonly=True, store=False + ) + + @api.depends("image_ids", "image_ids.sequence") + def _compute_main_image_id(self): + for record in self: + image_ids = record.image_ids.sorted(key=lambda i: (i.sequence, i.id)) + record.main_image_id = image_ids and image_ids[0] or None diff --git a/fs_product_multi_image/models/product_template_attribute_line.py b/fs_product_multi_image/models/product_template_attribute_line.py new file mode 100644 index 0000000000..b6b6915066 --- /dev/null +++ b/fs_product_multi_image/models/product_template_attribute_line.py @@ -0,0 +1,31 @@ +# Copyright 2023 ACSONE SA/NV +# Copyright 2017 Akretion (http://www.akretion.com). +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import models + + +class ProductTemplateAttributeLine(models.Model): + + _inherit = "product.template.attribute.line" + + def write(self, values): + res = super().write(values) + if "value_ids" in values: + product_image_attribute_value_ids = self.product_tmpl_id.image_ids.mapped( + "attribute_value_ids" + ).filtered(lambda x: x.attribute_id == self.attribute_id) + available_attribute_values_ids = self.value_ids + to_remove = product_image_attribute_value_ids.filtered( + lambda x: x not in available_attribute_values_ids + ) + if to_remove: + for image in self.product_tmpl_id.image_ids: + image.attribute_value_ids -= to_remove + return res + + def unlink(self): + for line in self: + for image in line.product_tmpl_id.image_ids: + image.attribute_value_ids -= line.value_ids + return super().unlink() diff --git a/fs_product_multi_image/readme/CONTRIBUTORS.rst b/fs_product_multi_image/readme/CONTRIBUTORS.rst new file mode 100644 index 0000000000..33d0863bc0 --- /dev/null +++ b/fs_product_multi_image/readme/CONTRIBUTORS.rst @@ -0,0 +1,7 @@ +* Laurent Mignon +* Raphaël Reverdy +* Denis Roussel +* Quentin Groulard +* `Camptocamp `_ + + * Iván Todorovich diff --git a/fs_product_multi_image/readme/DESCRIPTION.rst b/fs_product_multi_image/readme/DESCRIPTION.rst new file mode 100644 index 0000000000..b1ae446b13 --- /dev/null +++ b/fs_product_multi_image/readme/DESCRIPTION.rst @@ -0,0 +1,4 @@ +Attach images to products and categories and store them on an external +filesystem instead of the database. + +This addon is a drop-in replacement for the **storage_image_product** addon. diff --git a/fs_product_multi_image/readme/USAGE.rst b/fs_product_multi_image/readme/USAGE.rst new file mode 100644 index 0000000000..43c2b473dd --- /dev/null +++ b/fs_product_multi_image/readme/USAGE.rst @@ -0,0 +1,13 @@ +On the category and product form, a new tab allows you to add images to the +related object. The images can be specific to the model or you can use an +existing one. + +On the link forms, you can add an image tag in addition to the image. In +the specific case of the product template, you can also specify for which +variant attribute values the image is valid. + +On the product variant form, the image tag will be automatically filled whith +the image tag of the product template for the same variant attribute values. + +In every case, a main image is computed and used as the default image for the +object. It depends on the sequence of the images (first one is the main one). diff --git a/fs_product_multi_image/security/fs_product_category_image.xml b/fs_product_multi_image/security/fs_product_category_image.xml new file mode 100644 index 0000000000..20c6ec6d6b --- /dev/null +++ b/fs_product_multi_image/security/fs_product_category_image.xml @@ -0,0 +1,34 @@ + + + + + + fs.product.category.image access read + + + + + + + + + fs.product.category.image access system admin + + + + + + + + + fs.product.category.image access sales manager + + + + + + + + + diff --git a/fs_product_multi_image/security/fs_product_image.xml b/fs_product_multi_image/security/fs_product_image.xml new file mode 100644 index 0000000000..4ecbabac5d --- /dev/null +++ b/fs_product_multi_image/security/fs_product_image.xml @@ -0,0 +1,34 @@ + + + + + + fs.product.image access read + + + + + + + + + fs.product.image access erp manager + + + + + + + + + fs.product.image access sales manager + + + + + + + + + diff --git a/fs_product_multi_image/static/description/icon.png b/fs_product_multi_image/static/description/icon.png new file mode 100644 index 0000000000..3a0328b516 Binary files /dev/null and b/fs_product_multi_image/static/description/icon.png differ diff --git a/fs_product_multi_image/tests/__init__.py b/fs_product_multi_image/tests/__init__.py new file mode 100644 index 0000000000..c686b22ff1 --- /dev/null +++ b/fs_product_multi_image/tests/__init__.py @@ -0,0 +1 @@ +from . import test_fs_product_multi_image diff --git a/fs_product_multi_image/tests/test_fs_product_multi_image.py b/fs_product_multi_image/tests/test_fs_product_multi_image.py new file mode 100644 index 0000000000..aabcdd6702 --- /dev/null +++ b/fs_product_multi_image/tests/test_fs_product_multi_image.py @@ -0,0 +1,259 @@ +# Copyright 2017 Akretion (http://www.akretion.com). +# Copyright 2023 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +import base64 +import io + +from PIL import Image + +from odoo.tests.common import TransactionCase + + +class TestFsProductMultiImage(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True)) + cls.white_image = cls._create_image(16, 16, color="#FFFFFF") + cls.black_image = cls._create_image(16, 16, color="#000000") + cls.logo_image = cls._create_image(16, 16, color="#FFA500") + cls.template = cls.env.ref("product.product_product_4_product_template") + cls.product_a = cls.env.ref("product.product_product_4") + cls.product_b = cls.env.ref("product.product_product_4b") + cls.product_c = cls.env.ref("product.product_product_4c") + cls.image_white = cls.env["fs.image"].create( + { + "image": { + "filename": "white.png", + "content": base64.b64encode(cls.white_image), + } + } + ) + cls.image_logo = cls.env["fs.image"].create( + { + "image": { + "filename": "logo.png", + "content": base64.b64encode(cls.logo_image), + } + } + ) + cls.image_black = cls.env["fs.image"].create( + { + "image": { + "filename": "black.png", + "content": base64.b64encode(cls.black_image), + } + } + ) + + def setUp(self): + super().setUp() + self.temp_dir = self.env["fs.storage"].create( + { + "name": "Temp FS Storage", + "protocol": "memory", + "code": "mem_dir", + "directory_path": "/tmp/", + "model_xmlids": "fs_product_multi_image.model_fs_product_category_image," + "fs_product_multi_image.model_fs_product_image", + } + ) + + @classmethod + def _create_image(cls, width, height, color="#4169E1", img_format="PNG"): + f = io.BytesIO() + Image.new("RGB", (width, height), color).save(f, img_format) + f.seek(0) + return f.read() + + def test_available_attribute_value(self): + # The template have already 5 attribute values + # see demo data of ipad + image = self.env["fs.product.image"].new({"product_tmpl_id": self.template.id}) + self.assertEqual(len(image.available_attribute_value_ids), 5) + + def test_add_image_for_all_variant(self): + self.assertEqual(len(self.product_a.variant_image_ids), 0) + image = self.env["fs.product.image"].create( + { + "product_tmpl_id": self.template.id, + "specific_image": { + "filename": "white.png", + "content": base64.b64encode(self.white_image), + }, + } + ) + self.assertEqual(self.product_a.image.getvalue(), self.white_image) + self.assertEqual(self.product_a.variant_image_ids, image) + self.assertEqual(self.product_a.main_image_id, image) + self.assertEqual(self.product_b.image.getvalue(), self.white_image) + self.assertEqual(self.product_b.variant_image_ids, image) + self.assertEqual(self.product_b.main_image_id, image) + self.assertEqual(self.product_c.image.getvalue(), self.white_image) + self.assertEqual(self.product_c.variant_image_ids, image) + self.assertEqual(self.product_c.main_image_id, image) + + def test_add_image_for_white_variant(self): + image = self.env["fs.product.image"].create( + { + "product_tmpl_id": self.template.id, + "image_id": self.image_white.id, + "attribute_value_ids": [ + (6, 0, [self.env.ref("product.product_attribute_value_3").id]) + ], + } + ) + # White product should have the image + self.assertEqual(self.product_a.variant_image_ids, image) + self.assertEqual(self.product_a.main_image_id, image) + self.assertEqual(self.product_c.variant_image_ids, image) + self.assertEqual(self.product_c.main_image_id, image) + # Black product should not have the image + self.assertEqual(len(self.product_b.variant_image_ids), 0) + self.assertFalse(self.product_b.main_image_id) + + def _create_multiple_images(self): + logo = self.env["fs.product.image"].create( + { + "product_tmpl_id": self.template.id, + "image_id": self.image_logo.id, + "sequence": 10, + "link_existing": True, + } + ) + image_wh = self.env["fs.product.image"].create( + { + "product_tmpl_id": self.template.id, + "image_id": self.image_white.id, + "attribute_value_ids": [ + (6, 0, [self.env.ref("product.product_attribute_value_3").id]) + ], + "sequence": 2, + "link_existing": True, + } + ) + image_bk = self.env["fs.product.image"].create( + { + "product_tmpl_id": self.template.id, + "image_id": self.image_black.id, + "attribute_value_ids": [ + (6, 0, [self.env.ref("product.product_attribute_value_4").id]) + ], + "sequence": 1, + "link_existing": True, + } + ) + return logo, image_wh, image_bk + + def test_add_image_for_white_and_black_variant(self): + logo, image_wh, image_bk = self._create_multiple_images() + # White product should have the white image and the logo + self.assertEqual(self.product_a.variant_image_ids, image_wh + logo) + self.assertEqual(self.product_c.variant_image_ids, image_wh + logo) + # Black product should have the black image and the logo + self.assertEqual(self.product_b.variant_image_ids, image_bk + logo) + + def _test_main_images(self, expected): + for image, products in expected: + for prod in products: + self.assertEqual(prod.image.getvalue(), image) + + def test_main_image_and_urls(self): + logo, image_wh, image_bk = self._create_multiple_images() + # Template should have the one w/ lower sequence + expected = ((self.black_image, self.template),) + self._test_main_images(expected) + # Should have different main images + expected = ( + (self.white_image, self.product_a + self.product_c), + (self.black_image, self.product_b), + ) + self._test_main_images(expected) + # Change image order, change main image + logo.sequence = 0 + image_wh.sequence = 10 + expected = ((self.logo_image, self.template),) + self._test_main_images(expected) + expected = ( + (self.logo_image, self.product_a + self.product_c), + (self.logo_image, self.product_b), + ) + self._test_main_images(expected) + + def test_main_image_attribute(self): + """ + Attach the image to the template and check the first image of the + variant is the one with same attributes + """ + self.env["fs.product.image"].create( + { + "product_tmpl_id": self.template.id, + "image_id": self.image_logo.id, + "sequence": 1, + "link_existing": True, + } + ) + self.env["fs.product.image"].create( + { + "product_tmpl_id": self.template.id, + "image_id": self.image_white.id, + "attribute_value_ids": [ + ( + 6, + 0, + [ + self.env.ref("product.product_attribute_value_4").id, + self.env.ref("product.product_attribute_value_1").id, + ], + ) + ], + "sequence": 10, + "link_existing": True, + } + ) + # The variant should not take the only with the lowest sequence but + # the one with same attributes + expected = ((self.white_image, self.product_b),) + self._test_main_images(expected) + expected = ((self.logo_image, self.product_c + self.product_a),) + self._test_main_images(expected) + + def test_drop_template_attribute_value_propagation_to_image(self): + black_image = self.env["fs.product.image"].create( + { + "product_tmpl_id": self.template.id, + "image_id": self.image_black.id, + "attribute_value_ids": [ + ( + 6, + 0, + [ + self.env.ref("product.product_attribute_value_4").id, + self.env.ref("product.product_attribute_value_1").id, + ], + ) + ], + "sequence": 10, + "link_existing": True, + } + ) + # Remove Color black from variant tab: + self.template.attribute_line_ids.sudo().filtered( + lambda x: x.display_name == "Color" + ).value_ids -= self.env.ref("product.product_attribute_value_4") + # Attribute black is removed from image: + self.assertTrue( + self.env.ref("product.product_attribute_value_4") + not in black_image.attribute_value_ids + ) + + # Remove Leg attribute line from variant tab: + self.template.attribute_line_ids.sudo().filtered( + lambda x: x.display_name == "Legs" + ).unlink() + # Product image attribute values from Legs are removed: + self.assertTrue( + self.env.ref("product.product_attribute_value_1") + not in black_image.attribute_value_ids + ) diff --git a/fs_product_multi_image/views/fs_product_category_image.xml b/fs_product_multi_image/views/fs_product_category_image.xml new file mode 100644 index 0000000000..acaba220aa --- /dev/null +++ b/fs_product_multi_image/views/fs_product_category_image.xml @@ -0,0 +1,21 @@ + + + + + + product.category.fs.image.form + fs.product.category.image + + primary + + + + + + + + diff --git a/fs_product_multi_image/views/fs_product_image.xml b/fs_product_multi_image/views/fs_product_image.xml new file mode 100644 index 0000000000..e60cf84bda --- /dev/null +++ b/fs_product_multi_image/views/fs_product_image.xml @@ -0,0 +1,32 @@ + + + + + + fs.product.image.form + fs.product.image + + primary + + + + + + + + + + diff --git a/fs_product_multi_image/views/product_category.xml b/fs_product_multi_image/views/product_category.xml new file mode 100644 index 0000000000..c4e3757fd2 --- /dev/null +++ b/fs_product_multi_image/views/product_category.xml @@ -0,0 +1,35 @@ + + + + + + product.category.form + product.category + + +
+ +
+ + + + + + + + + + + +
+
+ + + +
diff --git a/fs_product_multi_image/views/product_product.xml b/fs_product_multi_image/views/product_product.xml new file mode 100644 index 0000000000..b7534f26ad --- /dev/null +++ b/fs_product_multi_image/views/product_product.xml @@ -0,0 +1,57 @@ + + + + + product.product + + + + + kanban_image('product.product', 'image_medium', record.id.raw_value) + + + + + product.product + + + + 1 + + + + + + +

+ If you need to edit the images, do it from the product template. +

+ +
+
+
+
+ + product.product + + + + 1 + + + + + + +
diff --git a/fs_product_multi_image/views/product_template.xml b/fs_product_multi_image/views/product_template.xml new file mode 100644 index 0000000000..7298ae3583 --- /dev/null +++ b/fs_product_multi_image/views/product_template.xml @@ -0,0 +1,49 @@ + + + + + product.template + + + + + 1 + + + + + + + + + + + + + + + + + + + product.template + + + + + kanban_image('product.template', 'image_medium', record.id.raw_value) + + + + diff --git a/image_tag/README.rst b/image_tag/README.rst new file mode 100644 index 0000000000..38929e8775 --- /dev/null +++ b/image_tag/README.rst @@ -0,0 +1,35 @@ +**This file is going to be generated by oca-gen-addon-readme.** + +*Manual changes will be overwritten.* + +Please provide content in the ``readme`` directory: + +* **DESCRIPTION.rst** (required) +* INSTALL.rst (optional) +* CONFIGURE.rst (optional) +* **USAGE.rst** (optional, highly recommended) +* DEVELOP.rst (optional) +* ROADMAP.rst (optional) +* HISTORY.rst (optional, recommended) +* **CONTRIBUTORS.rst** (optional, highly recommended) +* CREDITS.rst (optional) + +Content of this README will also be drawn from the addon manifest, +from keys such as name, authors, maintainers, development_status, +and license. + +A good, one sentence summary in the manifest is also highly recommended. + + +Automatic changelog generation +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +`HISTORY.rst` can be auto generated using `towncrier `_. + +Just put towncrier compatible changelog fragments into `readme/newsfragments` +and the changelog file will be automatically generated and updated when a new fragment is added. + +Please refer to `towncrier` documentation to know more. + +NOTE: the changelog will be automatically generated when using `/ocabot merge $option`. +If you need to run it manually, refer to `OCA/maintainer-tools README `_. diff --git a/image_tag/__init__.py b/image_tag/__init__.py new file mode 100644 index 0000000000..0650744f6b --- /dev/null +++ b/image_tag/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/image_tag/__manifest__.py b/image_tag/__manifest__.py new file mode 100644 index 0000000000..9fce9bee00 --- /dev/null +++ b/image_tag/__manifest__.py @@ -0,0 +1,19 @@ +# Copyright 2023 ACSONE SA/NV +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +{ + "name": "Image Tag", + "summary": """ + Image tag model""", + "version": "16.0.1.0.0", + "license": "AGPL-3", + "author": "ACSONE SA/NV,Akretion,Odoo Community Association (OCA)", + "website": "https://github.com/OCA/storage", + "depends": ["server_environment", "sale"], # sale is needed for the menu only :-( + "data": [ + "security/res_groups.xml", + "security/image_tag.xml", + "views/image_tag.xml", + ], + "demo": [], +} diff --git a/image_tag/models/__init__.py b/image_tag/models/__init__.py new file mode 100644 index 0000000000..888490ab30 --- /dev/null +++ b/image_tag/models/__init__.py @@ -0,0 +1 @@ +from . import image_tag diff --git a/image_tag/models/image_tag.py b/image_tag/models/image_tag.py new file mode 100644 index 0000000000..86f255873d --- /dev/null +++ b/image_tag/models/image_tag.py @@ -0,0 +1,22 @@ +# Copyright 2023 ACSONE SA/NV +# Copyright 2018 Akretion (http://www.akretion.com). +# @author Raphaël Reverdy +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +from odoo import api, fields, models + + +class ImageTag(models.Model): + _name = "image.tag" + _inherit = ["server.env.techname.mixin"] + _description = "Image Tag" + + @api.model + def _get_default_apply_on(self): + return False + + name = fields.Char(required=True) + apply_on = fields.Selection( + selection=[], + default=lambda self: self._get_default_apply_on(), + ) diff --git a/image_tag/readme/CONTRIBUTORS.rst b/image_tag/readme/CONTRIBUTORS.rst new file mode 100644 index 0000000000..172b2d223c --- /dev/null +++ b/image_tag/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Laurent Mignon diff --git a/image_tag/readme/DESCRIPTION.rst b/image_tag/readme/DESCRIPTION.rst new file mode 100644 index 0000000000..40adead6fe --- /dev/null +++ b/image_tag/readme/DESCRIPTION.rst @@ -0,0 +1,3 @@ +This addon provide only one basic model used to define image's tags. These +tags are used by other addons to enrich the image's information of an image +linked to an other model. The `fs_product_multi_image` addon use this model. diff --git a/image_tag/readme/USAGE.rst b/image_tag/readme/USAGE.rst new file mode 100644 index 0000000000..314932f6fa --- /dev/null +++ b/image_tag/readme/USAGE.rst @@ -0,0 +1,2 @@ +To manage the list of available tags, you must have the ``Image Tag Manager`` +role. diff --git a/image_tag/security/image_tag.xml b/image_tag/security/image_tag.xml new file mode 100644 index 0000000000..61a18fa863 --- /dev/null +++ b/image_tag/security/image_tag.xml @@ -0,0 +1,25 @@ + + + + + + image.tag access read + + + + + + + + + + image.tag access read + + + + + + + + diff --git a/image_tag/security/res_groups.xml b/image_tag/security/res_groups.xml new file mode 100644 index 0000000000..7ba612b045 --- /dev/null +++ b/image_tag/security/res_groups.xml @@ -0,0 +1,10 @@ + + + + Image Tag Manager + + + diff --git a/image_tag/static/description/icon.png b/image_tag/static/description/icon.png new file mode 100644 index 0000000000..3a0328b516 Binary files /dev/null and b/image_tag/static/description/icon.png differ diff --git a/image_tag/views/image_tag.xml b/image_tag/views/image_tag.xml new file mode 100644 index 0000000000..05a7a67489 --- /dev/null +++ b/image_tag/views/image_tag.xml @@ -0,0 +1,43 @@ + + + + image.tag + + + + + + + + + + image.tag + + + + + + + + + Image Tag + ir.actions.act_window + image.tag + tree + + [] + {} + + + + + tree + + + + diff --git a/setup/fs_base_multi_image/odoo/addons/fs_base_multi_image b/setup/fs_base_multi_image/odoo/addons/fs_base_multi_image new file mode 120000 index 0000000000..e4d18ef8c4 --- /dev/null +++ b/setup/fs_base_multi_image/odoo/addons/fs_base_multi_image @@ -0,0 +1 @@ +../../../../fs_base_multi_image \ No newline at end of file diff --git a/setup/fs_base_multi_image/setup.py b/setup/fs_base_multi_image/setup.py new file mode 100644 index 0000000000..28c57bb640 --- /dev/null +++ b/setup/fs_base_multi_image/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) diff --git a/setup/fs_product_multi_image/odoo/addons/fs_product_multi_image b/setup/fs_product_multi_image/odoo/addons/fs_product_multi_image new file mode 120000 index 0000000000..3327030d29 --- /dev/null +++ b/setup/fs_product_multi_image/odoo/addons/fs_product_multi_image @@ -0,0 +1 @@ +../../../../fs_product_multi_image \ No newline at end of file diff --git a/setup/fs_product_multi_image/setup.py b/setup/fs_product_multi_image/setup.py new file mode 100644 index 0000000000..28c57bb640 --- /dev/null +++ b/setup/fs_product_multi_image/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) diff --git a/setup/image_tag/odoo/addons/image_tag b/setup/image_tag/odoo/addons/image_tag new file mode 120000 index 0000000000..f25ed02804 --- /dev/null +++ b/setup/image_tag/odoo/addons/image_tag @@ -0,0 +1 @@ +../../../../image_tag \ No newline at end of file diff --git a/setup/image_tag/setup.py b/setup/image_tag/setup.py new file mode 100644 index 0000000000..28c57bb640 --- /dev/null +++ b/setup/image_tag/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)