MCPcopy
hub / github.com/django/django / override_settings

Class override_settings

django/test/utils.py:483–565  ·  view source on GitHub ↗

Act as either a decorator or a context manager. If it's a decorator, take a function and return a wrapped function. If it's a contextmanager, use it with the ``with`` statement. In either event, entering/exiting are called before and after, respectively, the function/block is execut

Source from the content-addressed store, hash-verified

481
482
483class override_settings(TestContextDecorator):
484 """
485 Act as either a decorator or a context manager. If it's a decorator, take a
486 function and return a wrapped function. If it's a contextmanager, use it
487 with the ``with`` statement. In either event, entering/exiting are called
488 before and after, respectively, the function/block is executed.
489 """
490
491 enable_exception = None
492
493 def __init__(self, **kwargs):
494 self.options = kwargs
495 super().__init__()
496
497 def enable(self):
498 # Keep this code at the beginning to leave the settings unchanged
499 # in case it raises an exception because INSTALLED_APPS is invalid.
500 if "INSTALLED_APPS" in self.options:
501 try:
502 apps.set_installed_apps(self.options["INSTALLED_APPS"])
503 except Exception:
504 apps.unset_installed_apps()
505 raise
506 override = UserSettingsHolder(settings._wrapped)
507 for key, new_value in self.options.items():
508 setattr(override, key, new_value)
509 self.wrapped = settings._wrapped
510 settings._wrapped = override
511 for key, new_value in self.options.items():
512 try:
513 setting_changed.send(
514 sender=settings._wrapped.__class__,
515 setting=key,
516 value=new_value,
517 enter=True,
518 )
519 except Exception as exc:
520 self.enable_exception = exc
521 self.disable()
522
523 def disable(self):
524 if "INSTALLED_APPS" in self.options:
525 apps.unset_installed_apps()
526 settings._wrapped = self.wrapped
527 del self.wrapped
528 responses = []
529 for key in self.options:
530 new_value = getattr(settings, key, None)
531 responses_for_setting = setting_changed.send_robust(
532 sender=settings._wrapped.__class__,
533 setting=key,
534 value=new_value,
535 enter=False,
536 )
537 responses.extend(responses_for_setting)
538 if self.enable_exception is not None:
539 exc = self.enable_exception
540 self.enable_exception = None

Callers 15

rtlMethod · 0.90
setUpClassMethod · 0.90
settingsMethod · 0.90
tests.pyFile · 0.90
test_same_originMethod · 0.90
test_denyMethod · 0.90
test_dont_set_if_setMethod · 0.90
test_response_exemptMethod · 0.90
test_is_extendableMethod · 0.90
test_coop_onMethod · 0.90

Calls

no outgoing calls

Tested by 15

setUpClassMethod · 0.72
settingsMethod · 0.72
test_same_originMethod · 0.72
test_denyMethod · 0.72
test_dont_set_if_setMethod · 0.72
test_response_exemptMethod · 0.72
test_is_extendableMethod · 0.72
test_coop_onMethod · 0.72
test_messagesMethod · 0.72