Example #1
0
 def test_DictCharField_sets_default_value_for_subfields(self):
     default_value = factory.make_name('default_value')
     multi_field = DictCharField(
         [('field_a', forms.CharField(
             label='Field a', initial=default_value))],
         required=False)
     self.assertEquals(
         default_value, multi_field.clean_sub_fields('')['field_a'])
Example #2
0
 def test_DictCharField_sets_default_value_for_subfields(self):
     default_value = factory.make_name("default_value")
     multi_field = DictCharField(
         [(
             "field_a",
             forms.CharField(label="Field a", initial=default_value),
         )],
         required=False,
     )
     self.assertEquals(default_value,
                       multi_field.clean_sub_fields("")["field_a"])
Example #3
0
 class FakeForm(forms.Form):
     multi_field = DictCharField(
         [
             ('field_a', forms.CharField(label='Field a')),
             ('field_b', forms.CharField(
                 label='Field b', required=False, max_length=3)),
         ])
Example #4
0
 class FakeFormSkip(forms.Form):
     multi_field = DictCharField(
         [(
             field_name,
             forms.CharField(label=field_label, max_length=3),
         )],
         skip_check=True,
     )
Example #5
0
def get_driver_parameters_from_json(
    json_power_type_parameters,
    initial_power_params=None,
    skip_check=False,
    scope=None,
):
    """Return power type parameters.

    :param json_power_type_parameters: Power type parameters expressed
        as a JSON string or as set of JSONSchema-verifiable objects.
        Will be validated using jsonschema.validate().
    :type json_power_type_parameters: JSON string or iterable.
    :param initial_power_params: Power paramaters that were already set, any
        field which matches will have its initial value set.
    :type initial_power_params: dict
    :param skip_check: Whether the field should be checked or not.
    :type skip_check: bool
    :return: A dict of power parameters for all power types, indexed by
        power type name.
    """
    validate(json_power_type_parameters, JSON_POWER_DRIVERS_SCHEMA)
    power_parameters = {
        # Empty type, for the case where nothing is entered in the form yet.
        "": DictCharField([], required=False, skip_check=True)
    }
    if initial_power_params is None:
        initial_power_params = []
    for power_type in json_power_type_parameters:
        fields = []
        has_required_field = False
        for json_field in power_type["fields"]:
            # Skip fields that do not match the scope.
            if scope is not None and json_field["scope"] != scope:
                continue
            field_name = json_field["name"]
            if field_name in initial_power_params:
                json_field["default"] = initial_power_params[field_name]
            has_required_field = has_required_field or json_field["required"]
            fields.append((json_field["name"], make_form_field(json_field)))
        params = DictCharField(fields,
                               required=has_required_field,
                               skip_check=skip_check)
        power_parameters[power_type["name"]] = params
    return power_parameters
Example #6
0
 def test_expands_dict_fields(self):
     field_name = factory.make_name("field_name")
     sub_fields = {
         factory.make_name("sub_field"): CharField()
         for _ in range(3)
     }
     fields = {field_name: DictCharField(sub_fields)}
     defaults = {
         "%s_%s" % (field_name, field): factory.make_name("subfield")
         for field in sub_fields.keys()
     }
     data = {field_name: DictCharField(fields)}
     results = get_overridden_query_dict(defaults, data, fields)
     expected = {key: Equals(value) for key, value in defaults.items()}
     expected.update({
         name: IsInstance(value.__class__)
         for name, value in fields.items()
     })
     self.assertThat(results, MatchesDict(expected))
Example #7
0
 class FakeForm(forms.Form):
     multi_field = DictCharField([
         ("field_a", forms.CharField(label="Field a")),
         (
             "field_b",
             forms.CharField(label="Field b",
                             required=False,
                             max_length=3),
         ),
     ])
Example #8
0
 def test_DictCharField_init(self):
     testField = DictCharField([
         ('field_a', forms.CharField(label='Field a')),
         ('field_b', forms.CharField(label='Field b')),
         ('field_c', forms.CharField(label='Field c')),
     ])
     self.assertEqual(['field_a', 'field_b', 'field_c'], testField.names)
     self.assertEqual(['field_a', 'field_b', 'field_c'],
                      testField.widget.names)
     self.assertEqual(
         [field.widget for field in testField.field_dict.values()],
         testField.widget.widgets)
Example #9
0
 def test_DictCharField_init(self):
     testField = DictCharField(
         [
             ('field_a', forms.CharField(label='Field a')),
             ('field_b', forms.CharField(label='Field b')),
             ('field_c', forms.CharField(label='Field c')),
         ])
     self.assertEqual(['field_a', 'field_b', 'field_c'], testField.names)
     self.assertEqual(
         ['field_a', 'field_b', 'field_c'], testField.widget.names)
     # In Django 1.11, widgets are deep-copied in the Field constructor,
     # so instead of comparing directly, we compare their types and names.
     expected_widgets = [
         type(widget) for widget in testField.widget.widgets]
     got_widgets = [
         type(field.widget) for field in testField.field_dict.values()]
     self.assertEqual(got_widgets, expected_widgets)
Example #10
0
 class FakeFormRequiredFalse(forms.Form):
     multi_field = DictCharField(
         [("field_a", forms.CharField(label="Field a"))],
         required=False)
     char_field = forms.CharField(label="Field a")
Example #11
0
 class FakeFormRequiredFalse(forms.Form):
     multi_field = DictCharField(
         [('field_a', forms.CharField(label='Field a'))],
         required=False)
     char_field = forms.CharField(label='Field a')
Example #12
0
__all__ = [
    'POWER_TYPE_PARAMETERS',
]

from django import forms
from maasserver.config_forms import DictCharField
from maasserver.fields import MACAddressFormField
from provisioningserver.enum import (
    IPMI_DRIVER,
    IPMI_DRIVER_CHOICES,
    POWER_TYPE,
)

POWER_TYPE_PARAMETERS = {
    POWER_TYPE.DEFAULT:
    DictCharField([], required=False, skip_check=True),
    POWER_TYPE.WAKE_ON_LAN:
    DictCharField([
        ('mac_address', MACAddressFormField(label="MAC Address",
                                            required=False)),
    ],
                  required=False,
                  skip_check=True),
    POWER_TYPE.VMWARE:
    DictCharField([
        ('power_address', forms.CharField(label="Address", required=False)),
        ('power_user', forms.CharField(label="Username", required=False)),
        ('power_pass',
         forms.CharField(widget=forms.PasswordInput(render_value=True),
                         label="Password",
                         required=False)),