Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to convert old API into NEW

By
Dr Obx
on 12/3/15, 8:11 AM 606 views

Guys, I'm not sure how should it look like in NEW API format.....

<pre>

# image: all image fields are base64 encoded and PIL-supported

'image': fields.binary("Image",

help="This field holds the image used as image for the product, limited to 1024x1024px."),

'image_medium': fields.function(_get_image, fnct_inv=_set_image,

string="Medium-sized image", type="binary", multi="_get_image",

store={

'product.template': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),

},

help="Medium-sized image of the product. It is automatically "\

"resized as a 128x128px image, with aspect ratio preserved, "\

"only when the image exceeds one of those sizes. Use this field in form views or some kanban views."),

'image_small': fields.function(_get_image, fnct_inv=_set_image,

string="Small-sized image", type="binary", multi="_get_image",

store={

'product.template': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),

},

help="Small-sized image of the product. It is automatically "\

"resized as a 64x64px image, with aspect ratio preserved. "\

"Use this field anywhere a small image is required."),

</pre>

 <pre>

def _get_image(self, cr, uid, ids, name, args, context=None):

result = dict.fromkeys(ids, False)

for obj in self.browse(cr, uid, ids, context=context):

result[obj.id] = tools.image_get_resized_images(obj.image, avoid_resize_medium=True)

return result

def _set_image(self, cr, uid, id, name, value, args, context=None):

return self.write(cr, uid, [id], {'image': tools.image_resize_image_big(value)}, context=context)

</pre>

I can't make it work :(


1

Axel Mendoza

--Axel Mendoza--
10203
| 7 7 8
Camaguey, Cuba
--Axel Mendoza--

DevOps - Full stack - Software Architect - Developer - Technology Integrator

I could help you to develop anything and solve complex problems based on technologies, integrations and tricky stuffs mostly in Python with OpenERP/Odoo, Zato, Django and many others frameworks programming languages and technologies.

I offers consulting services to anyone with an unanswered questions or needs for customizations. Think about it, maybe it's better to have an expert to solve your issues and projects than having a full time employee trying to understand what to do an how

Reach me at aekroft@gmail.com

Axel Mendoza
On 12/3/15, 10:36 AM

Hi @Dr Obx

You could try this:


from openerp import tools
from openerp import models, fields, api

class product_template_test(models.Model):
_name = 'product.template'

image = fields.Binary("Image", help="This field holds the image used as image for the product, limited to 1024x1024px."),
image_medium = fields.Binary(compute='_get_image', inverse='_set_image', string="Medium-sized image", store=True, help="Medium-sized image of the product. It is automatically "\
"resized as a 128x128px image, with aspect ratio preserved, "\
"only when the image exceeds one of those sizes. Use this field in form views or some kanban views.")
image_small = fields.Binary(compute='_get_image', inverse='_set_image', string="Small-sized image", store=True, help="Small-sized image of the product. It is automatically "\
"resized as a 64x64px image, with aspect ratio preserved. "\
"Use this field anywhere a small image is required.")
@api.multi
def _get_image(self):
for obj in self:
result = tools.image_get_resized_images(obj.image, avoid_resize_medium=True)
obj.image_medium = result.get('image_medium', False)
obj.image_small = result.get('image_small', False)
return result

@api.multi
def _set_image(self):
for obj in self:
obj.image_medium = tools.image_resize_image_big(obj.image_medium)
obj.image_small = tools.image_resize_image_big(obj.image_small)
return True

Notes:

-   Maybe you don't need the inverse argument in the fields

-   The store=dict is not supported in the new api but maybe you don't need it in this case.

I did exactly the same before I read your post, but when I'm trying to use it .... it does nothing whatever I do :) I mean it wont re-scale images

Dr Obx
on 12/4/15, 5:21 AM

Not working for me either. I get a RuntimeError: maximum recursion depth exceeded

Rubén Cabrera
on 12/22/15, 5:09 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)

Stats

Asked: 12/3/15, 8:11 AM
Seen: 606 times
Last updated: 12/4/15, 5:08 AM