Commits

Gael Pasgrimaud committed 2646d98

s/update/set/ and add instructions and metadata as keyword argument. also raise an error on invalid argument

  • Participants
  • Parent commits 4c2955d
  • Branches new_api

Comments (0)

Files changed (2)

formalchemy/fields.py

             setattr(copied, attr, value)
         return copied
 
-    def update(self, **kwattrs):
+    def set(self, **kwattrs):
         """
         Update field attributes in place. Allowed attributes are: validate,
-        renderer, readonly, nul_as, label, multiple, options, size::
+        renderer, readonly, nul_as, label, multiple, options, size,
+        instructions, metadata::
 
             >>> field = Field('myfield')
-            >>> field.update(label='My field', renderer=SelectFieldRenderer,
-            ...              options=[('Value', 1)])
+            >>> field.set(label='My field', renderer=SelectFieldRenderer,
+            ...            options=[('Value', 1)])
             AttributeField(myfield)
             >>> field.label_text
             'My field'
             value = kwattrs.pop(attr)
             if attr == 'validate':
                 self.validators.append(value)
+            elif attr == 'metadata':
+                self.metadata.update(value)
+            elif attr == 'instructions':
+                self.metadata['instructions'] = value
             elif attr in mapping:
                 attr = mapping.get(attr)
                 setattr(self, attr, value)
                 if attr == 'options' and value is not None:
                     value = _normalized_options(value)
                 self.render_opts[attr] = value
+            else:
+                raise ValueError('Invalid argument %s' % attr)
         return self
 
     def with_null_as(self, option):
         self._value = value
         self.is_relation = False
         self.is_scalar_relation = False
-        self.update(**kwattrs)
+        self.set(**kwattrs)
 
-    def update(self, **kwattrs):
+    def set(self, **kwattrs):
         if 'value' in kwattrs:
             self._value = kwattrs.pop('value')
-        return AbstractField.update(self, **kwattrs)
+        return AbstractField.set(self, **kwattrs)
 
     def model_value(self):
         return self.raw_value

formalchemy/tests/test_fieldset_api.py

 
     """
 
-def field_update():
+def field_set():
     """
     >>> fs = FieldSet(User)
     >>> fs.insert(fs.password, Field('login'))
     >>> def validate(value, field):
     ...     if len(value) < 2: raise ValidationError('Need more than 2 chars')
-    >>> fs.password.update(renderer=PasswordFieldRenderer, validate=validate)
+    >>> fs.password.set(renderer=PasswordFieldRenderer, validate=validate)
     AttributeField(password)
     >>> fs.password.renderer
     <PasswordFieldRenderer for AttributeField(password)>
     >>> fs.password.validators # doctest: +ELLIPSIS
     [<function required at ...>, <function validate at ...>]
 
+    >>> fs.password.set(instructions='Put a password here')
+    AttributeField(password)
+    >>> fs.password.metadata
+    {'instructions': 'Put a password here'}
+
     >>> field = Field('password', value='passwd', renderer=PasswordFieldRenderer)
     >>> field.renderer
     <PasswordFieldRenderer for AttributeField(password)>
     >>> field.raw_value
     'passwd'
-    >>> field.update(value='new_passwd')
+
+    >>> field.set(value='new_passwd')
     AttributeField(password)
     >>> field.raw_value
     'new_passwd'
+
+    >>> field.set(non_exist=True)
+    Traceback (most recent call last):
+    ...
+    ValueError: Invalid argument non_exist
+    
+
     """