Python Monkey Patch Static Method

Python Monkey Patch Static Method

Here is a classic editable list in OpenERP v6.0 :

Monkey
  • A instance at 0x7f6757a4e488 A.f # class method is a function is. Labels: bound, methods descriptor, monkey patch, python, unbound.
  • Patch for Python 3.6.3 to use a custom static OpenSSL - python-3.6.3-custom-static-openssl.patch.
  • And thanks to Bryan Forbes’ article, I engineered a method to monkey patch the original ManyToOne.prototype.onkeydown. If Python is well-equiped to.

In this case, 'monkey patching' means adding a new variable or method to a class after it's been defined. For instance, say we defined class A as. Class A (object): def init (self, num): self.num = num def add (self, other): return A (self.num + other.num) But now we want to.

It’s a custom view I created this month at work for one of our customer to let him select a list of products, then batch-print their labels on stickers.

The view above is produced by the following XML :

If you start searching a product template with the first field, you’ll get a pop-up similar to this one:

As you can see, these kind of pop-up inherits the width of their parent field, which hide the end of all lines if they are too long. It becomes difficult to distinguish the items when all found objects have the same long prefix.

Now I want to get rid of this behavior and let the pop-up menu take all the necessary width it needs to fully display its content.

My instinct told me that this default style could easily be overridden with some static CSS directives. But digging deeper into OpenERP web client code , I realized that the width is dynamically set by the many2one widget itself.

The code responsible for this behavior is located in the addons/openerp/static/javascript/m2o.js file, in the ManyToOne.prototype.on_keydown method:

Monkey

Python Method May Be Static

Python

My goal is now to alter this default behavior, without touching the code in m2o.js .

Python Monkey Patch Static Method Template

And thanks to Bryan Forbes’ article , I engineered a method to monkey patch the original ManyToOne.prototype.on_keydown Javascript method.

Here is the code I added in the XML view, just below the line_ids field:

Iron on patches

The result of this is a nice looking pop-up which doesn’t break any vanilla Javascript of the OpenERP web client:

Python Monkey Patch Function

Related content

Python Monkey Patch Static Method Tutorial

Please enable JavaScript to view the comments powered by Disqus .