General hook for ModelStorage modification

Rational

Following Generic support for cache fields, I found that we have 3 common pattern that requires to extend ModelStorage.create, ModelStorage.write and ModelStorage.delete which may be tricky to extend correctly (see Verify access right only for RPC):

  • apply some modification to the values used for ModelStorage.create and ModelStorage.write (ex: standardize the format of email, put in uppercase the country code, set number from a sequence etc.)
  • trigger an action on modified records (ex: clear a cache, call workflow transition of linked record etc.)
  • check if record can be modified (ex: forbid modification of posted move)

Proposal

I propose to add 3 new hooks in ModelStorage:

  • preprocess_values(cls, mode, values) which is called by ModelStorage.create and ModelStorage.write with each values and must return the modified value (preferably a copy if modified).
  • on_modification(cls, mode, records, field_names=None) which is called by ModelStorage.create, ModelStorage.write at the end and by ModelStorage.delete at the beginning. The field_names contains at least all the fields that have been modified.
  • check_modification(cls, records, values) which is called only by ModelStorage.write at the beginning.

Implementation

It looks good to me as I think inheriting create() and write() is too complex and should be reserved for less frequent use cases.

2 Likes