From a345984e12b3e8810d1db29d223c4025f4e8815e Mon Sep 17 00:00:00 2001 From: Maciej Lewinski Date: Mon, 28 Jun 2021 16:19:27 +0200 Subject: [PATCH 1/5] Added filed to gitignore file --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 79981bbc..c685de76 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,6 @@ venv .tox/ .coverage .vscode/ +build/**/* +foo/**/* +foobar* From 433cf5d77a4740469e5cd636f73fa6fedf24e43f Mon Sep 17 00:00:00 2001 From: Maciej Lewinski Date: Tue, 29 Jun 2021 09:01:16 +0200 Subject: [PATCH 2/5] Fix for test missing secret key --- tests/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/__init__.py b/tests/__init__.py index e496e1a1..a32ac85f 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -29,6 +29,7 @@ settings.configure(**{ 'DEBUG': True, 'ALLOWED_HOSTS': ['*'], + 'SECRET_KEY': 'test_secret', 'DATABASES': { 'default': db_settings, }, From e91aa51a14b2bf2479f9b68595b3dcc9530bdbc6 Mon Sep 17 00:00:00 2001 From: Maciej Lewinski Date: Tue, 29 Jun 2021 09:02:02 +0200 Subject: [PATCH 3/5] M2m validation errors ref T32083 --- binder/views.py | 7 ++++++- tests/test_m2m_store_errors.py | 37 ++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/binder/views.py b/binder/views.py index c12c1760..aca46916 100644 --- a/binder/views.py +++ b/binder/views.py @@ -1405,7 +1405,8 @@ def store_m2m_field(obj, field, value, request): ignored_fields.append(field) except BinderValidationError as e: validation_errors.append(e) - + except BinderFieldTypeError: + raise if validation_errors: raise sum(validation_errors, None) @@ -2002,6 +2003,10 @@ def _multi_put_save_objects(self, ordered_objects, objects, request): view._store(obj, values, request, pk=oid) except BinderValidationError as e: validation_errors.append(e) + except BinderFieldTypeError: + if not validation_errors: + raise + if oid < 0: new_id_map[(model, oid)] = obj.id for base in getmro(model)[1:]: diff --git a/tests/test_m2m_store_errors.py b/tests/test_m2m_store_errors.py index 0d5167da..34198a7e 100644 --- a/tests/test_m2m_store_errors.py +++ b/tests/test_m2m_store_errors.py @@ -121,3 +121,40 @@ def test_saving_o2o_with_validation_error(self): response = self.client.put('/contact_person/', data=json.dumps(model_data), content_type='application/json') self.assert_validation_error_as_response(response) + + # Validation should be shown when contained in m2m relation + def test_saving_m2m_with_validation_error(self): + data = { + "id": -65, + # Name exceeds allowed 64 charecters, should show this error in response + 'name': 'Scooby Dooooooooooooooooooooooooooooooooooooooooooooooooooooooooo', + } + + + zoo_data = { + "data": [ + { + "id": -4, + 'name': 'Test zoo', + 'most_popular_animals': [-65], + "contacts": [], + 'opening_time': '08:00' + + } + ], + "with": { + "animal": [data] + } + } + + animal_data = { + "data": [data] + } + + response_animal = self.client.put( + '/animal/', data=json.dumps(animal_data), content_type='application/json') + response_zoo = self.client.put( + '/zoo/', data=json.dumps(zoo_data), content_type='application/json') + + self.assertEqual(response_zoo.status_code, 400) + self.assertIn(str(response_animal.content), str(response_zoo.content)) From 12a0ca2385c074b495d61fda4f86c6a8decbf9fe Mon Sep 17 00:00:00 2001 From: Daan van der Kallen Date: Mon, 23 Aug 2021 13:46:55 +0200 Subject: [PATCH 4/5] Fix linting error --- binder/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/binder/views.py b/binder/views.py index aca46916..3e3536df 100644 --- a/binder/views.py +++ b/binder/views.py @@ -2006,7 +2006,7 @@ def _multi_put_save_objects(self, ordered_objects, objects, request): except BinderFieldTypeError: if not validation_errors: raise - + if oid < 0: new_id_map[(model, oid)] = obj.id for base in getmro(model)[1:]: From 38ed2ed3f3e7d311098860e895f810e921565fc9 Mon Sep 17 00:00:00 2001 From: Maciej Lewinski Date: Mon, 31 Jan 2022 14:07:43 +0100 Subject: [PATCH 5/5] Modifications after code review --- binder/views.py | 2 -- tests/test_m2m_store_errors.py | 27 +++++++++++++++++++++------ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/binder/views.py b/binder/views.py index 3e3536df..981c9c11 100644 --- a/binder/views.py +++ b/binder/views.py @@ -1405,8 +1405,6 @@ def store_m2m_field(obj, field, value, request): ignored_fields.append(field) except BinderValidationError as e: validation_errors.append(e) - except BinderFieldTypeError: - raise if validation_errors: raise sum(validation_errors, None) diff --git a/tests/test_m2m_store_errors.py b/tests/test_m2m_store_errors.py index 34198a7e..732c8835 100644 --- a/tests/test_m2m_store_errors.py +++ b/tests/test_m2m_store_errors.py @@ -130,16 +130,26 @@ def test_saving_m2m_with_validation_error(self): 'name': 'Scooby Dooooooooooooooooooooooooooooooooooooooooooooooooooooooooo', } - zoo_data = { "data": [ { "id": -4, - 'name': 'Test zoo', - 'most_popular_animals': [-65], + 'name': 2, + 'most_popular_animals': [], "contacts": [], - 'opening_time': '08:00' + 'opening_time': 'time validation error' + } + ] + } + zoo_data_with_animal = { + "data": [ + { + "id": -4, + 'name': 2, + 'most_popular_animals': [-65], + "contacts": [], + 'opening_time': 'time validation error' } ], "with": { @@ -155,6 +165,11 @@ def test_saving_m2m_with_validation_error(self): '/animal/', data=json.dumps(animal_data), content_type='application/json') response_zoo = self.client.put( '/zoo/', data=json.dumps(zoo_data), content_type='application/json') - + response_zoo_with_animal = self.client.put( + '/zoo/', data=json.dumps(zoo_data_with_animal), content_type='application/json') + self.assertEqual(response_zoo.status_code, 400) - self.assertIn(str(response_animal.content), str(response_zoo.content)) + self.assertIn(str(response_animal.content), + str(response_zoo_with_animal.content)) + self.assertIn(str(response_zoo.content), str( + response_zoo_with_animal.content))