def save_resource(self): """Accept the add/edit of the current resource. """ # -- # Hackorama to get this working outside the method that the # parameters where defined in. parameters_widget = [ self.parameters_scrollarea.layout().itemAt(i) for i in range(self.parameters_scrollarea.layout().count())][0] parameters = parameters_widget.widget().get_parameters() # To store parameters, we need the english version. translated_to_english = dict( (y, x) for x, y in list(self.resource_parameters.items())) resource = {} for parameter in parameters: resource[translated_to_english[parameter.name]] = parameter.value # verify the parameters are ok - create a throw-away resource param try: parameter = ResourceParameter() parameter.name = resource['Resource name'] parameter.help_text = resource['Resource description'] # Adding in the frequency property. This is not in the # FloatParameter by default, so maybe we should subclass. parameter.frequency = resource['Frequency'] parameter.description = NeedsProfile.format_sentence( resource['Readable sentence'], resource) parameter.minimum_allowed_value = float( resource['Minimum allowed']) parameter.maximum_allowed_value = float( resource['Maximum allowed']) parameter.unit.name = resource['Unit'] parameter.unit.plural = resource['Units'] parameter.unit.abbreviation = resource['Unit abbreviation'] parameter.value = float(resource['Default']) except ValueOutOfBounds as e: warning = self.tr( 'Problem - default value is invalid') + '\n' + str(e) # noinspection PyTypeChecker,PyArgumentList QMessageBox.warning(None, 'InaSAFE', warning) return except InvalidMaximumError as e: warning = self.tr( 'Problem - maximum value is invalid') + '\n' + str(e) # noinspection PyTypeChecker,PyArgumentList QMessageBox.warning(None, 'InaSAFE', warning) return except InvalidMinimumError as e: warning = self.tr( 'Problem - minimum value is invalid') + '\n' + str(e) # noinspection PyTypeChecker,PyArgumentList QMessageBox.warning(None, 'InaSAFE', warning) return # end of test for parameter validity self.add_resource(resource) self.switch_context(self.profile_edit_page)
def get_needs_parameters(self): """Get the minimum needs resources in parameter format :returns: The minimum needs resources wrapped in parameters. :rtype: list """ parameters = [] for resource in self.minimum_needs['resources']: parameter = ResourceParameter() parameter.name = resource['Resource name'] parameter.help_text = resource['Resource description'] # Adding in the frequency property. This is not in the # FloatParameter by default, so maybe we should subclass. parameter.frequency = resource['Frequency'] parameter.description = self.format_sentence( resource['Readable sentence'], resource) parameter.minimum_allowed_value = float( resource['Minimum allowed']) parameter.maximum_allowed_value = float( resource['Maximum allowed']) parameter.unit.name = resource['Unit'] parameter.unit.plural = resource['Units'] parameter.unit.abbreviation = resource['Unit abbreviation'] parameter.value = float(resource['Default']) # choose highest precision between resource's parameters # start with default of 1 precisions = [1] precision_influence = [ 'Maximum allowed', 'Minimum allowed', 'Default' ] for element in precision_influence: resource_element = str(resource[element]) if resource[element] is not None and '.' in resource_element: precisions.append(self.precision_of(resource_element)) parameter.precision = max(precisions) parameters.append(parameter) prov_parameter = TextParameter() prov_parameter.name = tr('Provenance') prov_parameter.description = tr('The provenance of minimum needs') prov_parameter.help_text = tr('The provenance of minimum needs') try: prov_parameter.value = self.provenance except TypeError: prov_parameter.value = '' parameters.append(prov_parameter) return parameters
def get_needs_parameters(self): """Get the minimum needs resources in parameter format :returns: The minimum needs resources wrapped in parameters. :rtype: list """ parameters = [] for resource in self.minimum_needs['resources']: parameter = ResourceParameter() parameter.name = resource['Resource name'] parameter.help_text = resource['Resource description'] # Adding in the frequency property. This is not in the # FloatParameter by default, so maybe we should subclass. parameter.frequency = resource['Frequency'] parameter.description = self.format_sentence( resource['Readable sentence'], resource) parameter.minimum_allowed_value = float( resource['Minimum allowed']) parameter.maximum_allowed_value = float( resource['Maximum allowed']) parameter.unit.name = resource['Unit'] parameter.unit.plural = resource['Units'] parameter.unit.abbreviation = resource['Unit abbreviation'] parameter.value = float(resource['Default']) # choose highest precision between resource's parameters # start with default of 1 precisions = [1] precision_influence = [ 'Maximum allowed', 'Minimum allowed', 'Default'] for element in precision_influence: resource_element = str(resource[element]) if resource[element] is not None and '.' in resource_element: precisions.append(self.precision_of(resource_element)) parameter.precision = max(precisions) parameters.append(parameter) prov_parameter = TextParameter() prov_parameter.name = tr('Provenance') prov_parameter.description = tr('The provenance of minimum needs') prov_parameter.help_text = tr('The provenance of minimum needs') try: prov_parameter.value = self.provenance except TypeError: prov_parameter.value = '' parameters.append(prov_parameter) return parameters
def test_all(self): """Basic test of all properties.""" unit = Unit() unit.name = 'metre' unit.plural = 'metres' unit.abbreviation = 'm' unit.description = ( '<b>metres</b> are a metric unit of measure. There are 100 ' 'centimetres in 1 metre.'), unit.help_text = 'Help for metre unit' parameter = ResourceParameter() parameter.is_required = True parameter.minimum_allowed_value = 1.0 parameter.maximum_allowed_value = 2.0 parameter.value = 1.123 parameter.frequency = 'weekly' parameter.unit = unit self.assertEqual(1.123, parameter.value) self.assertDictEqual(unit.serialize(), parameter.unit.serialize()) self.assertEqual('weekly', parameter.frequency)