This question has been flagged
2 Replies
1723 Views

I found a module that autocomplete search in a website e-commerce with high light match words and image. But I didn't really understand what each command do.

Can you please explain to me how this code work, and why they did /shop/get_suggest?

class WebsiteSale(http.Controller):
    @http.route(['/shop/get_suggest'], type='http', auth="public", methods=['GET'], website=True)
    def get_suggest_json(self, **kw):
        query = kw.get('query')
        names = query.split(' ')
        domain = ['|' for k in range(len(names) - 1)] + [('name', 'ilike', name) for name in names]
        products = request.env['product.template'].search(domain, limit=15)
        products = sorted(products, key=lambda x: SequenceMatcher(None, query.lower(), x.name.lower()).ratio(),
                          reverse=True)
        results = []
        for product in products:
            results.append({'value': product.name, 'data': {'id': product.id, 'after_selected': product.name}})
        return json.dumps({
            'query': 'Unit',
            'suggestions': results
        })
Avatar
Discard
Best Answer

Hi,

The function get_suggest_json will get called/executed using the name given in the route. If you check the code you can see that this route will get called from some JS files or XML files. You can give the name as per your like, better using names which can get you idea about the function it executes.


To know more about the Controllers you can check this blog:  Web Controllers in Odoo

Also you can check this official odoo documentation on the same:  Web Controllers

Thanks

Avatar
Discard
Author Best Answer

@Niyas Raphy thank you a lot for your response. I understand know why we use it.


Avatar
Discard