Ticket #15954: 15954.2.patch

File 15954.2.patch, 12.5 KB (added by Aymeric Augustin, 13 years ago)
  • docs/internals/deprecation.txt

     
    196196
    197197        * The UK-prefixed objects of ``django.contrib.localflavor.uk`` will only
    198198          be accessible through their new GB-prefixed names (GB is the correct
    199           ISO 3166 code for United Kingdom). They have been depreacted since the
     199          ISO 3166 code for United Kingdom). They have been deprecated since the
    200200          1.4 release.
    201201
     202        * The :setting:`IGNORABLE_404_STARTS` and :setting:`IGNORABLE_404_ENDS`
     203          settings have been superseded by :setting:`IGNORABLE_404_URLS` in
     204          the 1.4 release. They will be removed.
     205
    202206    * 2.0
    203207        * ``django.views.defaults.shortcut()``. This function has been moved
    204208          to ``django.contrib.contenttypes.views.shortcut()`` as part of the
  • docs/howto/error-reporting.txt

     
    6666those are usually just people typing in broken URLs or broken Web 'bots).
    6767
    6868You can tell Django to stop reporting particular 404s by tweaking the
    69 :setting:`IGNORABLE_404_ENDS` and :setting:`IGNORABLE_404_STARTS` settings. Both
    70 should be a tuple of strings. For example::
     69:setting:`IGNORABLE_404_URLS` setting. It should be a tuple of compiled
     70regular expression objects. For example::
    7171
    72     IGNORABLE_404_ENDS = ('.php', '.cgi')
    73     IGNORABLE_404_STARTS = ('/phpmyadmin/',)
     72    import re
     73    IGNORABLE_404_URLS = (
     74        re.compile(r'\.(php|cgi)$'),
     75        re.compile(r'^/phpmyadmin/'),
     76    )
    7477
    7578In this example, a 404 to any URL ending with ``.php`` or ``.cgi`` will *not* be
    7679reported. Neither will any URL starting with ``/phpmyadmin/``.
    7780
     81The following example shows how to exclude some conventional URLs that browsers and
     82crawlers often request::
     83
     84    import re
     85    IGNORABLE_404_URLS = (
     86        re.compile(r'^/apple-touch-icon.*\.png$'),
     87        re.compile(r'^/favicon.ico$),
     88        re.compile(r'^/robots.txt$),
     89    )
     90
     91
    7892The best way to disable this behavior is to set
    7993:setting:`SEND_BROKEN_LINK_EMAILS` to ``False``.
    8094
     
    93107   records are ignored, but you can use them for error reporting by writing a
    94108   handler and :doc:`configuring logging </topics/logging>` appropriately.
    95109
     110.. seealso::
     111
     112   .. versionchanged:: 1.4
     113
     114   Previously, two settings were used to control which URLs not to report:
     115   :setting:`IGNORABLE_404_STARTS` and :setting:`IGNORABLE_404_ENDS`. They
     116   were replaced by :setting:`IGNORABLE_404_URLS`.
  • docs/releases/1.4.txt

     
    176176
    177177For more details see the docs about
    178178:doc:`customizing the comments framework </ref/contrib/comments/custom>`.
     179
     180`IGNORABLE_404_STARTS` and `IGNORABLE_404_ENDS` settings
     181~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     182
     183Django can report 404 errors: see :doc:`/howto/error-reporting`.
     184Until Django 1.3, it was possible to exclude some URLs from the reporting
     185by adding prefixes to :setting:`IGNORABLE_404_STARTS` and suffixes to
     186:setting:`IGNORABLE_404_ENDS`.
     187
     188In Django 1.4, these two settings are superseded by
     189:setting:`IGNORABLE_404_URLS`, which is a list of compiled regular expressions.
     190Django won't send an email for 404 errors on URLs that match any of them.
     191
     192Furthermore, the previous settings had some rather arbitrary default values::
     193
     194    IGNORABLE_404_STARTS = ('/cgi-bin/', '/_vti_bin', '/_vti_inf')
     195    IGNORABLE_404_ENDS = ('mail.pl', 'mailform.pl', 'mail.cgi', 'mailform.cgi',
     196                          'favicon.ico', '.php')
     197
     198It's not Django's role to decide if your website has a legacy ``/cgi-bin/``
     199section or a ``favicon.ico``. As a consequence, the default value of
     200:setting:`IGNORABLE_404_URLS` is now empty.
     201
     202If you have customized :setting:`IGNORABLE_404_STARTS` and
     203:setting:`IGNORABLE_404_ENDS`, or if you want to keep the old default value,
     204you should add the following lines in your settings file::
     205
     206    import re
     207    IGNORABLE_404_URLS = (
     208        # for each <prefix> in IGNORABLE_404_STARTS
     209        re.compile(r'^<prefix>'),
     210        # for each <suffix> in IGNORABLE_404_ENDS
     211        re.compile(r'<suffix>$'),
     212    )
     213
     214Don't forget to escape characters that have a special meaning in a regular
     215expression.
  • docs/ref/settings.txt

     
    10201020``SHORT_DATETIME_FORMAT``, ``FIRST_DAY_OF_WEEK``, ``DECIMAL_SEPARATOR``,
    10211021``THOUSAND_SEPARATOR`` and ``NUMBER_GROUPING``.
    10221022
    1023 .. setting:: IGNORABLE_404_ENDS
     1023.. setting:: IGNORABLE_404_URLS
    10241024
    1025 IGNORABLE_404_ENDS
     1025IGNORABLE_404_URLS
    10261026------------------
    10271027
    1028 Default: ``('mail.pl', 'mailform.pl', 'mail.cgi', 'mailform.cgi', 'favicon.ico', '.php')``
     1028.. versionadded:: 1.4
    10291029
    1030 See also ``IGNORABLE_404_STARTS`` and ``Error reporting via email``.
     1030Default: ``()``
    10311031
    1032 .. setting:: IGNORABLE_404_STARTS
     1032List of compiled regular expression objects describing URLs that should be
     1033ignored when reporting HTTP 404 errors via email (see
     1034:doc:`/howto/error-reporting`). Use this if your site does not provide a
     1035commonly requested file such as ``favicon.ico`` or ``robots.txt``, or if it
     1036gets hammered by script kiddies.
    10331037
    1034 IGNORABLE_404_STARTS
    1035 --------------------
     1038This is only used if :setting:`SEND_BROKEN_LINK_EMAILS` is set to ``True`` and
     1039``CommonMiddleware`` is installed (see :doc:`/topics/http/middleware`).
    10361040
    1037 Default: ``('/cgi-bin/', '/_vti_bin', '/_vti_inf')``
    1038 
    1039 A tuple of strings that specify beginnings of URLs that should be ignored by
    1040 the 404 emailer. See ``SEND_BROKEN_LINK_EMAILS``, ``IGNORABLE_404_ENDS`` and
    1041 the :doc:`/howto/error-reporting`.
    1042 
    10431041.. setting:: INSTALLED_APPS
    10441042
    10451043INSTALLED_APPS
     
    14351433Whether to send an email to the ``MANAGERS`` each time somebody visits a
    14361434Django-powered page that is 404ed with a non-empty referer (i.e., a broken
    14371435link). This is only used if ``CommonMiddleware`` is installed (see
    1438 :doc:`/topics/http/middleware`. See also ``IGNORABLE_404_STARTS``,
    1439 ``IGNORABLE_404_ENDS`` and :doc:`/howto/error-reporting`.
     1436:doc:`/topics/http/middleware`). See also ``IGNORABLE_404_URLS`` and
     1437:doc:`/howto/error-reporting`.
    14401438
    14411439.. setting:: SERIALIZATION_MODULES
    14421440
     
    20452043   This setting has been replaced by :setting:`USER` in
    20462044   :setting:`DATABASES`.
    20472045
     2046.. setting:: IGNORABLE_404_ENDS
     2047
     2048IGNORABLE_404_ENDS
     2049------------------
     2050
     2051.. deprecated:: 1.4
     2052   This setting has been superseded by :setting:`IGNORABLE_404_URLS`.
     2053
     2054.. setting:: IGNORABLE_404_STARTS
     2055
     2056IGNORABLE_404_STARTS
     2057--------------------
     2058
     2059.. deprecated:: 1.4
     2060   This setting has been superseded by :setting:`IGNORABLE_404_URLS`.
     2061
    20482062.. setting:: TEST_DATABASE_CHARSET
    20492063
    20502064TEST_DATABASE_CHARSET
     
    20712085.. deprecated:: 1.2
    20722086   This setting has been replaced by :setting:`TEST_NAME` in
    20732087   :setting:`DATABASES`.
    2074 
  • tests/regressiontests/middleware/tests.py

     
    11# -*- coding: utf-8 -*-
    22
     3import re
     4
    35from django.conf import settings
     6from django.core import mail
    47from django.http import HttpRequest
    58from django.middleware.common import CommonMiddleware
    69from django.middleware.http import ConditionalGetMiddleware
     
    912
    1013class CommonMiddlewareTest(TestCase):
    1114    def setUp(self):
    12         self.slash = settings.APPEND_SLASH
    13         self.www = settings.PREPEND_WWW
     15        self.append_slash = settings.APPEND_SLASH
     16        self.prepend_www = settings.PREPEND_WWW
     17        self.ignorable_404_urls = settings.IGNORABLE_404_URLS
     18        self.send_broken_email_links = settings.SEND_BROKEN_LINK_EMAILS
    1419
    1520    def tearDown(self):
    16         settings.APPEND_SLASH = self.slash
    17         settings.PREPEND_WWW = self.www
     21        settings.APPEND_SLASH = self.append_slash
     22        settings.PREPEND_WWW = self.prepend_www
     23        settings.IGNORABLE_404_URLS = self.ignorable_404_urls
     24        settings.SEND_BROKEN_LINK_EMAILS = self.send_broken_email_links
    1825
    1926    def _get_request(self, path):
    2027        request = HttpRequest()
     
    249256      self.assertEqual(r['Location'],
    250257                        'http://www.testserver/middleware/customurlconf/slash/')
    251258
     259    # Tests for the 404 error reporting via email
     260
     261    def test_404_error_reporting(self):
     262        settings.IGNORABLE_404_URLS = (re.compile(r'foo'),)
     263        settings.SEND_BROKEN_LINK_EMAILS = True
     264        request = self._get_request('regular_url/that/does/not/exist')
     265        request.META['HTTP_REFERER'] = '/another/url/'
     266        response = self.client.get(request.path)
     267        CommonMiddleware().process_response(request, response)
     268        self.assertEqual(len(mail.outbox), 1)
     269        self.assertIn('Broken', mail.outbox[0].subject)
     270
     271    def test_404_error_reporting_no_referer(self):
     272        settings.IGNORABLE_404_URLS = (re.compile(r'foo'),)
     273        settings.SEND_BROKEN_LINK_EMAILS = True
     274        request = self._get_request('regular_url/that/does/not/exist')
     275        response = self.client.get(request.path)
     276        CommonMiddleware().process_response(request, response)
     277        self.assertEqual(len(mail.outbox), 0)
     278
     279    def test_404_error_reporting_ignored_url(self):
     280        settings.IGNORABLE_404_URLS = (re.compile(r'foo'),)
     281        settings.SEND_BROKEN_LINK_EMAILS = True
     282        request = self._get_request('foo_url/that/does/not/exist/either')
     283        request.META['HTTP_REFERER'] = '/another/url/'
     284        response = self.client.get(request.path)
     285        CommonMiddleware().process_response(request, response)
     286        self.assertEqual(len(mail.outbox), 0)
     287
     288
    252289class ConditionalGetMiddlewareTest(TestCase):
    253290    urls = 'regressiontests.middleware.cond_get_urls'
    254291    def setUp(self):
  • django/conf/global_settings.py

     
    246246# is an admin.
    247247ADMIN_FOR = ()
    248248
    249 # 404s that may be ignored.
    250 IGNORABLE_404_STARTS = ('/cgi-bin/', '/_vti_bin', '/_vti_inf')
    251 IGNORABLE_404_ENDS = ('mail.pl', 'mailform.pl', 'mail.cgi', 'mailform.cgi', 'favicon.ico', '.php')
     249# List of compiled regular expression objects representing URLs that need not
     250# be reported when SEND_BROKEN_LINK_EMAILS is True. Here is are a few examples:
     251#    import re
     252#    IGNORABLE_404_URLS = (
     253#        re.compile(r'^/apple-touch-icon.*\.png$'),
     254#        re.compile(r'^/favicon.ico$),
     255#        re.compile(r'^/robots.txt$),
     256#        re.compile(r'^/phpmyadmin/),
     257#        re.compile(r'\.(cgi|php|pl)$'),
     258#    )
     259IGNORABLE_404_URLS = ()
    252260
    253261# A secret key for this particular Django installation. Used in secret-key
    254262# hashing algorithms. Set this in your settings, or Django will complain
  • django/middleware/common.py

     
    127127    """
    128128    Returns True if a 404 at the given URL *shouldn't* notify the site managers.
    129129    """
    130     for start in settings.IGNORABLE_404_STARTS:
    131         if uri.startswith(start):
    132             return True
    133     for end in settings.IGNORABLE_404_ENDS:
    134         if uri.endswith(end):
    135             return True
    136     return False
     130    if getattr(settings, 'IGNORABLE_404_STARTS', ()):
     131        import warnings
     132        warnings.warn('The IGNORABLE_404_STARTS setting has been deprecated '
     133                      'in favour of IGNORABLE_404_URLS.',
     134                      PendingDeprecationWarning)
     135        for start in settings.IGNORABLE_404_STARTS:
     136            if uri.startswith(start):
     137                return True
     138    if getattr(settings, 'IGNORABLE_404_ENDS', ()):
     139        import warnings
     140        warnings.warn('The IGNORABLE_404_ENDS setting has been deprecated '
     141                      'in favour of IGNORABLE_404_URLS.',
     142                      PendingDeprecationWarning)
     143        for end in settings.IGNORABLE_404_ENDS:
     144            if uri.endswith(end):
     145                return True
     146    return any(pattern.search(uri) for pattern in settings.IGNORABLE_404_URLS)
    137147
    138148def _is_internal_request(domain, referer):
    139149    """
Back to Top