← Back to team overview

launchpad-reviewers team mailing list archive

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

 

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

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

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

This branch adds a base class for forms that save the content of their fields in the database (using the Config model).  This base class will be used to create the various maas configuration forms.
-- 
https://code.launchpad.net/~rvb/maas/config-form/+merge/93989
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~rvb/maas/config-form into lp:maas.
=== modified file 'src/maasserver/forms.py'
--- src/maasserver/forms.py	2012-02-21 13:34:20 +0000
+++ src/maasserver/forms.py	2012-02-21 13:34:20 +0000
@@ -20,9 +20,13 @@
     UserCreationForm,
     )
 from django.contrib.auth.models import User
-from django.forms import ModelForm
+from django.forms import (
+    Form,
+    ModelForm,
+    )
 from maasserver.fields import MACAddressFormField
 from maasserver.models import (
+    Config,
     MACAddress,
     Node,
     NODE_AFTER_COMMISSIONING_ACTION,
@@ -120,3 +124,36 @@
         model = User
         fields = (
             'username', 'first_name', 'last_name', 'email', 'is_superuser')
+
+
+class ConfigForm(Form):
+    """A base class for forms that save the content of their fields into
+    Config objects.
+    """
+
+    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}
+
+    def save(self):
+        """Save the content of the fields into the database.
+
+        :return: Whether or not the content of the fields was valid and hence
+            sucessfully saved into the detabase.
+        :rtype: boolean
+        """
+        self.full_clean()
+        if self._errors:
+            return False
+        else:
+            for name, value in self.cleaned_data.items():
+                Config.objects.set_config(name, value)
+            return True
+
+
+class MaasOptionsForm(ConfigForm):
+    title = forms.CharField(label="MaaS name")
+    provide_dhcp = forms.BooleanField(
+        label="Provide DHCP on this subnet", required=False)

=== modified file 'src/maasserver/tests/test_forms.py'
--- src/maasserver/tests/test_forms.py	2012-01-31 12:15:08 +0000
+++ src/maasserver/tests/test_forms.py	2012-02-21 13:34:20 +0000
@@ -11,9 +11,17 @@
 __metaclass__ = type
 __all__ = []
 
+from django import forms
 from django.http import QueryDict
-from maasserver.forms import NodeWithMACAddressesForm
-from maasserver.testing import TestCase
+from maasserver.forms import (
+    ConfigForm,
+    NodeWithMACAddressesForm,
+    )
+from maasserver.models import Config
+from maasserver.testing import (
+    factory,
+    TestCase,
+    )
 
 
 class NodeWithMACAddressesFormTest(TestCase):
@@ -84,3 +92,39 @@
         self.assertSequenceEqual(
             ['aa:bb:cc:dd:ee:ff', '9a:bb:c3:33:e5:7f'],
             [mac.mac_address for mac in node.macaddress_set.all()])
+
+
+class TestOptionForm(ConfigForm):
+
+    field1 = forms.CharField(label="Field 1", max_length=10)
+    field2 = forms.BooleanField(label="Field 2", required=False)
+
+
+class ConfigFormTest(TestCase):
+
+    def test_form_valid_saves_into_db(self):
+        value = factory.getRandomString(10)
+        form = TestOptionForm({'field1': value, 'field2': False})
+        result = form.save()
+
+        self.assertTrue(result)
+        self.assertEqual(value, Config.objects.get_config('field1'))
+        self.assertFalse(Config.objects.get_config('field2'))
+
+    def test_form_invalid_does_not_save_into_db(self):
+        value_too_long = factory.getRandomString(20)
+        form = TestOptionForm({'field1': value_too_long, 'field2': False})
+        result = form.save()
+
+        self.assertFalse(result)
+        self.assertIn('field1', form._errors)
+        self.assertIsNone(Config.objects.get_config('field1'))
+        self.assertIsNone(Config.objects.get_config('field2'))
+
+    def test_form_loads_initial_values(self):
+        value = factory.getRandomString()
+        Config.objects.set_config('field1', value)
+        form = TestOptionForm()
+
+        self.assertItemsEqual(['field1'], form.initial)
+        self.assertEqual(value, form.initial['field1'])