Module editor
Helpers to validate form datum
- Imports:
- nagare
- Classes:
- Editor(object): An editor object act as a buffer between the datum received from a form an the target object.
- Property(var.Var): An editor property
Class editor.Editor
- Bases:
- object
An editor object act as a buffer between the datum received from a form an the target object.
An editor object has a set of editor properties, each with a possible validating function, and will modified the target object only if all the validating functions are passed ok.
- Methods:
- __init__(self, target, properties_to_create=()): Initialisation
- commit(self, properties_to_commit=(), properties_to_validate=()): Write back the value of the property to the target object, only if they are all valid
- is_validated(self, properties_to_validate): Check the validity of a set of properties
Method editor.Editor.__init__
- Arguments:
- self, target, properties_to_create=()
Initialisation
Create the set of properties from some attributes of the target object
- In:
- target - the target object
- properties_to_create -- name of the attritbutes to read from the target object, then to write into
Method editor.Editor.commit
- Arguments:
- self, properties_to_commit=(), properties_to_validate=()
Write back the value of the property to the target object, only if they are all valid
- In:
- properties_to_commit -- names of properties to check then to write to the target object
- properties_to_validate -- additional names of properties to check
- Return:
- are all the properties valid ?
Method editor.Editor.is_validated
- Arguments:
- self, properties_to_validate
Check the validity of a set of properties
- In:
- properties_to_validate -- name of the properties to validate
- Return
- a boolean
Class editor.Property
- Bases:
- var.Var
An editor property
An editor property has:
- a current value: this value is always set
- a valid value: this value is only set if the validating function has validated it
- a validating function: a function that raise an exception if the value is invalid else, return the value to set
- the message of the last validation error or None if the current value is valid
- Methods:
- __init__(self, v=None): Initialisation
- _validate(self, input): Default validation function
- commit(self, o, name): Set the attribute name of the object o with the valid value
- set(self, input): Set the values
- validate(self, f): Set the validating function
Method editor.Property.__init__
- Arguments:
- self, v=None
Initialisation
Members are:
- self.input -- the current value
- self.value -- the last valid value
- self.error -- the last error message
Method editor.Property._validate
- Arguments:
- self, input
Default validation function
- In:
- input -- value to validate
- Return
- input
Method editor.Property.commit
- Arguments:
- self, o, name
Set the attribute name of the object o with the valid value
- In:
- o -- target object
- name -- name of the attribute to set in the target object
Method editor.Property.set
- Arguments:
- self, input
Set the values
Always set the current value but only the valid value if the validating function return OK
- In:
- input -- the input string or cgi.FieldStorage object
Method editor.Property.validate
- Arguments:
- self, f
Set the validating function
- In:
- f -- the validating function. Called with the value to validate. Raise the exception ValueError is the value is invalid. Return the value to be set (i.e a validation function can do conversions too)
- Return:
- self