| 1 |
""" |
|---|
| 2 |
Copyright (c) 2004, CherryPy Team (team@cherrypy.org) |
|---|
| 3 |
All rights reserved. |
|---|
| 4 |
|
|---|
| 5 |
Redistribution and use in source and binary forms, with or without modification, |
|---|
| 6 |
are permitted provided that the following conditions are met: |
|---|
| 7 |
|
|---|
| 8 |
* Redistributions of source code must retain the above copyright notice, |
|---|
| 9 |
this list of conditions and the following disclaimer. |
|---|
| 10 |
* Redistributions in binary form must reproduce the above copyright notice, |
|---|
| 11 |
this list of conditions and the following disclaimer in the documentation |
|---|
| 12 |
and/or other materials provided with the distribution. |
|---|
| 13 |
* Neither the name of the CherryPy Team nor the names of its contributors |
|---|
| 14 |
may be used to endorse or promote products derived from this software |
|---|
| 15 |
without specific prior written permission. |
|---|
| 16 |
|
|---|
| 17 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
|---|
| 18 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|---|
| 19 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|---|
| 20 |
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
|---|
| 21 |
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|---|
| 22 |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|---|
| 23 |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|---|
| 24 |
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
|---|
| 25 |
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 26 |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 27 |
""" |
|---|
| 28 |
|
|---|
| 29 |
""" |
|---|
| 30 |
Simple form handling module. |
|---|
| 31 |
""" |
|---|
| 32 |
|
|---|
| 33 |
import cherrypy |
|---|
| 34 |
import defaultformmask |
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
class FormField: |
|---|
| 38 |
|
|---|
| 39 |
def __init__(self, label, name, typ, mask=None, mandatory=0, size=15, |
|---|
| 40 |
optionList=[], defaultValue='', defaultMessage='', validate=None): |
|---|
| 41 |
self.isField = 1 |
|---|
| 42 |
self.label = label |
|---|
| 43 |
self.name = name |
|---|
| 44 |
self.typ = typ |
|---|
| 45 |
if mask is None: |
|---|
| 46 |
self.mask = defaultformmask.defaultMask |
|---|
| 47 |
else: |
|---|
| 48 |
self.mask = mask |
|---|
| 49 |
self.mandatory = mandatory |
|---|
| 50 |
self.size = size |
|---|
| 51 |
self.optionList = optionList |
|---|
| 52 |
self.defaultValue = defaultValue |
|---|
| 53 |
self.defaultMessage = defaultMessage |
|---|
| 54 |
self.validate = validate |
|---|
| 55 |
self.errorMessage = "" |
|---|
| 56 |
|
|---|
| 57 |
def render(self, leaveValues): |
|---|
| 58 |
if leaveValues: |
|---|
| 59 |
if self.typ !='submit': |
|---|
| 60 |
self.currentValue = cherrypy.request.paramMap.get(self.name, "") |
|---|
| 61 |
else: |
|---|
| 62 |
self.currentValue = self.defaultValue |
|---|
| 63 |
else: |
|---|
| 64 |
self.currentValue = self.defaultValue |
|---|
| 65 |
self.errorMessage = self.defaultMessage |
|---|
| 66 |
return self.mask(self) |
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
class FormSeparator: |
|---|
| 70 |
|
|---|
| 71 |
def __init__(self, label, mask): |
|---|
| 72 |
self.isField = 0 |
|---|
| 73 |
self.label = label |
|---|
| 74 |
self.mask = mask |
|---|
| 75 |
|
|---|
| 76 |
def render(self, dummy): |
|---|
| 77 |
return self.mask(self.label) |
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
class Form: |
|---|
| 81 |
|
|---|
| 82 |
method = "post" |
|---|
| 83 |
enctype = "" |
|---|
| 84 |
|
|---|
| 85 |
def formView(self, leaveValues=0): |
|---|
| 86 |
if self.enctype: |
|---|
| 87 |
enctypeTag = 'enctype="%s"' % self.enctype |
|---|
| 88 |
else: |
|---|
| 89 |
enctypeTag = "" |
|---|
| 90 |
|
|---|
| 91 |
res = ['<form method="%s" %s action="postForm">' |
|---|
| 92 |
% (self.method, enctypeTag)] |
|---|
| 93 |
for field in self.fieldList: |
|---|
| 94 |
res.append(field.render(leaveValues)) |
|---|
| 95 |
res.append["</form>"] |
|---|
| 96 |
|
|---|
| 97 |
return "".join(res) |
|---|
| 98 |
|
|---|
| 99 |
def validateFields(self): |
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
pass |
|---|
| 103 |
|
|---|
| 104 |
def validateForm(self): |
|---|
| 105 |
|
|---|
| 106 |
for field in self.fieldList: |
|---|
| 107 |
if field.isField: |
|---|
| 108 |
field.errorMessage = "" |
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
for field in self.fieldList: |
|---|
| 112 |
if (field.isField and field.mandatory |
|---|
| 113 |
and not cherrypy.request.paramMap.get(field.name)): |
|---|
| 114 |
field.errorMessage = "Missing" |
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
for field in self.fieldList: |
|---|
| 118 |
if field.isField and field.validate and not field.errorMessage: |
|---|
| 119 |
value = cherrypy.request.paramMap.get(field.name, "") |
|---|
| 120 |
field.errorMessage = field.validate(value) |
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
self.validateFields() |
|---|
| 124 |
for field in self.fieldList: |
|---|
| 125 |
if field.isField and field.errorMessage: |
|---|
| 126 |
return 0 |
|---|
| 127 |
return 1 |
|---|
| 128 |
|
|---|
| 129 |
def setFieldErrorMessage(self, fieldName, errorMessage): |
|---|
| 130 |
for field in self.fieldList: |
|---|
| 131 |
if field.isField and field.name == fieldName: |
|---|
| 132 |
field.errorMessage = errorMessage |
|---|
| 133 |
|
|---|
| 134 |
def getFieldOptionList(self, fieldName): |
|---|
| 135 |
for field in self.fieldList: |
|---|
| 136 |
if field.isField and field.name == fieldName: |
|---|
| 137 |
return field.optionList |
|---|
| 138 |
|
|---|
| 139 |
def getFieldDefaultValue(self, fieldName): |
|---|
| 140 |
for field in self.fieldList: |
|---|
| 141 |
if field.isField and field.name == fieldName: |
|---|
| 142 |
return field.defaultValue |
|---|
| 143 |
|
|---|
| 144 |
def setFieldDefaultValue(self, fieldName, defaultValue): |
|---|
| 145 |
for field in self.fieldList: |
|---|
| 146 |
if field.isField and field.name == fieldName: |
|---|
| 147 |
field.defaultValue = defaultValue |
|---|
| 148 |
|
|---|
| 149 |
def getFieldNameList(self, exceptList=[]): |
|---|
| 150 |
fieldNameList = [] |
|---|
| 151 |
for field in self.fieldList: |
|---|
| 152 |
if field.isField and field.name and field.name not in exceptList: |
|---|
| 153 |
fieldNameList.append(field.name) |
|---|
| 154 |
return fieldNameList |
|---|
| 155 |
|
|---|
| 156 |
|
|---|