Contributors mailing list archives

contributors@odoo-community.org

Browse archives

Avatar

A methodology / best practice / Odoo / Python question

by
Data Dance s.r.o., Radovan Skolnik
- 06/09/2021 09:43:10

Hello,

 

I am struggling with this for a while so I have decided to ask here. I am building a module that would be showing active filters/ordering as tags on e-commerce by parsing query part of the URL. Besides the standard ones (search, order) we are also using brands filter and also custom_info. Both of these can have multiple options checked. User has the option to remove them individually instead of searching for appropriate checkobxes on the page. Now my approach is basically iterate through key/value pairs and for each generate what looks like a tag visually with URL that has that key/value pair removed (so it in fact removes that part of filter). For that I have devised a code like this:

 

from odoo.http import request

 

from werkzeug import OrderedMultiDict

from werkzeug.urls import url_parse, url_encode

 

def _get_filtered_url(param, value):

filtered_args = OrderedMultiDict(filter(lambda arg: arg[0]!=param or arg[1]!=value, request.httprequest.args.items(multi=True)))

url = url_parse(request.httprequest.url)

return url.replace(query=url_encode(filtered_args)).to_url()

 

Now this code is obviously not a object/class method - it should be (in my opinion) a static method. Now the question is: static method of what? Because I need to be able to call it from XML template that renders part of WebsiteSale. I guess I could create a dummy (transient?) class and call it like request.env['my_transient_class']._get_filtered_url(param, value) but that somehow does not seem right to me. Or is it the right way? I am trying to write a clean concise code that is not hacky.

 

Any advice is welcome here. Thank you very much.

 

Best regards

 

Radovan Skolnik

 

Follow-Ups