def test_in_admin(self): id = next(unique_id) model_admin = site._registry[SimpleConcurrentModel] with attributes((model_admin.__class__, 'list_editable_policy', CONCURRENCY_LIST_EDITABLE_POLICY_ABORT_ALL), (ConcurrentModelAdmin, 'form', DELETE_ATTRIBUTE)): saved, __ = SimpleConcurrentModel.objects.get_or_create(pk=id) url = reverse('admin:demo_simpleconcurrentmodel_change', args=[saved.pk]) res = self.app.get(url, user=self.user.username) form = res.form saved.save() # create conflict here res = form.submit(expect_errors=True) self.assertEqual(res.status_code, 409) target = res.context['target'] self.assertIn('target', res.context) self.assertIn('saved', res.context) self.assertEqual(res.context['target'].version, target.version) self.assertEqual(res.context['saved'].version, saved.version) self.assertEqual(res.context['request_path'], url)
def test_in_admin(self): id = next(unique_id) middlewares = list(settings.MIDDLEWARE_CLASSES) + ['concurrency.middleware.ConcurrencyMiddleware'] model_admin = site._registry[SimpleConcurrentModel] with attributes((model_admin.__class__, 'list_editable_policy', CONCURRENCY_LIST_EDITABLE_POLICY_ABORT_ALL), (ConcurrentModelAdmin, 'form', DELETE_ATTRIBUTE)): with self.settings(MIDDLEWARE_CLASSES=middlewares): saved, __ = SimpleConcurrentModel.objects.get_or_create(pk=id) url = reverse('admin:demo_simpleconcurrentmodel_change', args=[saved.pk]) res = self.app.get(url, user='******') form = res.form saved.save() # create conflict here res = form.submit(expect_errors=True) self.assertEqual(res.status_code, 409) target = res.context['target'] self.assertIn('target', res.context) self.assertIn('saved', res.context) self.assertEqual(res.context['target'].version, target.version) self.assertEqual(res.context['saved'].version, saved.version) self.assertEqual(res.context['request_path'], url)
def test_concurrency_policy_silent(self): id = next(unique_id) self.TARGET.objects.get_or_create(pk=id) model_admin = site._registry[self.TARGET] with attributes((model_admin.__class__, 'list_editable_policy', CONCURRENCY_LIST_EDITABLE_POLICY_SILENT)): res = self.app.get('/admin/', user='******') res = res.click(self.TARGET._meta.verbose_name_plural) self._create_conflict(id) form = res.forms['changelist-form'] form['form-0-username'] = '******' res = form.submit('_save').follow() self.assertTrue(self.TARGET.objects.filter(username=SENTINEL).exists()) self.assertFalse(self.TARGET.objects.filter(username='******').exists())
def test_concurrency(self): id = 1 admin_register(ListEditableConcurrentModel, ActionsModelAdmin) model_admin = site._registry[ListEditableConcurrentModel] with attributes((ConcurrentModelAdmin, 'list_editable_policy', CONCURRENCY_LIST_EDITABLE_POLICY_SILENT), (ConcurrentModelAdmin, 'form', ConcurrentForm), ): obj, __ = ListEditableConcurrentModel.objects.get_or_create(pk=id) request1 = get_fake_request('pk=%s&_concurrency_version_1=2' % id) model_admin.save_model(request1, obj, None, True) self.assertIn(obj.pk, model_admin._get_conflicts(request1)) obj = refetch(obj) request2 = get_fake_request('pk=%s&_concurrency_version_1=%s' % (id, obj.version)) model_admin.save_model(request2, obj, None, True) self.assertNotIn(obj.pk, model_admin._get_conflicts(request2))
def test_concurrency_policy_silent(self): id = next(unique_id) self.TARGET.objects.get_or_create(pk=id) model_admin = site._registry[self.TARGET] with attributes((model_admin.__class__, 'list_editable_policy', CONCURRENCY_LIST_EDITABLE_POLICY_SILENT)): res = self.app.get('/admin/', user='******') res = res.click(self.TARGET._meta.verbose_name_plural) self._create_conflict(id) form = res.forms['changelist-form'] form['form-0-username'] = '******' res = form.submit('_save').follow() self.assertTrue( self.TARGET.objects.filter(username=SENTINEL).exists()) self.assertFalse( self.TARGET.objects.filter(username='******').exists())
def test_concurrency(self): id = 1 admin_register(ListEditableConcurrentModel, ActionsModelAdmin) model_admin = site._registry[ListEditableConcurrentModel] with attributes( (ConcurrentModelAdmin, 'list_editable_policy', CONCURRENCY_LIST_EDITABLE_POLICY_SILENT), (ConcurrentModelAdmin, 'form', ConcurrentForm), ): obj, __ = ListEditableConcurrentModel.objects.get_or_create(pk=id) request1 = get_fake_request('pk=%s&_concurrency_version_1=2' % id) model_admin.save_model(request1, obj, None, True) self.assertIn(obj.pk, model_admin._get_conflicts(request1)) obj = refetch(obj) request2 = get_fake_request('pk=%s&_concurrency_version_1=%s' % (id, obj.version)) model_admin.save_model(request2, obj, None, True) self.assertNotIn(obj.pk, model_admin._get_conflicts(request2))