diff options
author | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2014-01-21 23:21:03 +0100 |
---|---|---|
committer | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2014-01-21 23:33:22 +0100 |
commit | dccb37f551d19d9dba68d35a888941cde64f861e (patch) | |
tree | 0984f0abf19c003a58d0025c84a848cc21ed5a3d /factory | |
parent | 9323fbeea374394833987cb710ac9becb7726a44 (diff) | |
download | factory-boy-dccb37f551d19d9dba68d35a888941cde64f861e.tar factory-boy-dccb37f551d19d9dba68d35a888941cde64f861e.tar.gz |
Improve mute_signals (Closes #122).
Diffstat (limited to 'factory')
-rw-r--r-- | factory/__init__.py | 2 | ||||
-rw-r--r-- | factory/django.py | 36 | ||||
-rw-r--r-- | factory/helpers.py | 4 |
3 files changed, 21 insertions, 21 deletions
diff --git a/factory/__init__.py b/factory/__init__.py index 251306a..b4e63be 100644 --- a/factory/__init__.py +++ b/factory/__init__.py @@ -79,7 +79,5 @@ from .helpers import ( lazy_attribute_sequence, container_attribute, post_generation, - - prevent_signals, ) diff --git a/factory/django.py b/factory/django.py index b502923..6f39c34 100644 --- a/factory/django.py +++ b/factory/django.py @@ -221,22 +221,22 @@ class ImageField(FileField): return thumb_io.getvalue() -class PreventSignals(object): +class mute_signals(object): """Temporarily disables and then restores any django signals. Args: *signals (django.dispatch.dispatcher.Signal): any django signals Examples: - with prevent_signals(pre_init): + with mute_signals(pre_init): user = UserFactory.build() ... - @prevent_signals(pre_save, post_save) + @mute_signals(pre_save, post_save) class UserFactory(factory.Factory): ... - @prevent_signals(post_save) + @mute_signals(post_save) def generate_users(): UserFactory.create_batch(10) """ @@ -247,7 +247,7 @@ class PreventSignals(object): def __enter__(self): for signal in self.signals: - logger.debug('PreventSignals: Disabling signal handlers %r', + logger.debug('mute_signals: Disabling signal handlers %r', signal.receivers) self.paused[signal] = signal.receivers @@ -255,22 +255,28 @@ class PreventSignals(object): def __exit__(self, exc_type, exc_value, traceback): for signal, receivers in self.paused.items(): - logger.debug('PreventSignals: Restoring signal handlers %r', + logger.debug('mute_signals: Restoring signal handlers %r', receivers) signal.receivers = receivers self.paused = {} - def __call__(self, func): - if isinstance(func, types.FunctionType): - @functools.wraps(func) + def __call__(self, callable_obj): + if isinstance(callable_obj, base.FactoryMetaClass): + generate_method = getattr(callable_obj, '_generate') + + @functools.wraps(generate_method) + def wrapped_generate(*args, **kwargs): + with self: + return generate_method(*args, **kwargs) + + callable_obj._generate = wrapped_generate + return callable_obj + + else: + @functools.wraps(callable_obj) def wrapper(*args, **kwargs): with self: - return func(*args, **kwargs) + return callable_obj(*args, **kwargs) return wrapper - generate_method = getattr(func, '_generate', None) - if generate_method: - func._generate = classmethod(self(generate_method.__func__)) - - return func
\ No newline at end of file diff --git a/factory/helpers.py b/factory/helpers.py index 719d76d..4a2a254 100644 --- a/factory/helpers.py +++ b/factory/helpers.py @@ -140,7 +140,3 @@ def container_attribute(func): def post_generation(fun): return declarations.PostGeneration(fun) - - -def prevent_signals(*signals): - return django.PreventSignals(*signals)
\ No newline at end of file |