Browse Source

[MIG] base_multi_image: Migration to 12.0

12.0-mig-module_prototyper_last
Vladislav Shepilov 5 years ago
parent
commit
b55855816d
  1. 4
      base_multi_image/README.rst
  2. 2
      base_multi_image/__manifest__.py
  3. 36
      base_multi_image/models/image.py
  4. 1
      base_multi_image/models/owner.py
  5. 66
      base_multi_image/readme/CONFIGURE.rst
  6. 6
      base_multi_image/readme/CONTRIBUTORS.rst
  7. 4
      base_multi_image/readme/CREDITS.rst
  8. 2
      base_multi_image/readme/DESCRIPTION.rst
  9. 4
      base_multi_image/readme/INSTALL.rst
  10. 3
      base_multi_image/readme/ROADMAP.rst
  11. 14
      base_multi_image/views/image_view.xml

4
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:

2
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, "

36
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.'))

1
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',

66
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::
<field
name="image_ids"
nolabel="1"
context="{
'default_owner_model': 'my.model.name',
'default_owner_id': id,
}"
mode="kanban"/>
* 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

6
base_multi_image/readme/CONTRIBUTORS.rst

@ -0,0 +1,6 @@
* Pedro M. Baeza <pedro.baeza@serviciosbaeza.com>
* Rafael Blasco <rafabn@antiun.com>
* Jairo Llopis <yajo.sk8@gmail.com>
* Sodexis <dev@sodexis.com>
* Dave Lasley <dave@laslabs.com>
* Shepilov Vladislav <shepilov.v@protonmail.com>

4
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.

2
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.

4
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.

3
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.

14
base_multi_image/views/image_view.xml

@ -91,6 +91,7 @@
<field name="model">base_multi_image.image</field>
<field name="arch" type="xml">
<kanban string="Product Images">
<field name="id"/>
<field name="name"/>
<field name="storage"/>
<field name="sequence"/>
@ -102,12 +103,13 @@
style="position: absolute; right: 0; padding: 4px; diplay: inline-block">X</a>
<div class="oe_module_vignette">
<a type="open">
<img
t-att-src="kanban_image(
'base_multi_image.image',
'image_small',
record.id.value)"
class="oe_kanban_image"/>
<img
t-att-alt="record.name"
t-att-src="kanban_image(
'base_multi_image.image',
'image_small',
record.id.value)"
class="oe_kanban_image"/>
</a>
<div class="oe_module_desc">
<div class="oe_kanban_box_content oe_kanban_color_bglight oe_kanban_color_border">

Loading…
Cancel
Save