Changeset View
Changeset View
Standalone View
Standalone View
swh/web/common/middlewares.py
# Copyright (C) 2018-2019 The Software Heritage developers | # Copyright (C) 2018-2019 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU Affero General Public License version 3, or any later version | # License: GNU Affero General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from bs4 import BeautifulSoup | |||||
from htmlmin import minify | from htmlmin import minify | ||||
import sentry_sdk | import sentry_sdk | ||||
from swh.web.common.utils import prettify_html | |||||
class HtmlPrettifyMiddleware(object): | class HtmlPrettifyMiddleware(object): | ||||
""" | """ | ||||
Django middleware for prettifying generated HTML in | Django middleware for prettifying generated HTML in | ||||
development mode. | development mode. | ||||
""" | """ | ||||
def __init__(self, get_response): | def __init__(self, get_response): | ||||
self.get_response = get_response | self.get_response = get_response | ||||
def __call__(self, request): | def __call__(self, request): | ||||
response = self.get_response(request) | response = self.get_response(request) | ||||
if 'text/html' in response.get('Content-Type', ''): | if 'text/html' in response.get('Content-Type', ''): | ||||
if hasattr(response, 'content'): | if hasattr(response, 'content'): | ||||
content = response.content | content = response.content | ||||
response.content = BeautifulSoup(content, 'lxml').prettify() | response.content = prettify_html(content) | ||||
elif hasattr(response, 'streaming_content'): | elif hasattr(response, 'streaming_content'): | ||||
content = b''.join(response.streaming_content) | content = b''.join(response.streaming_content) | ||||
response.streaming_content = \ | response.streaming_content = prettify_html(content) | ||||
BeautifulSoup(content, 'lxml').prettify() | |||||
return response | return response | ||||
class HtmlMinifyMiddleware(object): | class HtmlMinifyMiddleware(object): | ||||
""" | """ | ||||
Django middleware for minifying generated HTML in | Django middleware for minifying generated HTML in | ||||
production mode. | production mode. | ||||
Show All 34 Lines |