You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
4.0 KiB

10 years ago
10 years ago
10 years ago
  1. {
  2. 'name': 'Web Context Tunnel',
  3. 'category': 'Hidden',
  4. 'author': "Akretion,Odoo Community Association (OCA)",
  5. 'license': 'AGPL-3',
  6. 'description': """
  7. Web Context Tunnel.
  8. ===================
  9. The problem with OpenERP on_changes
  10. -----------------------------------
  11. OpenERP uses to pass on_change Ajax events arguments using positional
  12. arguments. This is annoying as modules often need to pass extra arguments
  13. that are not present in the base on_change signatures. As soon as two modules
  14. try to alter this signature to add their extra arguments, they are incompatible
  15. between them unless some extra glue module make them compatible again by
  16. taking all extra arguments into account. But this leads to a combinatorial
  17. explosion to make modules compatible again.
  18. The solution
  19. ------------
  20. This module provides a simple work around that will work in most of the cases.
  21. In fact it works if the base on_change is designed to pass the context
  22. argument. Else it won't work and you should go the old way. But in any case
  23. it's a bad practice if an on_change doesn't pass the context argument and you
  24. can certainly rant about these bad on_changes to the the context added in the
  25. arguments.
  26. So for an on_change passing the context, how does this module works?
  27. Well OpenERP already has an elegant solution for an extension module to alter
  28. an XML attributes: put an extension point in the view using
  29. position="attributes" and then redefine the attribute. That is already used at
  30. several places to replace the "context" attribute that the client will send to
  31. the server.
  32. The idea here is to wrap the extra arguments needed by your on_change inside
  33. that context dictionary just as it were a regular Python kwargs. That context
  34. should then be automatically propagated accross the on_change call chain,
  35. no matter of the module order and without any need to hack any on_change
  36. signature.
  37. The issue with just position="attributes" and redefining the context, is that
  38. again, if two independent modules redefine the context, they are incompatible
  39. unless a third module accounts for both of them.
  40. But with this module, an extension point can now use position="attributes" and
  41. instead of redefining the "context" attribute, you will now just define a new
  42. "context_foo" attribute this way:
  43. <attribute name="context_foo">{'my_extra_field': my_extra_field}</attribute>.
  44. This module modifies the web client in such a way that before sending the Ajax
  45. on_change event request to the server, all the node attributes starting with
  46. "context" are merged into a single context dictionnary, keeping the keys and
  47. values from all extensions. In the rare case a module really wants to override
  48. the value in context, then it needs to still override the original context
  49. attribute (or the other original attribute).
  50. And of course, if you should call your on_change by API or webservice instead
  51. of using the web client, simply ensure you are wrapping the required extra
  52. arguments in the context dictionary.
  53. Tests
  54. -----
  55. This module comes with a simple test in static/test/context_tunnel.js.
  56. To run it, open the page /web/tests?mod=web_context_tunnel in your browser
  57. as explained here https://doc.openerp.com/trunk/web/testing
  58. It should also be picked by the Python testing when testing with PhantomJS.
  59. As for testing modules using web_context_tunnel with YAML, yes it's possible.
  60. In fact you need to manually mimic the new web-client behavior by manually
  61. ensuring you add the extra context keys you will need later in your on_change.
  62. For instance, before the on_change is called, you can alter the context with
  63. a !python statement like context.update({'my_extra_field': my_extra_field}).
  64. You can see an example of module conversion to use web_context_tunnel here
  65. for instance:
  66. https://github.com/openerpbrasil/l10n_br_core/commit/33065366726a83dbc69b9f0031c81d82362fbfae
  67. """,
  68. 'version': '8.0.2.0.0',
  69. 'depends': ['web'],
  70. 'data': [
  71. 'views/web_context_tunnel.xml',
  72. ],
  73. 'test': [
  74. 'static/test/context_tunnel.js',
  75. ],
  76. 'css': [],
  77. 'auto_install': False,
  78. 'web_preload': False,
  79. }