def test_middleware(): handler = mock.Mock(status_code=409) type(handler.return_value).status_code = mock.PropertyMock(return_value=409) with override_settings(CONCURRENCY_HANDLER409=handler): request = _get_request('needsquoting#') r = ConcurrencyMiddleware().process_exception(request, RecordModifiedError(target=SimpleConcurrentModel())) assert r.status_code == 409
def test_process_exception(self): """ Tests that RecordModifiedError is handled correctly. """ id = next(unique_id) m, __ = SimpleConcurrentModel.objects.get_or_create(pk=id) copy = SimpleConcurrentModel.objects.get(pk=m.pk) copy.save() request = self._get_request('/') r = ConcurrencyMiddleware().process_exception(request, RecordModifiedError(target=m)) self.assertEqual(r.status_code, 409)
def _select_lock(model_instance, version_value=None): version_field = model_instance.RevisionMetaInfo.field value = version_value or getattr(model_instance, version_field.name) is_versioned = value != version_field.get_default() if model_instance.pk is not None and is_versioned: kwargs = {'pk': model_instance.pk, version_field.name: value} alias = router.db_for_write(model_instance) NOWAIT = connections[alias].features.has_select_for_update_nowait entry = model_instance.__class__.objects.select_for_update( nowait=NOWAIT).filter(**kwargs) if not entry: logger.debug("Conflict detected on `{0}` pk:`{0.pk}`, " "version `{1}` not found".format( model_instance, value)) if conf.POLICY & CONCURRENCY_POLICY_CALLBACK: conf._callback(model_instance) else: raise RecordModifiedError( _('Record has been modified or no version value passed'), target=model_instance) elif is_versioned and conf.SANITY_CHECK and model_instance._revisionmetainfo.sanity_check: raise InconsistencyError( _('Version field is set (%s) but record has not `pk`.') % value)
def callback(target, *args, **kwargs): raise RecordModifiedError(_('Record has been modified'), target=target)