Skip to Content
Menu
This question has been flagged
2 Replies
2457 Views

Using Odoo 12 I'm trying to resize file uploads as they are attached to the crm_lead model. 

When I visit my website and use my custom form that generates leads to upload a file it doesn't seem to be resized though.

My code is included below, any ideas why it isn't working?


from odoo import api, models

import logging
_logger = logging.getLogger(__name__)

class Lead(models.Model):
    _inherit = 'crm.lead'

    @api.model
    def create(self,values):
        _logger.info(values)
        record = super(Lead, self).create(values)

        if 'image' in values:
            resize_image = tools.image_resize_image(values['image'], size=(2048, 2048), avoid_if_small=True)
            record['image'] = resize_image

        return record

    @api.multi
    def write(self, values):
        _logger.info(values)
        record super(Lead, self).write(values)

        if 'image' in values:
            resize_image = tools.image_resize_image(values['image'], size=(2048, 2048), avoid_if_small=True)
            record['image'] = resize_image

        return record
Avatar
Discard
Author Best Answer

This ended up being the class to override:

from odoo.addons.website_form.controllers.main import WebsiteForm  # Import the class

class CustomWebsiteForm(WebsiteForm):
    # Link all files attached on the form
    def insert_attachment(self, model, id_record, files):
Avatar
Discard
Best Answer

Hello Anthony Taylor,

First you need to import as below:

from odoo import api, models

from odoo.tools import image_resize_images, image_resize_image, ustr

import logging

logger = logging.getLogger(__name_)

@api.model_create_multi

def create(self, values):

        _logger.info(values)

        if 'image' in values:

        image = ustr(vals['image'] or '').encode('utf-8')

            resize_image = image_resize_image(values['image'], size=(2048, 2048))

            values['image'] = resize_image

        return super(Lead, self).create(values)

@api.multi

def write(self, values):

        _logger.info(values)

        if 'image' in values:

        image = ustr(vals['image'] or '').encode('utf-8')

            resize_image = image_resize_image(values['image'], size=(2048, 2048))

            values['image'] = resize_image

        return super(Lead, self).create(values)

You can also refer to this link:-

https://www.ridingbytes.com/2016/01/04/odoo-dynamic-image-resizing

However if you want to resize large image, you can use below code. It is also available in addons. addons/hr/models/hr.py:

from odoo.modules.module import get_module_resource

    @api.model

    def _default_image(self):

        image_path = get_module_resource('hr', 'static/src/img', 'default_image.png')

        return tools.image_resize_image_big(base64.b64encode(open(image_path, 'rb').read()))

Thank you!

Regards,




Email:      odoo@aktivsoftware.com  

Skype: kalpeshmaheshwari

   

Avatar
Discard
Author

Thank you for your response. I think the issue I'm running into is that the images I'm wanting to resize are attached separately via the mail.thread when they are submitted from the web form.

I have not figured out yet how to apply the resize code to the mail.thread attachments. I suspect I'm going about it the wrong way.

Related Posts Replies Views Activity
1
Feb 22
9337
1
Jun 19
7990
3
Apr 24
6506
1
Jun 24
1074
2
Dec 23
12008