diff -r df97cade97ac -r 1f276091b42a app/soc/logic/cleaning.py --- a/app/soc/logic/cleaning.py Sun Mar 29 21:35:02 2009 +0000 +++ b/app/soc/logic/cleaning.py Mon Mar 30 19:35:44 2009 +0000 @@ -52,6 +52,12 @@ DEF_NO_SUCH_DOCUMENT_MSG = ugettext( "There is no such document with that link ID under this entity.") +DEF_MUST_BE_ABOVE_LIMIT_FMT = ugettext( + "Must be at least %d characters, it has %d characters.") + +DEF_MUST_BE_UNDER_LIMIT_FMT = ugettext( + "Must be under %d characters, it has %d characters.") + def check_field_is_empty(field_name): """Returns decorator that bypasses cleaning for empty fields. @@ -297,6 +303,36 @@ return wrapper +def clean_content_length(field_name, min_length=0, max_length=500): + """Clean method for cleaning a field which must contain at least min and + not more then max length characters. + + Args: + field_name: the name of the field needed cleaning + min_length: the minimum amount of allowed characters + max_length: the maximum amount of allowed characters + """ + + @check_field_is_empty(field_name) + def wrapper(self): + """Decorator wrapper method. + """ + + value = self.cleaned_data[field_name] + value_length = len(value) + + if value_length < min_length: + raise forms.ValidationError(DEF_MUST_BE_ABOVE_LIMIT_FMT %( + min_length, value_length)) + + if value_length > max_length: + raise forms.ValidationError(DEF_MUST_BE_UNDER_LIMIT_FMT %( + max_length, value_length)) + + return value + return wrapper + + def clean_phone_number(field_name): """Clean method for cleaning a field that may only contain numerical values. """