def update(self, params): """Update a config value.""" assert reload_object(self.user).is_superuser, "Permission denied." if 'name' not in params: raise HandlerPKError("Missing name in params") if 'value' not in params: raise HandlerValidationError("Missing value in params") name = params['name'] value = params['value'] form = get_config_form(name, {name: value}) if form.is_valid(): try: form.save() except ValidationError as e: self._fix_validation_error(name, e.error_dict) raise HandlerValidationError(e.error_dict) return self._include_choice({ 'name': name, 'value': Config.objects.get_config(name), }) else: self._fix_validation_error(name, form.errors) raise HandlerValidationError(form.errors)
def update(self, params): """Update a config value.""" if not self.user.is_superuser: raise HandlerPermissionError() if "name" not in params: raise HandlerPKError("Missing name in params") if "value" not in params: raise HandlerValidationError("Missing value in params") name = params["name"] value = params["value"] try: form = get_config_form(name, {name: value}) except ValidationError: raise HandlerDoesNotExistError(name) if form.is_valid(): try: request = HttpRequest() request.user = self.user form.save(ENDPOINT.UI, request) except ValidationError as e: self._fix_validation_error(name, e.error_dict) raise HandlerValidationError(e.error_dict) return self._include_choice( {"name": name, "value": Config.objects.get_config(name)} ) else: self._fix_validation_error(name, form.errors) raise HandlerValidationError(form.errors)
def get_maas_form(name, value): """Get the Form for the provided name. Most names use a ConfigForm, but some names have been moved out of the Config database and now use different forms. The new form is instantiated here and returned to provide continued access to the values when using this API.""" if name in migrated_config_values: form = migrated_config_values[name].form(data={}) # Copy all initial values to data then set provided value. form.data = form.initial.copy() form.data[name] = value return form return get_config_form(name, {name: value})
def test_get_config_form_returns_initialized_form(self): maas_name = factory.make_string() Config.objects.set_config('maas_name', maas_name) form = get_config_form('maas_name') # The form is populated with the value of the 'maas_name' config item. self.assertEqual({'maas_name': maas_name}, form.initial)