Skip to content

Commit

Permalink
Merge pull request #11 from maxipavlovic/feature/LITE-17187
Browse files Browse the repository at this point in the history
LITE-17187 CQRS supports custom data in DELETE payload
  • Loading branch information
d3rky authored Feb 19, 2021
2 parents 555f72e + 591e1ef commit 6c4bbec
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions dj_cqrs/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
5 changes: 5 additions & 0 deletions dj_cqrs/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions tests/test_master/test_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'}

0 comments on commit 6c4bbec

Please sign in to comment.