from django import forms


# found on the interwebs, class decorator which injects string stripping
# into forms. since django refuses to have this as a standard or an optional
# field flag, we decorate stuff on our own.
def autostrip(cls):
    fields = [(key, value) for key, value in cls.base_fields.items() if isinstance(value, forms.CharField)]
    for field_name, field_object in fields:
        def get_clean_func(original_clean):
            return lambda value: original_clean(value and value.strip())

        clean_func = get_clean_func(getattr(field_object, 'clean'))
        setattr(field_object, 'clean', clean_func)
    return cls
