← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~rvb/maas/maas-settings-config-form-fix into lp:maas

 

Raphaël Badin has proposed merging lp:~rvb/maas/maas-settings-config-form-fix into lp:maas with lp:~rvb/maas/maas-settings-commi as a prerequisite.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~rvb/maas/maas-settings-config-form-fix/+merge/95123

This branch fixes the ConfigForm.  ConfigForm must use Config.objects.get_config method to fetch initial values to be able to fetch the values DEFAULT_CONFIG if no custom config values are present in the database.
-- 
https://code.launchpad.net/~rvb/maas/maas-settings-config-form-fix/+merge/95123
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~rvb/maas/maas-settings-config-form-fix into lp:maas.
=== modified file 'src/maasserver/forms.py'
--- src/maasserver/forms.py	2012-02-29 08:10:27 +0000
+++ src/maasserver/forms.py	2012-02-29 08:10:27 +0000
@@ -142,8 +142,14 @@
     def __init__(self, *args, **kwargs):
         super(ConfigForm, self).__init__(*args, **kwargs)
         if 'initial' not in kwargs:
-            configs = Config.objects.filter(name__in=list(self.fields))
-            self.initial = {config.name: config.value for config in configs}
+            self._load_initials()
+
+    def _load_initials(self):
+        self.initial = {}
+        for name in list(self.fields):
+            conf = Config.objects.get_config(name)
+            if conf is not None:
+                self.initial[name] = conf
 
     def save(self):
         """Save the content of the fields into the database.

=== modified file 'src/maasserver/tests/test_forms.py'
--- src/maasserver/tests/test_forms.py	2012-02-21 13:38:03 +0000
+++ src/maasserver/tests/test_forms.py	2012-02-29 08:10:27 +0000
@@ -17,7 +17,10 @@
     ConfigForm,
     NodeWithMACAddressesForm,
     )
-from maasserver.models import Config
+from maasserver.models import (
+    Config,
+    DEFAULT_CONFIG,
+    )
 from maasserver.testing import (
     factory,
     TestCase,
@@ -127,3 +130,11 @@
 
         self.assertItemsEqual(['field1'], form.initial)
         self.assertEqual(value, form.initial['field1'])
+
+    def test_form_loads_initial_values_from_default_value(self):
+        value = factory.getRandomString()
+        DEFAULT_CONFIG['field1'] = value
+        form = TestOptionForm()
+
+        self.assertItemsEqual(['field1'], form.initial)
+        self.assertEqual(value, form.initial['field1'])