From bff28be0b999901a8fde781ea979402ee9eaf637 Mon Sep 17 00:00:00 2001 From: Anthony Lukach Date: Thu, 12 Apr 2018 00:21:23 -0600 Subject: [PATCH] Use QuestionOption.question.name when fetching location_type question (#2032) --- cadasta/spatial/models.py | 1 + cadasta/spatial/tests/test_models.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/cadasta/spatial/models.py b/cadasta/spatial/models.py index 9469d0ee7..1f2976585 100644 --- a/cadasta/spatial/models.py +++ b/cadasta/spatial/models.py @@ -293,6 +293,7 @@ def set_label_on_spatialunits(sender, instance, **kwargs): try: rel_questionoption = QuestionOption.objects.get( name=instance.type, + question__name='location_type', question__questionnaire__id=instance.project.current_questionnaire) except QuestionOption.DoesNotExist: return diff --git a/cadasta/spatial/tests/test_models.py b/cadasta/spatial/tests/test_models.py index 8ce7ca30a..91ad28020 100644 --- a/cadasta/spatial/tests/test_models.py +++ b/cadasta/spatial/tests/test_models.py @@ -141,6 +141,34 @@ def test_location_type_label_questionnaire_default_lang(self): type='HOUSE') assert su.location_type_label == 'House' + def test_location_type_label_uses_correct_question_option(self): + """ + Ensure that the label setter pre_save signal fetches the correct + question option. + """ + questionnaire = q_factories.QuestionnaireFactory.create( + default_language='en') + location_question = q_factories.QuestionFactory.create( + questionnaire=questionnaire, + name='location_type', + type='S1' + ) + other_question = q_factories.QuestionFactory.create( + questionnaire=questionnaire, + name='least_favorite_music_genre', + type='S1' + ) + for q in (location_question, other_question): + q_factories.QuestionOptionFactory.create( + question=q, + name='HOUSE', + label={'en': 'House', 'de': 'Hause'} + ) + su = SpatialUnitFactory.create( + project=questionnaire.project, + type='HOUSE') + assert su.location_type_label == 'House' + def test_location_type_label_questionnaire_selected_lang(self): activate('es') questionnaire = q_factories.QuestionnaireFactory.create(