Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[15.0] [MIG] storage_image_import: Migration to 15.0 #392

Open
wants to merge 13 commits into
base: 15.0
Choose a base branch
from
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ paramiko
pyftpdlib
python-magic
python_slugify
unicodecsv
validators
6 changes: 6 additions & 0 deletions setup/storage_image_import/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)
82 changes: 82 additions & 0 deletions storage_image_import/README.rst
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/OCA/storage/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 <https://github.com/OCA/storage/issues/new?body=module:%20storage_image_import%0Aversion:%2014.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

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

Credits
=======

Authors
~~~~~~~

* Akretion

Contributors
~~~~~~~~~~~~

* Sébastien Beau <[email protected]>
* Kévin Roche <[email protected]>

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 <https://github.com/OCA/storage/tree/14.0/storage_image_import>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
1 change: 1 addition & 0 deletions storage_image_import/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
20 changes: 20 additions & 0 deletions storage_image_import/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2021 Akretion (https://www.akretion.com).
# @author Sébastien BEAU <[email protected]>
# 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"],
}
54 changes: 54 additions & 0 deletions storage_image_import/i18n/storage_image_import.pot
Original file line number Diff line number Diff line change
@@ -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 ""
2 changes: 2 additions & 0 deletions storage_image_import/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import image_relation_abstract
from . import storage_image
79 changes: 79 additions & 0 deletions storage_image_import/models/image_relation_abstract.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Copyright 2020 Akretion (https://www.akretion.com).
# @author Sébastien BEAU <[email protected]>
# 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(

Check warning on line 34 in storage_image_import/models/image_relation_abstract.py

View check run for this annotation

Codecov / codecov/patch

storage_image_import/models/image_relation_abstract.py#L32-L34

Added lines #L32 - L34 were not covered by tests
_("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)

Check warning on line 55 in storage_image_import/models/image_relation_abstract.py

View check run for this annotation

Codecov / codecov/patch

storage_image_import/models/image_relation_abstract.py#L55

Added line #L55 was not covered by tests
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

Check warning on line 70 in storage_image_import/models/image_relation_abstract.py

View check run for this annotation

Codecov / codecov/patch

storage_image_import/models/image_relation_abstract.py#L68-L70

Added lines #L68 - L70 were not covered by tests
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)
11 changes: 11 additions & 0 deletions storage_image_import/models/storage_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright 2020 Akretion (https://www.akretion.com).
# @author Sébastien BEAU <[email protected]>
# 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()
2 changes: 2 additions & 0 deletions storage_image_import/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Sébastien Beau <[email protected]>
* Kévin Roche <[email protected]>
1 change: 1 addition & 0 deletions storage_image_import/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This module allows to import images based on url (from csv file)
1 change: 1 addition & 0 deletions storage_image_import/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -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.
Binary file added storage_image_import/static/description/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading