Back to index

python-weblib  1.3.9
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes
pyweblib.forms.Field Class Reference
Inheritance diagram for pyweblib.forms.Field:
Inheritance graph
[legend]

List of all members.

Public Member Functions

def __init__
def idAttrStr
def labelHTML
def getValue
def setDefault
def setRegex
def setValue
def setCharset
def titleHTML
def valueHTML

Public Attributes

 value
 name
 text
 maxLen
 maxValues
 required
 accessKey
 inputHTMLTemplate
 valueHTMLTemplate
 charset
 default

Private Member Functions

def _accessKeyAttr
def _patternAndOptions
def _validateLen
def _validateFormat
def _validateMaxValue
def _encodeValue
def _defaultValue
def _defaultHTML

Private Attributes

 _re

Detailed Description

Base class for all kind of single input fields.

In most cases this class is not used directly
since derivate classes for most types of input fields exist.

Definition at line 42 of file forms.py.


Constructor & Destructor Documentation

def pyweblib.forms.Field.__init__ (   self,
  name,
  text,
  maxLen,
  maxValues,
  pattern,
  required = 0,
  default = None,
  accessKey = '' 
)
name
Field name used in <INPUT NAME="..">
text
User-friendly text describing this field
maxLen
maximum length of a single input value [Bytes]
maxValues
maximum amount of input values
default
default value to be used in method inputfield()
required
flag which marks field as mandantory input field
accessKey
key for accessing this field to be displayed by method inputHTML()
pattern
regex pattern of valid values either as string
or tuple (pattern,options)

Reimplemented in pyweblib.forms.HiddenInput.

Definition at line 52 of file forms.py.

00052 
00053   ):
00054     """
00055     name
00056         Field name used in <INPUT NAME="..">
00057     text
00058         User-friendly text describing this field
00059     maxLen
00060         maximum length of a single input value [Bytes]
00061     maxValues
00062         maximum amount of input values
00063     default
00064         default value to be used in method inputfield()
00065     required
00066         flag which marks field as mandantory input field
00067     accessKey
00068         key for accessing this field to be displayed by method inputHTML()
00069     pattern
00070         regex pattern of valid values either as string
00071         or tuple (pattern,options)
00072     """
00073     self.value = []
00074     self.name = name
00075     self.text = text
00076     self.maxLen = maxLen
00077     self.maxValues = maxValues
00078     self.required = required
00079     self.accessKey = accessKey
00080     self.inputHTMLTemplate = r'%s'
00081     self.valueHTMLTemplate = r'%s'
00082     # Charset is the preferred character set of the browser.
00083     # This is set by Form.add() the something meaningful.
00084     self.charset = 'iso-8859-1'
00085     self.setDefault(default)
00086     self.setRegex(pattern)


Member Function Documentation

def pyweblib.forms.Field._accessKeyAttr (   self) [private]

Definition at line 87 of file forms.py.

00087 
00088   def _accessKeyAttr(self):
00089     if self.accessKey:
00090       return 'accesskey="%s"' % (self.accessKey)
00091     else:
00092       return ''

Here is the caller graph for this function:

def pyweblib.forms.Field._defaultHTML (   self,
  default 
) [private]
HTML output of default.

Definition at line 224 of file forms.py.

00224 
00225   def _defaultHTML(self,default):
00226     """HTML output of default."""
00227     return escapeHTML(self._defaultValue(default)).encode(self.charset)

Here is the call graph for this function:

Here is the caller graph for this function:

def pyweblib.forms.Field._defaultValue (   self,
  default 
) [private]
returns default value

Definition at line 216 of file forms.py.

00216 
00217   def _defaultValue(self,default):
00218     """returns default value"""
00219     return default or self.__dict__.get('default','')

Here is the caller graph for this function:

def pyweblib.forms.Field._encodeValue (   self,
  value 
) [private]
Return Unicode object or string to be stored in self.value

Reimplemented in pyweblib.forms.Keygen, and pyweblib.forms.File.

Definition at line 185 of file forms.py.

00185 
00186   def _encodeValue(self,value):
00187     """
00188     Return Unicode object or string to be stored in self.value
00189     """
00190     try:
00191       value = unicode(value,self.charset)
00192     except UnicodeError:
00193       # Work around buggy browsers...
00194       value = unicode(value,'iso-8859-1')
00195     return value

Here is the caller graph for this function:

def pyweblib.forms.Field._patternAndOptions (   self,
  pattern 
) [private]
The result is a tuple (pattern string,pattern options).

pattern
Either a string containing a regex pattern,
a tuple (pattern string, pattern options) or None.

Definition at line 125 of file forms.py.

00125 
00126   def _patternAndOptions(self,pattern):
00127     """
00128     The result is a tuple (pattern string,pattern options).
00129 
00130     pattern
00131         Either a string containing a regex pattern,
00132         a tuple (pattern string, pattern options) or None.
00133     """
00134     if type(pattern) is types.TupleType:
00135       return pattern
00136     else:
00137       return pattern, 0

Here is the caller graph for this function:

def pyweblib.forms.Field._validateFormat (   self,
  value 
) [private]
Check format of the user's value for this field.

Empty input (zero-length string) are valid in any case.
You might override this method to change this behaviour.

Reimplemented in pyweblib.forms.Radio, and pyweblib.forms.File.

Definition at line 165 of file forms.py.

00165 
00166   def _validateFormat(self,value):
00167     """
00168     Check format of the user's value for this field.
00169 
00170     Empty input (zero-length string) are valid in any case.
00171     You might override this method to change this behaviour.
00172     """
00173     if (not self._re is None) and value:
00174       rm = self._re.match(value)
00175       if rm is None:
00176         raise InvalidValueFormat(
00177           self.name,
00178           self.text.encode(self.charset),
00179           value.encode(self.charset)
00180         )

Here is the caller graph for this function:

def pyweblib.forms.Field._validateLen (   self,
  value 
) [private]
Check length of the user's value for this field.

Definition at line 160 of file forms.py.

00160 
00161   def _validateLen(self,value):
00162     """Check length of the user's value for this field."""
00163     if len(value)>self.maxLen:
00164       raise InvalidValueLen(self.name,self.text,len(value),self.maxLen)

Here is the caller graph for this function:

def pyweblib.forms.Field._validateMaxValue (   self) [private]

Definition at line 181 of file forms.py.

00181 
00182   def _validateMaxValue(self):
00183     if len(self.value)>=self.maxValues:
00184       raise TooManyValues(self.name,self.text,len(self.value),self.maxValues)

Here is the caller graph for this function:

Returns self.value in case of multi-valued input or
self.value[0] if only one value is allowed.

Definition at line 103 of file forms.py.

00103 
00104   def getValue(self):
00105     """
00106     Returns self.value in case of multi-valued input or
00107     self.value[0] if only one value is allowed.
00108     """
00109     if self.maxValues>1:
00110       return self.value
00111     else:
00112       return self.value[0]

def pyweblib.forms.Field.idAttrStr (   self,
  id_value 
)

Definition at line 93 of file forms.py.

00093 
00094   def idAttrStr(self,id_value):
00095     if id_value is None:
00096       return ''
00097     else:
00098       return 'id="%s" ' % (id_value)

Here is the caller graph for this function:

def pyweblib.forms.Field.labelHTML (   self,
  labelText = None,
  for_value = None 
)

Definition at line 99 of file forms.py.

00099 
00100   def labelHTML(self,labelText=None,for_value=None):
00101     labelText = (labelText or self.text).encode(self.charset)
00102     return '<label for="%s">%s</label>' % (for_value or self.name,labelText)

def pyweblib.forms.Field.setCharset (   self,
  charset 
)
Define the character set of the user's input.

Definition at line 212 of file forms.py.

00212 
00213   def setCharset(self,charset):
00214     """Define the character set of the user's input."""
00215     self.charset = charset

def pyweblib.forms.Field.setDefault (   self,
  default 
)
Set the default of a field.

Mainly this is used by the application if self.default shall
be changed after initializing the field object.

Reimplemented in pyweblib.forms.Radio.

Definition at line 113 of file forms.py.

00113 
00114   def setDefault(self,default):
00115     """
00116     Set the default of a field.
00117 
00118     Mainly this is used by the application if self.default shall
00119     be changed after initializing the field object.
00120     """
00121     if type(default)==types.ListType:
00122       self.default = [i for i in default if i!=None]
00123     else:
00124       self.default = default or ''

Here is the caller graph for this function:

def pyweblib.forms.Field.setRegex (   self,
  pattern 
)
Set the regex pattern for validating this field.

Mainly this is used if self._re shall be changed after
the field object was initialized.

pattern
Either a string containing a regex pattern,
a tuple (pattern string, pattern options) or None.
If None regex checking in _validateFormat is switched off
(not recommended).

Reimplemented in pyweblib.forms.Textarea.

Definition at line 138 of file forms.py.

00138 
00139   def setRegex(self,pattern):
00140     """
00141     Set the regex pattern for validating this field.
00142 
00143     Mainly this is used if self._re shall be changed after
00144     the field object was initialized.
00145 
00146     pattern
00147         Either a string containing a regex pattern,
00148         a tuple (pattern string, pattern options) or None.
00149         If None regex checking in _validateFormat is switched off
00150         (not recommended).
00151     """
00152     patternstring,patternoptions = self._patternAndOptions(pattern)
00153     if patternstring is None:
00154       # Regex checking is completely switched off
00155       self._re = None
00156     else:
00157       # This is a Unicode input field
00158       patternoptions = patternoptions | re.U
00159       self._re = re.compile('%s$' % patternstring,patternoptions)

Here is the call graph for this function:

def pyweblib.forms.Field.setValue (   self,
  value 
)
Store the user's value into the field object.

This method can be used to modify the user's value
before storing it into self.value.

Definition at line 196 of file forms.py.

00196 
00197   def setValue(self,value):
00198     """
00199     Store the user's value into the field object.
00200 
00201     This method can be used to modify the user's value
00202     before storing it into self.value.
00203     """
00204     value = self._encodeValue(value)
00205     # Check if input is valid
00206     # Length valid?
00207     self._validateLen(value)
00208     # Format valid?
00209     self._validateFormat(value)
00210     self._validateMaxValue()
00211     self.value.append(value)

Here is the call graph for this function:

def pyweblib.forms.Field.titleHTML (   self,
  title 
)
HTML output of default.

Definition at line 220 of file forms.py.

00220 
00221   def titleHTML(self,title):
00222     """HTML output of default."""
00223     return escapeHTML(title or self.text).encode(self.charset)

Here is the call graph for this function:

Here is the caller graph for this function:

HTML output of self.value using the HTML template string
in self.valueHTMLTemplate.

Reimplemented in pyweblib.forms.Keygen, pyweblib.forms.Password, and pyweblib.forms.Textarea.

Definition at line 228 of file forms.py.

00228 
00229   def valueHTML(self):
00230     """
00231     HTML output of self.value using the HTML template string
00232     in self.valueHTMLTemplate.
00233     """
00234     return [
00235       self.valueHTMLTemplate % v
00236       for v in self.value
00237     ]
00238 


Member Data Documentation

Definition at line 154 of file forms.py.

Definition at line 78 of file forms.py.

Definition at line 83 of file forms.py.

Reimplemented in pyweblib.forms.Radio.

Definition at line 121 of file forms.py.

Definition at line 79 of file forms.py.

Reimplemented in pyweblib.forms.Radio.

Definition at line 75 of file forms.py.

Definition at line 76 of file forms.py.

Definition at line 73 of file forms.py.

Definition at line 77 of file forms.py.

Definition at line 74 of file forms.py.

Definition at line 72 of file forms.py.

Definition at line 80 of file forms.py.


The documentation for this class was generated from the following file: