MiddlewareNotUsed is a class within the django.core.exceptions module of the Django project.
django-pipeline (project documentation and PyPI package information) is a code library for handling and compressing static content assets when handling requests in Django web applications.
The django-pipeline project is open sourced under the MIT License and it is maintained by the developer community group Jazzband.
django-pipeline / pipeline / middleware.py
# middleware.py
from django.core.exceptions import MiddlewareNotUsed
from django.utils.encoding import DjangoUnicodeDecodeError
from django.utils.html import strip_spaces_between_tags as minify_html
from pipeline.conf import settings
from django.utils.deprecation import MiddlewareMixin
class MinifyHTMLMiddleware(MiddlewareMixin):
def __init__(self, *args, **kwargs):
super(MinifyHTMLMiddleware, self).__init__(*args, **kwargs)
if not settings.PIPELINE_ENABLED:
raise MiddlewareNotUsed
def process_response(self, request, response):
if response.has_header('Content-Type') and 'text/html' in response['Content-Type']:
try:
response.content = minify_html(response.content.decode('utf-8').strip())
response['Content-Length'] = str(len(response.content))
except DjangoUnicodeDecodeError:
pass
return response
## ... source file continues with no further MiddlewareNotUsed examples...
django-user-visit (PyPI package information) is a Django app and middleware for tracking daily user visits to your web application. The goal is to record per user per day instead of for every request a user sends to the application. The project is provided as open source under the MIT license.
django-user-visit / user_visit / middleware.py
# middleware.py
import logging
import typing
import django.db
from django.core.exceptions import MiddlewareNotUsed
from django.http import HttpRequest, HttpResponse
from django.utils import timezone
from user_visit.models import UserVisit
from .settings import RECORDING_DISABLED
logger = logging.getLogger(__name__)
SESSION_KEY = "user_visit.hash"
class UserVisitMiddleware:
def __init__(self, get_response: typing.Callable) -> None:
if RECORDING_DISABLED:
raise MiddlewareNotUsed("UserVisit recording has been disabled")
self.get_response = get_response
def __call__(self, request: HttpRequest) -> typing.Optional[HttpResponse]:
if request.user.is_anonymous:
return self.get_response(request)
uv = UserVisit.objects.build(request, timezone.now())
if request.session.get(SESSION_KEY, "") == uv.hash:
return self.get_response(request)
try:
uv.save()
except django.db.IntegrityError:
logger.warning("Unable to record user visit - duplicate request hash")
else:
request.session[SESSION_KEY] = uv.hash
return self.get_response(request)
## ... source file continues with no further MiddlewareNotUsed examples...