From 591e1ef20468b66994aec35baaf395072fe7bdf7 Mon Sep 17 00:00:00 2001 From: Max Kolyubyakin Date: Fri, 19 Feb 2021 12:20:59 +0300 Subject: [PATCH] LITE-17187 CQRS supports custom data in DELETE payload --- dj_cqrs/mixins.py | 4 ++++ dj_cqrs/signals.py | 5 +++++ tests/test_master/test_mixin.py | 15 +++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/dj_cqrs/mixins.py b/dj_cqrs/mixins.py index 23a99f7..9b7da78 100644 --- a/dj_cqrs/mixins.py +++ b/dj_cqrs/mixins.py @@ -280,6 +280,10 @@ def _cqrs_serializer_cls(self): except ImportError: raise ImportError("Model {}: CQRS_SERIALIZER can't be imported.".format(self.__class__)) + def get_custom_cqrs_delete_data(self): + """ This method should be overridden when additional data is needed in DELETE payload. """ + pass + class MasterMixin(RawMasterMixin, metaclass=MasterMeta): """ diff --git a/dj_cqrs/signals.py b/dj_cqrs/signals.py index a1c0abf..7946a20 100644 --- a/dj_cqrs/signals.py +++ b/dj_cqrs/signals.py @@ -92,6 +92,11 @@ def post_delete(cls, sender, **kwargs): 'cqrs_revision': instance.cqrs_revision + 1, 'cqrs_updated': str(now()), } + + data = instance.get_custom_cqrs_delete_data() + if data: + instance_data['custom'] = data + signal_type = SignalType.DELETE payload = TransportPayload(signal_type, sender.CQRS_ID, instance_data, instance.pk) diff --git a/tests/test_master/test_mixin.py b/tests/test_master/test_mixin.py index 86ad3d0..153d354 100644 --- a/tests/test_master/test_mixin.py +++ b/tests/test_master/test_mixin.py @@ -792,3 +792,18 @@ def test_sequential_transactions(mocker): assert instance.cqrs_revision == 1 assert publisher_mock.call_args_list[0][0][0].instance_data['char_field'] == '1' assert publisher_mock.call_args_list[1][0][0].instance_data['char_field'] == '2' + + +@pytest.mark.django_db(transaction=True) +def test_get_custom_cqrs_delete_data(mocker): + publisher_mock = mocker.patch('dj_cqrs.controller.producer.produce') + + m = models.SimplestModel.objects.create(id=1) + m.get_custom_cqrs_delete_data = lambda *args: {'1': '2'} + m.delete() + + payload = publisher_mock.call_args_list[1][0][0] + assert payload.signal_type == SignalType.DELETE + assert payload.instance_data['id'] == 1 + assert payload.instance_data['cqrs_revision'] == 1 + assert payload.instance_data['custom'] == {'1': '2'}