Ticket #10214: SBLE_internal_81e644e.diff

File SBLE_internal_81e644e.diff, 8.9 KB (added by Will Boyce, 13 years ago)
  • django/conf/global_settings.py

    diff --git a/django/conf/global_settings.py b/django/conf/global_settings.py
    index 0aee63d..f4a75f6 100644
    a b FILE_CHARSET = 'utf-8'  
    142142# E-mail address that error messages come from.
    143143SERVER_EMAIL = 'root@localhost'
    144144
    145 # Whether to send broken-link emails.
    146 SEND_BROKEN_LINK_EMAILS = False
     145# Whether to send broken-link emails. Can be 'all', 'internal' or 'none'.
     146SEND_BROKEN_LINK_EMAILS = 'none'
    147147
    148148# Database connection info.
    149149DATABASES = {
    ALLOWED_INCLUDE_ROOTS = ()  
    245245ADMIN_FOR = ()
    246246
    247247# List of compiled regular expression objects representing URLs that need not
    248 # be reported when SEND_BROKEN_LINK_EMAILS is True. Here are a few examples:
     248# be reported when SEND_BROKEN_LINK_EMAILS is 'all' or 'internal'. Here are a few examples:
    249249#    import re
    250250#    IGNORABLE_404_URLS = (
    251251#        re.compile(r'^/apple-touch-icon.*\.png$'),
  • django/middleware/common.py

    diff --git a/django/middleware/common.py b/django/middleware/common.py
    index 4dd2e14..4609e3d 100644
    a b class CommonMiddleware(object):  
    9292    def process_response(self, request, response):
    9393        "Send broken link emails and calculate the Etag, if needed."
    9494        if response.status_code == 404:
    95             if settings.SEND_BROKEN_LINK_EMAILS and not settings.DEBUG:
     95            if settings.SEND_BROKEN_LINK_EMAILS in ['all', 'internal', True] and not settings.DEBUG:
    9696                # If the referrer was from an internal link or a non-search-engine site,
    9797                # send a note to the managers.
    9898                domain = request.get_host()
    9999                referer = request.META.get('HTTP_REFERER', None)
    100                 is_internal = _is_internal_request(domain, referer)
    101100                path = request.get_full_path()
    102                 if referer and not _is_ignorable_404(path) and (is_internal or '?' not in referer):
    103                     ua = request.META.get('HTTP_USER_AGENT', '<none>')
    104                     ip = request.META.get('REMOTE_ADDR', '<none>')
    105                     mail_managers("Broken %slink on %s" % ((is_internal and 'INTERNAL ' or ''), domain),
    106                         "Referrer: %s\nRequested URL: %s\nUser agent: %s\nIP address: %s\n" \
    107                                   % (referer, request.get_full_path(), ua, ip),
    108                                   fail_silently=True)
     101                if referer and not _is_ignorable_404(path):
     102                    is_internal = _is_internal_request(domain, referer)
     103                    notify = is_internal if settings.SEND_BROKEN_LINK_EMAILS == 'internal' else ('?' not in referer)
     104                    if notify:
     105                        ua = request.META.get('HTTP_USER_AGENT', '<none>')
     106                        ip = request.META.get('REMOTE_ADDR', '<none>')
     107                        mail_managers("Broken %slink on %s" % ((is_internal and 'INTERNAL ' or ''), domain),
     108                            "Referrer: %s\nRequested URL: %s\nUser agent: %s\nIP address: %s\n" \
     109                                    % (referer, request.get_full_path(), ua, ip),
     110                                    fail_silently=True)
    109111                return response
    110112
    111113        # Use ETags, if requested.
  • docs/howto/error-reporting.txt

    diff --git a/docs/howto/error-reporting.txt b/docs/howto/error-reporting.txt
    index 64af2a0..ddb9e65 100644
    a b not found" errors). Django sends emails about 404 errors when:  
    5858
    5959* :setting:`DEBUG` is ``False``
    6060
    61 * :setting:`SEND_BROKEN_LINK_EMAILS` is ``True``
     61* :setting:`SEND_BROKEN_LINK_EMAILS` is ``'all'`` or ``'internal'``
    6262
    6363* Your :setting:`MIDDLEWARE_CLASSES` setting includes ``CommonMiddleware``
    6464  (which it does by default).
    crawlers often request::  
    9595periods to escape them.)
    9696
    9797The best way to disable this behavior is to set
    98 :setting:`SEND_BROKEN_LINK_EMAILS` to ``False``.
     98:setting:`SEND_BROKEN_LINK_EMAILS` to 'none'.
    9999
    100100.. seealso::
    101101
  • docs/ref/middleware.txt

    diff --git a/docs/ref/middleware.txt b/docs/ref/middleware.txt
    index 737e0b2..802e658 100644
    a b Adds a few conveniences for perfectionists:  
    6262  normalize URLs.
    6363
    6464* Sends broken link notification emails to :setting:`MANAGERS` if
    65   :setting:`SEND_BROKEN_LINK_EMAILS` is set to ``True``.
     65  :setting:`SEND_BROKEN_LINK_EMAILS` is set to ``'all'`` or ``'internal'``.
    6666
    6767* Handles ETags based on the :setting:`USE_ETAGS` setting. If
    6868  :setting:`USE_ETAGS` is set to ``True``, Django will calculate an ETag
  • docs/ref/settings.txt

    diff --git a/docs/ref/settings.txt b/docs/ref/settings.txt
    index a35d99a..c2cf443 100644
    a b ignored when reporting HTTP 404 errors via email (see  
    11331133commonly requested file such as ``favicon.ico`` or ``robots.txt``, or if it
    11341134gets hammered by script kiddies.
    11351135
    1136 This is only used if :setting:`SEND_BROKEN_LINK_EMAILS` is set to ``True`` and
     1136This is only used if :setting:`SEND_BROKEN_LINK_EMAILS` is set to ``'all'`` or ``'internal'`` and
    11371137``CommonMiddleware`` is installed (see :doc:`/topics/http/middleware`).
    11381138
    11391139.. setting:: INSTALLED_APPS
    MANAGERS  
    13301330Default: ``()`` (Empty tuple)
    13311331
    13321332A tuple in the same format as :setting:`ADMINS` that specifies who should get
    1333 broken-link notifications when ``SEND_BROKEN_LINK_EMAILS=True``.
     1333broken-link notifications when ``SEND_BROKEN_LINK_EMAILS`` is ``'all'`` or ``'internal'``.
    13341334
    13351335.. setting:: MEDIA_ROOT
    13361336
    better. ``django-admin.py startproject`` creates one automatically.  
    15331533SEND_BROKEN_LINK_EMAILS
    15341534-----------------------
    15351535
    1536 Default: ``False``
     1536Default: ``'none'``
    15371537
    15381538Whether to send an email to the :setting:`MANAGERS` each time somebody visits
    15391539a Django-powered page that is 404ed with a non-empty referer (i.e., a broken
    15401540link). This is only used if ``CommonMiddleware`` is installed (see
    1541 :doc:`/topics/http/middleware`). See also :setting:`IGNORABLE_404_URLS` and
     1541:doc:`/topics/http/middleware`). Valid values are ``'none'``, ``'all'``
     1542and ``'internal'``. See also :setting:`IGNORABLE_404_URLS` and
    15421543:doc:`/howto/error-reporting`.
    15431544
    15441545.. setting:: SERIALIZATION_MODULES
  • tests/regressiontests/middleware/tests.py

    diff --git a/tests/regressiontests/middleware/tests.py b/tests/regressiontests/middleware/tests.py
    index 124eb19..272d784 100644
    a b class CommonMiddlewareTest(TestCase):  
    262262
    263263    def test_404_error_reporting(self):
    264264        settings.IGNORABLE_404_URLS = (re.compile(r'foo'),)
    265         settings.SEND_BROKEN_LINK_EMAILS = True
     265        settings.SEND_BROKEN_LINK_EMAILS = 'all'
    266266        request = self._get_request('regular_url/that/does/not/exist')
    267267        request.META['HTTP_REFERER'] = '/another/url/'
    268268        response = self.client.get(request.path)
    class CommonMiddlewareTest(TestCase):  
    272272
    273273    def test_404_error_reporting_no_referer(self):
    274274        settings.IGNORABLE_404_URLS = (re.compile(r'foo'),)
    275         settings.SEND_BROKEN_LINK_EMAILS = True
     275        settings.SEND_BROKEN_LINK_EMAILS = 'all'
    276276        request = self._get_request('regular_url/that/does/not/exist')
    277277        response = self.client.get(request.path)
    278278        CommonMiddleware().process_response(request, response)
    class CommonMiddlewareTest(TestCase):  
    280280
    281281    def test_404_error_reporting_ignored_url(self):
    282282        settings.IGNORABLE_404_URLS = (re.compile(r'foo'),)
    283         settings.SEND_BROKEN_LINK_EMAILS = True
     283        settings.SEND_BROKEN_LINK_EMAILS = 'all'
    284284        request = self._get_request('foo_url/that/does/not/exist/either')
    285285        request.META['HTTP_REFERER'] = '/another/url/'
    286286        response = self.client.get(request.path)
    287287        CommonMiddleware().process_response(request, response)
    288288        self.assertEqual(len(mail.outbox), 0)
    289289
     290    def test_404_error_reporting_internal(self):
     291        settings.IGNORABLE_404_URLS = (re.compile(r'foo'),)
     292        settings.SEND_BROKEN_LINK_EMAILS = 'internal'
     293        request = self._get_request('regular_url/that/does/not/exist')
     294        request.META['HTTP_REFERER'] = 'http://testserver/another/url/'
     295        response = self.client.get(request.path)
     296        CommonMiddleware().process_response(request, response)
     297        self.assertEqual(len(mail.outbox), 1)
     298        self.assertIn('Broken', mail.outbox[0].subject)
     299
     300    def test_404_error_reporting_external_ignore(self):
     301        settings.IGNORABLE_404_URLS = (re.compile(r'foo'),)
     302        settings.SEND_BROKEN_LINK_EMAILS = 'internal'
     303        request = self._get_request('regular_url/that/does/not/exist')
     304        request.META['HTTP_REFERER'] = '/another/url/'
     305        response = self.client.get(request.path)
     306        CommonMiddleware().process_response(request, response)
     307        self.assertEqual(len(mail.outbox), 0)
     308
    290309
    291310class ConditionalGetMiddlewareTest(TestCase):
    292311    urls = 'regressiontests.middleware.cond_get_urls'
Back to Top