def _convert_to_python(self, value, state): self.assert_string(value, state) try: value = value.encode('ascii', 'strict') except UnicodeEncodeError: raise Invalid(self.message('phoneFormat', state), value, state) if unicode is str: # Python 3 value = value.decode('ascii') value = self._mark_chars_re.sub('-', value) for f, t in [(' ', ' '), ('--', '-'), (' - ', '-'), ('- ', '-'), (' -', '-')]: value = value.replace(f, t) value = self._perform_rex_transformation(value, self._preTransformations) if self.default_cc: if callable(self.default_cc): cc = self.default_cc() else: cc = self.default_cc value = self._prepend_country_code(value, self._ccIncluder, cc) value = self._perform_rex_transformation(value, self._postTransformations) value = value.replace(' ', '') # did we successfully transform that phone number? Thus, is it valid? if not self._phoneIsSane.search(value): raise Invalid(self.message('phoneFormat', state), value, state) return value
def _validate_python(self, value, state): value = str(value).strip().upper() if not value: raise Invalid(self.message('empty', state), value, state) if not value or len(value) != 2: raise Invalid(self.message('wrongLength', state), value, state) if value not in self.states and not (self.extra_states and value in self.extra_states): raise Invalid(self.message('invalid', state), value, state)
def _to_python(self, value, state): self.assert_string(value, state) match = self.regex.search(value) if not match: raise Invalid( self.message('invalid', state) % self.format, value, state) return self.delimiter.join(match.groups())
def _convert_to_python(self, value, state): self.assert_string(value, state) match = self.regex.search(value) if not match: raise Invalid(self.message('invalid', state), value, state) return '%s%s%s' % (match.group(1).upper(), match.group(2), match.group(3).upper())
def _to_python(self, value, state): self.assert_string(value, state) match = self._phoneRE.search(value) if not match: raise Invalid( self.message('phoneFormat', state), value, state) return value
def _convert_from_python(self, value, state): self.assert_string(value, state) match = self._phoneRE.search(value) if not match: raise Invalid(self.message('phoneFormat', state), value, state) result = '%s-%s-%s' % (match.group(1), match.group(2), match.group(3)) if match.group(4): result += " ext.%s" % match.group(4) return result
def validate_python(self, fields_dict, state): if fields_dict[self.country_field] in self._vd: try: zip_validator = self._vd[fields_dict[self.country_field]]() fields_dict[self.zip_field] = zip_validator.to_python(fields_dict[self.zip_field]) except Invalid, e: message = self.message('badFormat', state) raise Invalid(message, fields_dict, state, error_dict = {self.zip_field: e.message, self.country_field: message})
def _to_python(self, value, state): upval = value.upper() if self.key_ok: try: c = get_language(value) return value except: pass for k, v in get_languages(): if v.upper() == upval: return k raise Invalid(self.message('valueNotFound', state), value, state)
def _convert_to_python(self, value, state): upval = value.upper() if self.key_ok: try: get_language(value) except Exception: pass else: return value for k, v in get_languages(): if v.upper() == upval: return k raise Invalid(self.message('valueNotFound', state), value, state)