Skip to content

Commit

Permalink
App Datenmanagement: Bugfix und Test für Batch-Zuweisung von Werten i…
Browse files Browse the repository at this point in the history
…n Tabellenansicht
  • Loading branch information
gdmhrogut committed Nov 7, 2023
1 parent 39e74e4 commit 0f37737
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 14 deletions.
37 changes: 36 additions & 1 deletion datenmanagement/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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):
"""
Expand Down
65 changes: 53 additions & 12 deletions datenmanagement/tests/test_models_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion datenmanagement/views/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 0f37737

Please sign in to comment.