def patch_forms(self, local_apps): """ Patch all the forms to strip whitespaces from field input. """ def is_form(member): """ Allow only custom made classes which are a subclass from BaseForm to pass. """ if not inspect.isclass(member): return False if not issubclass(member, BaseForm): return False return member.__module__.startswith(self.name) for app in local_apps: try: __import__('%s.forms' % app) except ImportError: continue else: forms_module = sys.modules['%s.forms' % app] form_classes = inspect.getmembers(forms_module, lambda member: is_form(member)) for form_name, form in form_classes: # Wrap the reference to this form with a function that strips the input from whitespace. if hasattr(form, 'base_fields'): form_class = autostrip(form) setattr(forms_module, form_name, form_class)
def ready(self): """ Code run on startup of django. """ # Call atfork because on heroku everything is a fork Random.atfork() # Patch all the forms to strip whitespace from field input local_installed_apps = [ app for app in settings.INSTALLED_APPS if app.startswith('%s.' % __name__) ] for app in local_installed_apps: try: __import__('%s.forms' % app) except ImportError: continue else: forms_module = sys.modules['%s.forms' % app] form_classes = inspect.getmembers( forms_module, lambda member: is_form(member)) for form_name, form in form_classes: # Wrap the reference to this form with a function that strips the input from whitespace. if hasattr(form, 'base_fields'): form_class = autostrip(form) setattr(forms_module, form_name, form_class)
def ready(self): """ Code run on startup of django. """ # Patch all the forms to strip whitespace from field input local_installed_apps = [app for app in settings.INSTALLED_APPS if app.startswith('%s.' % __name__)] for app in local_installed_apps: try: __import__('%s.forms' % app) except ImportError: continue else: forms_module = sys.modules['%s.forms' % app] form_classes = inspect.getmembers(forms_module, lambda member: self.is_form(member)) for form_name, form in form_classes: # Wrap the reference to this form with a function that strips the input from whitespace. if hasattr(form, 'base_fields'): form_class = autostrip(form) setattr(forms_module, form_name, form_class)
local_installed_apps = [app for app in settings.INSTALLED_APPS if app.startswith('%s.' % __name__)] def is_form(member): """ Allow only custom made classes which are a subclass from BaseForm to pass. """ if not inspect.isclass(member): return False if not issubclass(member, BaseForm): return False return member.__module__.startswith(__name__) for app in local_installed_apps: try: __import__('%s.forms' % app) except ImportError: continue else: forms_module = sys.modules['%s.forms' % app] form_classes = inspect.getmembers(forms_module, lambda member: is_form(member)) for form_name, form in form_classes: # Wrap the reference to this form with a function that strips the input from whitespace. if hasattr(form, 'base_fields'): form_class = autostrip(form) setattr(forms_module, form_name, form_class)