diff --git a/trytond/CHANGELOG b/trytond/CHANGELOG index bbf592775a..5549d90d6a 100644 --- a/trytond/CHANGELOG +++ b/trytond/CHANGELOG @@ -1,3 +1,4 @@ +* Add on_delete to ModelStorage * Add check_modification to ModelStorage * Add on_modification to ModelStorage * Add preprocess_values to ModelStorage diff --git a/trytond/doc/ref/models.rst b/trytond/doc/ref/models.rst index 80f61d3bae..c1db32c748 100644 --- a/trytond/doc/ref/models.rst +++ b/trytond/doc/ref/models.rst @@ -436,6 +436,10 @@ Class methods: It is used to trigger actions based on what has been modified. +.. classmethod:: ModelStorage.on_delete(records) + + Returns a list of methods to call once the records have been deleted. + .. classmethod:: ModelStorage.copy(records[, default]) Duplicate the records. diff --git a/trytond/trytond/model/modelsql.py b/trytond/trytond/model/modelsql.py index b754e38e01..6e03365b43 100644 --- a/trytond/trytond/model/modelsql.py +++ b/trytond/trytond/model/modelsql.py @@ -1472,7 +1472,7 @@ class ModelSQL(ModelStorage): cursor = transaction.connection.cursor() pool = Pool() - ids = cls._before_delete(records) + ids, on_delete = cls._before_delete(records) if not ids: return @@ -1581,7 +1581,7 @@ class ModelSQL(ModelStorage): cls._update_mptt(list(tree_ids.keys()), list(tree_ids.values())) - cls._after_delete(ids) + cls._after_delete(ids, on_delete) @classmethod def __check_domain_rule(cls, ids, mode): diff --git a/trytond/trytond/model/modelstorage.py b/trytond/trytond/model/modelstorage.py index c8c36ba2c8..719b93577b 100644 --- a/trytond/trytond/model/modelstorage.py +++ b/trytond/trytond/model/modelstorage.py @@ -430,6 +430,7 @@ class ModelStorage(Model): ModelData.clean(records) if check_access: cls.log(records, 'delete') + on_delete = cls.on_delete(records) cls.on_modification('delete', records) triggers = Trigger.get_triggers(cls.__name__, 'delete') @@ -469,7 +470,7 @@ class ModelStorage(Model): except ValueError: break transaction.delete_records[cls.__name__].update(ids) - return ids + return ids, on_delete @classmethod def delete(cls, records): @@ -478,12 +479,14 @@ class ModelStorage(Model): @classmethod @without_check_access - def _after_delete(cls, ids): + def _after_delete(cls, ids, on_delete): pool = Pool() Translation = pool.get('ir.translation') if any(getattr(f, 'translate', False) and not hasattr(f, 'set') for f in cls._fields.values()): Translation.delete_ids(cls.__name__, 'model', ids) + for meth in on_delete: + meth() @classmethod def check_modification(cls, mode, records, values=None, external=False): @@ -493,6 +496,10 @@ class ModelStorage(Model): def on_modification(cls, mode, records, field_names=None): assert mode in {'create', 'write', 'delete'} + @classmethod + def on_delete(cls, records): + return [] + @classmethod def copy(cls, records, default=None): '''