app/soc/logic/cleaning.py
changeset 1962 7c9e517f2089
parent 1935 99ec4bbd70f5
child 1984 eb02fd5427c5
equal deleted inserted replaced
1961:85a57ba6f6fa 1962:7c9e517f2089
   278 
   278 
   279 
   279 
   280 def clean_ascii_only(field_name):
   280 def clean_ascii_only(field_name):
   281   """Clean method for cleaning a field that may only contain ASCII-characters.
   281   """Clean method for cleaning a field that may only contain ASCII-characters.
   282   """
   282   """
   283   @check_field_is_empty(field_name)
   283 
   284   def wrapper(self):
   284   @check_field_is_empty(field_name)
   285     """Decorator wrapper method.
   285   def wrapper(self):
   286     """
   286     """Decorator wrapper method.
       
   287     """
       
   288 
   287     value = self.cleaned_data.get(field_name)
   289     value = self.cleaned_data.get(field_name)
   288 
   290 
   289     try:
   291     try:
   290       # encode to ASCII
   292       # encode to ASCII
   291       value = value.encode("ascii")
   293       value = value.encode("ascii")
   292     except UnicodeEncodeError:
   294     except UnicodeEncodeError:
   293       # can not encode as ASCII
   295       # can not encode as ASCII
   294       raise forms.ValidationError("Only ASCII characters are allowed")
   296       raise forms.ValidationError("Only ASCII characters are allowed")
       
   297 
       
   298     return value
       
   299   return wrapper
       
   300 
       
   301 
       
   302 def clean_numeric_only(field_name):
       
   303   """Clean method for cleaning a field that may only contain numerical values.
       
   304   """
       
   305 
       
   306   @check_field_is_empty(field_name)
       
   307   def wrapper(self):
       
   308     """Decorator wrapped method.
       
   309     """
       
   310 
       
   311     value = self.cleaned_data.get(field_name)
       
   312 
       
   313     if not value.isdigit():
       
   314       raise forms.ValidationError("Only numerical characters are allowed")
   295 
   315 
   296     return value
   316     return value
   297   return wrapper
   317   return wrapper
   298 
   318 
   299 
   319