def post_delete(cls, sender, **kwargs): """ :param dj_cqrs.mixins.MasterMixin sender: Class or instance inherited from CQRS MasterMixin. """ if not sender.CQRS_PRODUCE: return instance = kwargs['instance'] if not instance.is_sync_instance(): return instance_data = { 'id': instance.pk, '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) # Delete is always in transaction! transaction.on_commit(lambda: producer.produce(payload))
def test_producer(mocker): transport_mock = mocker.patch('tests.dj.transport.TransportStub.produce') produce(TransportPayload('a', 'b', {}, 'c', previous_data={'e': 'f'})) assert transport_mock.call_count == 1 assert transport_mock.call_args[0][0].to_dict() == { 'signal_type': 'a', 'cqrs_id': 'b', 'instance_data': {}, 'instance_pk': 'c', 'previous_data': { 'e': 'f' }, 'correlation_id': None, 'expires': None, 'retries': 0, }
def post_save(cls, sender, **kwargs): """ :param dj_cqrs.mixins.MasterMixin sender: Class or instance inherited from CQRS MasterMixin. """ if not sender.CQRS_PRODUCE: return update_fields = kwargs.get('update_fields') if update_fields and ('cqrs_revision' not in update_fields): return instance = kwargs['instance'] if not instance.is_sync_instance(): return using = kwargs['using'] sync = kwargs.get('sync', False) queue = kwargs.get('queue', None) connection = transaction.get_connection(using) if not connection.in_atomic_block: instance.reset_cqrs_saves_count() instance_data = instance.to_cqrs_dict(using, sync=sync) previous_data = instance.get_tracked_fields_data() signal_type = SignalType.SYNC if sync else SignalType.SAVE payload = TransportPayload( signal_type, sender.CQRS_ID, instance_data, instance.pk, queue, previous_data, expires=get_expires_datetime(), ) producer.produce(payload) elif instance.is_initial_cqrs_save: transaction.on_commit( lambda: MasterSignals.post_save( sender, instance=instance, using=using, sync=sync, queue=queue, ), )