From 0f37737f3986e2f36c9fb9991ac7aaeb19c25afa Mon Sep 17 00:00:00 2001 From: Sebastian Gutzeit Date: Tue, 7 Nov 2023 13:51:33 +0100 Subject: [PATCH] =?UTF-8?q?App=20Datenmanagement:=20Bugfix=20und=20Test=20?= =?UTF-8?q?f=C3=BCr=20Batch-Zuweisung=20von=20Werten=20in=20Tabellenansich?= =?UTF-8?q?t?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- datenmanagement/tests/base.py | 37 +++++++++++- datenmanagement/tests/test_models_simple.py | 65 +++++++++++++++++---- datenmanagement/views/functions.py | 3 +- 3 files changed, 91 insertions(+), 14 deletions(-) diff --git a/datenmanagement/tests/base.py b/datenmanagement/tests/base.py index de5a85b6..4e9ce959 100644 --- a/datenmanagement/tests/base.py +++ b/datenmanagement/tests/base.py @@ -283,7 +283,7 @@ def generic_delete_view_test(self, immediately, model, object_filter, status_cod self.login_assign_permissions(model) # clean object filter object_filter = clean_object_filter(model, object_filter) - # prepare the POST + # prepare the GET/POST deletion_object = get_object(model, object_filter) if immediately: response = self.client.get( @@ -316,6 +316,41 @@ def generic_delete_view_test(self, immediately, model, object_filter, status_cod # no more test objects left? self.assertEqual(model.objects.filter(**object_filter).count(), 0) + @override_settings(AUTHENTICATION_BACKENDS=['django.contrib.auth.backends.ModelBackend']) + def generic_assign_view_test(self, model, target_object_filter, updated_object_filter, + field, value, status_code, content_type, object_count): + """ + tests a view for assigning a specific value to a specific field of an object via GET + + :param self + :param model: model + :param target_object_filter: target object filter + :param updated_object_filter: updated object filter + :param field: specific field to which the specific value is to be assigned + :param value: specific value to be assigned + :param status_code: expected status code of response + :param content_type: expected content type of response + :param object_count: expected number of objects passing the object filter + """ + # perform login and set all necessary rights on the passed model + self.login_assign_permissions(model) + # clean object filters + target_object_filter = clean_object_filter(model, target_object_filter) + updated_object_filter = clean_object_filter(model, updated_object_filter) + # prepare the GET + target_object = get_object(model, target_object_filter) + response = self.client.get( + reverse( + 'datenmanagement:' + self.model.__name__ + '_assign', args=[target_object.pk] + ) + '?field=' + field + '&value=' + value + ) + # status code of response as expected? + self.assertEqual(response.status_code, status_code) + # content type of response as expected? + self.assertEqual(response['content-type'].lower(), content_type) + # updated object contains specific value which was assigned to its specific field? + self.assertEqual(model.objects.filter(**updated_object_filter).count(), object_count) + class DefaultMetaModelTestCase(DefaultModelTestCase): """ diff --git a/datenmanagement/tests/test_models_simple.py b/datenmanagement/tests/test_models_simple.py index 8587ad20..69eb6057 100644 --- a/datenmanagement/tests/test_models_simple.py +++ b/datenmanagement/tests/test_models_simple.py @@ -5915,7 +5915,7 @@ def setUpTestData(cls): adresse2 = Adressen.objects.create( adresse='Adresse1' ) - bevollmaechtigter_bezirksschornsteinfeger = ( + bevollmaechtigter_bezirksschornsteinfeger1 = ( Bevollmaechtigte_Bezirksschornsteinfeger.objects.create( auswaertig=False, vorname='Vorname1', @@ -5926,24 +5926,36 @@ def setUpTestData(cls): anschrift_ort='Ort1' ) ) + bevollmaechtigter_bezirksschornsteinfeger2 = ( + Bevollmaechtigte_Bezirksschornsteinfeger.objects.create( + auswaertig=True, + vorname='Vorname2', + nachname='Nachname2', + anschrift_strasse='Straße2', + anschrift_hausnummer='456', + anschrift_postleitzahl='23456', + anschrift_ort='Ort2' + ) + ) + cls.bevollmaechtigter_bezirksschornsteinfeger2 = bevollmaechtigter_bezirksschornsteinfeger2 cls.attributes_values_db_initial = { 'adresse': adresse1, - 'bevollmaechtigter_bezirksschornsteinfeger': bevollmaechtigter_bezirksschornsteinfeger + 'bevollmaechtigter_bezirksschornsteinfeger': bevollmaechtigter_bezirksschornsteinfeger1 } cls.attributes_values_db_updated = { - 'vergabedatum': VALID_DATE + 'bevollmaechtigter_bezirksschornsteinfeger': bevollmaechtigter_bezirksschornsteinfeger2 } cls.attributes_values_view_initial = { 'aktiv': True, 'adresse': str(adresse2.pk), 'bevollmaechtigter_bezirksschornsteinfeger': str( - bevollmaechtigter_bezirksschornsteinfeger.pk) + bevollmaechtigter_bezirksschornsteinfeger1.pk) } cls.attributes_values_view_updated = { 'aktiv': True, 'adresse': str(adresse2.pk), 'bevollmaechtigter_bezirksschornsteinfeger': str( - bevollmaechtigter_bezirksschornsteinfeger.pk), + bevollmaechtigter_bezirksschornsteinfeger1.pk), 'vergabedatum': VALID_DATE } cls.test_object = cls.model.objects.create(**cls.attributes_values_db_initial) @@ -6045,6 +6057,18 @@ def test_view_delete(self): 'text/html; charset=utf-8' ) + def test_view_assign(self): + self.generic_assign_view_test( + self.model, + self.attributes_values_db_initial, + self.attributes_values_db_updated, + 'bevollmaechtigter_bezirksschornsteinfeger', + str(self.bevollmaechtigter_bezirksschornsteinfeger2.pk), + 204, + 'text/html; charset=utf-8', + 1 + ) + def test_view_deleteimmediately(self): self.generic_delete_view_test( True, @@ -6310,31 +6334,36 @@ def setUpTestData(cls): adresse = Adressen.objects.create( adresse='Adresse' ) - traeger = Bewirtschafter_Betreiber_Traeger_Eigentuemer.objects.create( - bezeichnung='Bezeichnung', - art='Art' + traeger1 = Bewirtschafter_Betreiber_Traeger_Eigentuemer.objects.create( + bezeichnung='Bezeichnung1', + art='Art1' + ) + traeger2 = Bewirtschafter_Betreiber_Traeger_Eigentuemer.objects.create( + bezeichnung='Bezeichnung2', + art='Art2' ) + cls.traeger2 = traeger2 cls.attributes_values_db_initial = { 'adresse': adresse, 'bezeichnung': 'Bezeichnung1', - 'traeger': traeger, + 'traeger': traeger1, 'geometrie': VALID_POINT_DB } cls.attributes_values_db_updated = { - 'bezeichnung': 'Bezeichnung2' + 'traeger': traeger2 } cls.attributes_values_view_initial = { 'aktiv': True, 'adresse': str(adresse.pk), 'bezeichnung': 'Bezeichnung3', - 'traeger': str(traeger.pk), + 'traeger': str(traeger1.pk), 'geometrie': VALID_POINT_VIEW } cls.attributes_values_view_updated = { 'aktiv': True, 'adresse': str(adresse.pk), 'bezeichnung': 'Bezeichnung4', - 'traeger': str(traeger.pk), + 'traeger': str(traeger1.pk), 'geometrie': VALID_POINT_VIEW } cls.attributes_values_view_invalid = { @@ -6499,6 +6528,18 @@ def test_view_delete(self): 'text/html; charset=utf-8' ) + def test_view_assign(self): + self.generic_assign_view_test( + self.model, + self.attributes_values_db_initial, + self.attributes_values_db_updated, + 'traeger', + str(self.traeger2.pk), + 204, + 'text/html; charset=utf-8', + 1 + ) + def test_view_deleteimmediately(self): self.generic_delete_view_test( True, diff --git a/datenmanagement/views/functions.py b/datenmanagement/views/functions.py index d8dc0931..975c5385 100644 --- a/datenmanagement/views/functions.py +++ b/datenmanagement/views/functions.py @@ -106,7 +106,8 @@ def assign_object_value(request, pk): if request.user.has_perm('datenmanagement.change_' + model_name.lower()): if request.GET.get('field') and request.GET.get('value'): field, value = request.GET.get('field'), request.GET.get('value') - value_object = getattr(obj, field).__class__.objects.get(pk=value) + source_model = model._meta.get_field(field).remote_field.model + value_object = source_model.objects.get(pk=value) setattr(obj, field, value_object) obj.save() else: