diff --git a/safe/definitions/test/test_utilities.py b/safe/definitions/test/test_utilities.py index dd12b9c138..f141314e1b 100644 --- a/safe/definitions/test/test_utilities.py +++ b/safe/definitions/test/test_utilities.py @@ -67,7 +67,8 @@ default_classification_value_maps, fields_in_field_groups, get_field_groups, - map_report_component + map_report_component, + get_name ) from safe.common.utilities import safe_dir @@ -95,6 +96,15 @@ def test_definition(self): keyword_definition = definition(keyword) self.assertTrue('description' in keyword_definition) + def test_get_name(self): + """Test get_name method.""" + flood_name = get_name(hazard_flood['key']) + self.assertEqual(flood_name, hazard_flood['name']) + + not_exist_key = 'Mega flux capacitor' + not_found_name = get_name(not_exist_key) + self.assertEqual(not_exist_key, not_found_name) + def test_layer_purpose_for_layer(self): """Test for purpose_for_layer method.""" expected = ['aggregation', 'exposure', 'hazard'] diff --git a/safe/definitions/utilities.py b/safe/definitions/utilities.py index e379523184..19cca27879 100644 --- a/safe/definitions/utilities.py +++ b/safe/definitions/utilities.py @@ -280,6 +280,32 @@ def definition(keyword): return None +def get_name(keyword): + """Given a keyword, try to get the name of it. + + .. versionadded:: 4.2 + + Definition dicts are defined in keywords.py. We try to return + the name if present, otherwise we return none. + + keyword = 'layer_purpose' + kio = safe.utilities.keyword_io.Keyword_IO() + name = kio.get_name(keyword) + print name + + :param keyword: A keyword key. + :type keyword: str + + :returns: The name of the keyword + :rtype: str + """ + definition_dict = definition(keyword) + if definition_dict: + return definition_dict.get('name', keyword) + # Else, return the keyword + return keyword + + def get_allowed_geometries(layer_purpose_key): """Helper function to get all possible geometry diff --git a/safe/gui/widgets/dock.py b/safe/gui/widgets/dock.py index e12f1e07cc..c2585ea220 100644 --- a/safe/gui/widgets/dock.py +++ b/safe/gui/widgets/dock.py @@ -41,7 +41,7 @@ PREPARE_SUCCESS, ) from safe.definitions.provenance import provenance_list -from safe.definitions.utilities import map_report_component +from safe.definitions.utilities import map_report_component, get_name from safe.defaults import supporters_logo_path from safe.definitions.reports import ( final_product_tag, @@ -1435,7 +1435,13 @@ def write_project_variable(key, value): write_project_variable( '%s__%s' % (key, dict_key), dict_value) elif isinstance(value, (bool, str, unicode, Number)): - QgsExpressionContextUtils.setProjectVariable(key, value) + # Don't use get_name for field + if 'field' in key: + pretty_value = get_name(value) + QgsExpressionContextUtils.setProjectVariable( + key, pretty_value) + else: + QgsExpressionContextUtils.setProjectVariable(key, value) elif isinstance(value, type(None)): QgsExpressionContextUtils.setProjectVariable(key, '') elif isinstance(value, datetime): diff --git a/safe/impact_function/impact_function.py b/safe/impact_function/impact_function.py index fed86e2546..e6be896983 100644 --- a/safe/impact_function/impact_function.py +++ b/safe/impact_function/impact_function.py @@ -54,7 +54,11 @@ from safe.gis.raster.polygonize import polygonize from safe.gis.raster.zonal_statistics import zonal_stats from safe.definitions.analysis_steps import analysis_steps -from safe.definitions.utilities import definition, get_non_compulsory_fields +from safe.definitions.utilities import ( + definition, + get_non_compulsory_fields, + get_name +) from safe.definitions.exposure import indivisible_exposure from safe.definitions.fields import ( size_field, @@ -929,14 +933,12 @@ def prepare(self): return status, message # Set the name - hazard_name = definition( - self.hazard.keywords.get('hazard'))['name'] - exposure_name = definition( - self.exposure.keywords.get('exposure'))['name'] - hazard_geometry_name = definition( - self.hazard.keywords.get('layer_geometry'))['name'] - exposure_geometry_name = definition( - self.exposure.keywords.get('layer_geometry'))['name'] + hazard_name = get_name(self.hazard.keywords.get('hazard')) + exposure_name = get_name(self.exposure.keywords.get('exposure')) + hazard_geometry_name = get_name( + self.hazard.keywords.get('layer_geometry')) + exposure_geometry_name = get_name( + self.exposure.keywords.get('layer_geometry')) self._name = tr( '{hazard_type} {hazard_geometry} On {exposure_type} ' '{exposure_geometry}').format( @@ -944,7 +946,7 @@ def prepare(self): hazard_geometry=hazard_geometry_name, exposure_type=exposure_name, exposure_geometry=exposure_geometry_name - ) + ).title() # Set the title if self.exposure.keywords.get('exposure') == 'population':