Commit fb9f1b9b authored by Aymeric Augustin's avatar Aymeric Augustin
Browse files

Removed backwards-compatibility shim for #16288.

Also unit-tested django.utils.log.RequireDebugTrue for consistency.
parent 067505ad
Loading
Loading
Loading
Loading
+1 −38
Original line number Diff line number Diff line
@@ -74,11 +74,8 @@ class LazySettings(LazyObject):

            logging_config_func(DEFAULT_LOGGING)

            if self.LOGGING:
                # Backwards-compatibility shim for #16288 fix
                compat_patch_logging_config(self.LOGGING)

            # ... then invoke it with the logging settings
            if self.LOGGING:
                logging_config_func(self.LOGGING)

    def configure(self, default_settings=global_settings, **options):
@@ -195,37 +192,3 @@ class UserSettingsHolder(BaseSettings):
        return list(self.__dict__) + dir(self.default_settings)

settings = LazySettings()



def compat_patch_logging_config(logging_config):
    """
    Backwards-compatibility shim for #16288 fix. Takes initial value of
    ``LOGGING`` setting and patches it in-place (issuing deprecation warning)
    if "mail_admins" logging handler is configured but has no filters.

    """
    #  Shim only if LOGGING["handlers"]["mail_admins"] exists,
    #  but has no "filters" key
    if "filters" not in logging_config.get(
        "handlers", {}).get(
        "mail_admins", {"filters": []}):

        warnings.warn(
            "You have no filters defined on the 'mail_admins' logging "
            "handler: adding implicit debug-false-only filter. "
            "See http://docs.djangoproject.com/en/dev/releases/1.4/"
            "#request-exceptions-are-now-always-logged",
            DeprecationWarning)

        filter_name = "require_debug_false"

        filters = logging_config.setdefault("filters", {})
        while filter_name in filters:
            filter_name = filter_name + "_"

        filters[filter_name] = {
            "()": "django.utils.log.RequireDebugFalse",
        }

        logging_config["handlers"]["mail_admins"]["filters"] = [filter_name]
+10 −61
Original line number Diff line number Diff line
@@ -5,12 +5,12 @@ import logging
import sys
import warnings

from django.conf import compat_patch_logging_config, LazySettings
from django.conf import LazySettings
from django.core import mail
from django.test import TestCase, RequestFactory
from django.test.utils import override_settings
from django.utils.encoding import force_text
from django.utils.log import CallbackFilter, RequireDebugFalse
from django.utils.log import CallbackFilter, RequireDebugFalse, RequireDebugTrue
from django.utils.six import StringIO
from django.utils.unittest import skipUnless

@@ -40,46 +40,10 @@ OLD_LOGGING = {
}


class PatchLoggingConfigTest(TestCase):
    """
    Tests for backward-compat shim for #16288. These tests should be removed in
    Django 1.6 when that shim and DeprecationWarning are removed.

    """
    def test_filter_added(self):
        """
        Test that debug-false filter is added to mail_admins handler if it has
        no filters.

        """
        config = copy.deepcopy(OLD_LOGGING)

        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            compat_patch_logging_config(config)
            self.assertEqual(len(w), 1)

        self.assertEqual(
            config["handlers"]["mail_admins"]["filters"],
            ['require_debug_false'])

    def test_filter_configuration(self):
        """
        Test that the auto-added require_debug_false filter is an instance of
        `RequireDebugFalse` filter class.

        """
        config = copy.deepcopy(OLD_LOGGING)
        with warnings.catch_warnings(record=True):
            compat_patch_logging_config(config)

        flt = config["filters"]["require_debug_false"]
        self.assertEqual(flt["()"], "django.utils.log.RequireDebugFalse")

class LoggingFiltersTest(TestCase):
    def test_require_debug_false_filter(self):
        """
        Test the RequireDebugFalse filter class.

        """
        filter_ = RequireDebugFalse()

@@ -89,32 +53,17 @@ class PatchLoggingConfigTest(TestCase):
        with self.settings(DEBUG=False):
            self.assertEqual(filter_.filter("record is not used"), True)

    def test_no_patch_if_filters_key_exists(self):
        """
        Test that the logging configuration is not modified if the mail_admins
        handler already has a "filters" key.

        """
        config = copy.deepcopy(OLD_LOGGING)
        config["handlers"]["mail_admins"]["filters"] = []
        new_config = copy.deepcopy(config)
        compat_patch_logging_config(new_config)

        self.assertEqual(config, new_config)

    def test_no_patch_if_no_mail_admins_handler(self):
    def test_require_debug_true_filter(self):
        """
        Test that the logging configuration is not modified if the mail_admins
        handler is not present.

        Test the RequireDebugTrue filter class.
        """
        config = copy.deepcopy(OLD_LOGGING)
        config["handlers"].pop("mail_admins")
        new_config = copy.deepcopy(config)
        compat_patch_logging_config(new_config)
        filter_ = RequireDebugTrue()

        self.assertEqual(config, new_config)
        with self.settings(DEBUG=True):
            self.assertEqual(filter_.filter("record is not used"), True)

        with self.settings(DEBUG=False):
            self.assertEqual(filter_.filter("record is not used"), False)

class DefaultLoggingTest(TestCase):
    def setUp(self):
+1 −1

File changed.

Contains only whitespace changes.