There's more...

In this recipe, we choose to extend the default implementation of the methods. In the make_boorrow() and make_available() methods, we modified the returned result before the super() call. Note that, when you call super(), it will execute the default implementation. It is also possible to perform some actions after the super() call. Of course, we can also do both at the same time.

However, it is more difficult to change the behavior of the middle of a method. To do this, we will need to refactor the code so that we can extract an extension point to a separate method and override this new method in the extension module.

You may be tempted to completely rewrite a method. Always be very cautious when doing so; if you do not call the super() implementation of your method, you are breaking the extension mechanism and potentially breaking the add-ons that extend the method, meaning that the extension methods will never be called. Unless you are working in a controlled environment in which you know exactly which add-ons are installed and you've checked that you are not breaking them, avoid doing this. Also, if you have to, ensure that you document what you are doing in a very visible way.

What can you do before and after calling the original implementation of the method? There are lots of things, including (but not limited to) the following:

  • Modifying the arguments that are passed to the original implementation (before)
  • Modifying the context that is passed to the original implementation (before)
  • Modifying the result that is returned by the original implementation (after)
  • Calling another method (before and after)
  • Creating records (before and after)
  • Raising a UserError to cancel the execution in forbidden cases (before and after)
  • Splitting self in smaller recordsets, and calling the original implementation on each of the subsets in a different way (before)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset