/[glsr]/trunk/core/fieldparse.py
Gentoo

Diff of /trunk/core/fieldparse.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 118 Revision 119
24 @keywords: A list of valid Field options. 24 @keywords: A list of valid Field options.
25 """ 25 """
26 26
27 self.name = name 27 self.name = name
28 self.dest = keywords.get("dest", name) 28 self.dest = keywords.get("dest", name)
29 self.default = keywords.get("default", None) 29 self.default = str(keywords.get("default", ""))
30 self.required = keywords.get("required", False) 30 self.required = keywords.get("required", False)
31 self.filter = keywords.get("filter", None) 31 self.filter = keywords.get("ffilter", None)
32 self.regex = keywords.get("regex", None) 32 self.regex = keywords.get("regex", None)
33 self.min_size = keywords.get("min_size", 0) 33 self.min_size = keywords.get("min_size", 0)
34 self.max_size = keywords.get("max_size", -1) 34 self.max_size = keywords.get("max_size", -1)
35 self.type = keywords.get("type", None) 35 self.type = keywords.get("ftype", None)
36 self.value = self.default 36 self.value = self.default
37 37
38 self.TYPE_CHECKER = { 38 self.TYPE_CHECKER = {
39 "int": self._check_builtin, 39 "int": self._check_builtin,
40 "float": self._check_builtin, 40 "float": self._check_builtin,
41 } 41 }
42 42
43 def __str__(self): 43 def __str__(self):
44 return str(self.__dict__) 44 return str(self.__dict__)
45 45
46 def _check_builtin(self): 46 def _check_builtin(self, value):
47 47
48 try: 48 try:
49 return eval(self.type)(self.value) 49 eval(self.type)(value)
50 except ValueError: 50 except ValueError:
51 return False 51 return False
52
53 return True
52 54
53 def set_value(self, value): 55 def set_value(self, value, check = True):
54 """Set the value of the field.""" 56 """Set the value of the field."""
55 self.value = value
56 57
58 if not check or value == "" or self.check_value(value):
59 self.value = value
60 return True
61
62 self.value = ""
63 raise FieldParseError("Value type mismatch for '%s'" % self.name)
64
57 def check_value(self): 65 def check_value(self, value):
58 66
59 checker = self.TYPE_CHECKER.get(self.type) 67 checker = self.TYPE_CHECKER.get(self.type)
60 if checker is None: 68 if checker is None:
61 # If no checker is set, then the test returns successfull 69 # If no checker is set, then the test returns successfull
62 return True 70 return True
63 else: 71 else:
64 return checker 72 return checker(value)
65 73
66class FieldParser: 74class FieldParser:
67 """Manage and parse fields on a form.""" 75 """Manage and parse fields on a form."""
68 76
69 def __init__(self, request = None, error_method = None): 77 def __init__(self, request = None, error_method = None):
92 from cgi import escape 100 from cgi import escape
93 101
94 for field in self.fields: 102 for field in self.fields:
95 if field.required and field.value is None: 103 if field.required and field.value is None:
96 field.set_value("") 104 field.set_value("")
97 self.error_method("Missing required field '%s'" % field, 105 raise FieldParseError("Missing required field '%s'" % field)
98 self._get_values())
99 return False
100 106
101 if field.value is None: 107 if field.value is None:
102 continue 108 continue
103 109
104 err_msg = "" 110 err_msg = ""
105 if field.max_size > -1 and len(field.value) > field.max_size: 111 if field.max_size > -1 and len(field.value) > field.max_size:
106 err_msg = ("%s is too big! Maximum is %s characters." % 112 err_msg = ("%s is too big! Maximum is %s characters." %
107 (field.name, field.max_size)) 113 (field.name, field.max_size))
108 114
115 print "here!!!!!!!!!!!!!!!!!!!!!!!!!"
116 print field.value, field.name, type(field.value)
109 if len(field.value) < field.min_size: 117 if len(field.value) < field.min_size:
110 err_msg = ("%s is too small! Minimum is %s characters" % 118 err_msg = ("%s is too small! Minimum is %s characters" %
111 (field.name, field.min_size)) 119 (field.name, field.min_size))
120 print "here2!!!!!!!!!!!!!!!!!!!!!!!!!"
112 121
113 if field.regex is not None: 122 if field.regex is not None:
114 regex = re.compile(field.regex, re.UNICODE) 123 regex = re.compile(field.regex, re.UNICODE)
115 if not regex.match(field.value): 124 if not regex.match(field.value):
116 err_msg = ("%s contained illegal characters." % 125 err_msg = ("%s contained illegal characters." %
117 (field.name)) 126 (field.name))
118 127
119 if err_msg != "": 128 if err_msg != "":
120 field.set_value("") 129 field.set_value("")
121 self.error_method(err_msg, self._get_values()) 130 raise FieldParseError(err_msg)
122 return False
123 131
124 if field.filter is not None: 132 if field.filter is not None:
125 field.set_value(field.filter(field.value)) 133 field.set_value(field.filter(field.value))
126 134
127 return True 135 return True
149 157
150 @return: dictionary containing all fields and their respective values. 158 @return: dictionary containing all fields and their respective values.
151 """ 159 """
152 160
153 # Get the field values from the form. 161 # Get the field values from the form.
162 err = ""
154 for field in self.fields: 163 for field in self.fields:
164 try:
165 # err == "" is used to enable/disable the check. If an error
166 # has already been raised then don't check any more, just set.
167 # This way all of our field data will still be populated
168 # instead of choking on the first type mismatch.
155 field.set_value(self.request.getvalue(field.name, field.default)) 169 form_value = self.request.getvalue(field.name, field.default)
170 field.set_value(form_value, err == "")
171
172 except FieldParseError, err_msg:
173 err = err_msg
156 174
157 if not self._check_fields(): 175 if err != "":
176 self.error_method(err, self._get_values())
158 return 177 return False
178
179 try:
180 self._check_fields()
181
182 except FieldParseError, err_msg:
183 self.error_method(err_msg, self._get_values())
184 return False
159 185
160 return self._get_values() 186 return self._get_values()
161 187
162 def set_requester(self, request): 188 def set_requester(self, request):
163 """Set the requester object. 189 """Set the requester object.

Legend:
Removed from v.118  
changed lines
  Added in v.119

  ViewVC Help
Powered by ViewVC 1.1.20