For easy client communication, a business platform should incorporate media like voice, videos, or photographs. Odoo is aware of this and encourages the use of all media on its platform.
In some situations, no media is saved as an attachment. Using media with or without linking to attachments is possible with Odoo.
In the database, Odoo stores photos using binary fields. The ability to load a picture from a URL is not yet available in Odoo. Only already existing images can be uploaded to the system. In Odoo, an image field has the following fundamental definition:
from odoo import models
class PartnerImage(models.Model):
_inherit = 'res.partner'
img_customer = fields.Binary(string="Image", attachment=False, stored=True)
class ImageUrl(models.Model, ImageFromURLMixin): _name = 'custom.image' image_url = fields.Char(string="Image URL", required=True) create_image = fields.Binary(string="Image", compute='_compute_image', readonly=False, store=True)
from odoo import api, fields, models
import requests
import logging
import base64
_logger = logging.getLogger(__name__)
class ImageFromURLMixin:
def get_image_from_url(self, url):
"""
:return: Returns a base64 encoded string.
"""
data = ""
try:
# Python 2
# data = requests.get(url.strip()).content.encode("base64").replace("n", "")
# Python 3
data = base64.b64encode(requests.get(url.strip()).content).replace(b"n", b"")
except Exception as e:
_logger.warning("Can't load the image from URL %s" % url)
logging.exception(e)
return data
from odoo import api, fields, models
class ImageUrl(models.Model, ImageFromURLMixin):
_name = 'custom.image'
image_url = fields.Char(string="Image URL", required=True)
create_image = fields.Binary(string="Image", compute='_compute_image', readonly=False, store=True)
@api.depends("image_url")
def _compute_image(self):
for record in self:
image = None
if record.image_url:
image = self.get_image_from_url(record.image_url)
self.check_access_rule(image)
record.update({"create_image": image, })