Skip to content

Commit

Permalink
Fixes #1962 -- Return default location-type label if option is not de…
Browse files Browse the repository at this point in the history
…fined (#1968)
  • Loading branch information
oliverroick authored Feb 9, 2018
1 parent 5efefd6 commit ba39323
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
18 changes: 11 additions & 7 deletions cadasta/spatial/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from resources.mixins import ResourceModelMixin
from jsonattrs.fields import JSONAttributeField
from jsonattrs.decorators import fix_model_for_attributes
from questionnaires.models import Question
from questionnaires.models import Question, QuestionOption


@fix_model_for_attributes
Expand Down Expand Up @@ -124,18 +124,22 @@ def location_type_label(self):
if not self.project.current_questionnaire:
return dict(TYPE_CHOICES)[self.type]

question = Question.objects.get(
questionnaire_id=self.project.current_questionnaire,
name='location_type'
)
label = question.options.get(name=self.type).label_xlat
try:
question = Question.objects.get(
questionnaire_id=self.project.current_questionnaire,
name='location_type'
)
label = question.options.get(name=self.type).label_xlat
except (Question.DoesNotExist, QuestionOption.DoesNotExist):
return dict(TYPE_CHOICES)[self.type]

if label is None or isinstance(label, str):
return label
else:
return label.get(
get_language(),
label[question.questionnaire.default_language]
)
)


def reassign_spatial_geometry(instance):
Expand Down
24 changes: 24 additions & 0 deletions cadasta/spatial/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,30 @@ def test_location_type_label_questionnaire_single_lang(self):
type='HOUSE')
assert su.location_type_label == 'Haus'

def test_location_type_label_no_question_defined(self):
questionnaire = q_factories.QuestionnaireFactory.create()
su = SpatialUnitFactory.create(
project=questionnaire.project,
type='RW')
assert su.location_type_label == 'Right-of-way'

def test_location_type_label_no_question_option_defined(self):
questionnaire = q_factories.QuestionnaireFactory.create()
question = q_factories.QuestionFactory.create(
questionnaire=questionnaire,
name='location_type',
type='S1'
)
q_factories.QuestionOptionFactory.create(
question=question,
name='HOUSE',
label='Haus'
)
su = SpatialUnitFactory.create(
project=questionnaire.project,
type='RW')
assert su.location_type_label == 'Right-of-way'

def test_ui_class_name(self):
su = SpatialUnitFactory.create()
assert su.ui_class_name == "Location"
Expand Down

0 comments on commit ba39323

Please sign in to comment.