# encoding: utf-8 # # Copyright (c) 2009 Thomas Kongevold Adamcik # # Snippet is released under the MIT License. So feel free to use it in other # projects as long as the notice remains intact :) # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # See http://www.djangosnippets.org/snippets/1312/ ''' HTML Validation Middleware ========================== Simple development middleware to ensure that responses validate as HTML. Dependencies: ------------- - tidy (http://utidylib.berlios.de/) Installation: ------------- Assuming this file has been place in your PYTHON_PATH (e.g. djangovalidation/middleware.py), simply add the following to your middleware settings: 'djangovalidation.middleware.HTMLValidationMiddleware', Remember that the order of your middleware settings does matter, this middleware should be placed before eg. GzipMiddleware, djangologging and any other middlewares that modify the response's content. Operation: ---------- Validation only kicks in under to following conditions: - DEBUG == True - HTML_VALIDATION_ENABLE == True (default) - REMOTE_ADDR in INTERNAL_IPS - 'html' in Content-Type - 'disable-validation' not in GET - request.is_ajax() == False - type(response) == HttpResponse - request.path doesn't match HTML_VALIDATION_URL_IGNORE To bypass the check any uri can be appended with ?disable-validation Settings: --------- - HTML_VALIDATION_ENABLE - Turns middleware on/off. Default: True - HTML_VALIDATION_ENCODING - Default: 'utf-8' - HTML_VALIDATION_DOCTYPE - Default: 'strict' - HTML_VALIDATION_IGNORE - Default: ['trimming empty