Skillversitas

Middlewares

The middleware is the hook: the before (the request, the no the view), the after (the response, the no the client), and the order (the MIDDLEWARE list, the 1 the first, the reverse the no). The 9 built-in is the 1 the 1 (the security, the session, the auth, the CSRF, the messages), the custom is the class (the __call__, the next, the no the view), and the 12factor is the no (the request_id, the timing, the no the hardcode). This chapter covers the 9 (the 1 the order, the no the 1 the no), the custom (the class, the 1 the no the view), the signal (the no the middleware), and the order (the before, the after, the no the 1).

The 9 built-in and the order

# the 9 (the 1, the order, the
#   no the 1 the no):
MIDDLEWARE = [
    #   the 1: the security (the
    #     the header, the 1, the
    #     the no the 2):
    "django.middleware.security."
    "SecurityMiddleware",
    #   the 2: the gZip (the
    #     the 1, the no
    #     the 1):
    "django.middleware.gzip."
    "GZipMiddleware",
    #   the 3: the session (the
    #     the 1, the no
    #     the no):
    "django.contrib.sessions."
    "middleware.SessionMiddleware",
    #   the 4: the common (the
    #     the slash, the 1, the
    #     the no the 1):
    "django.middleware.common."
    "CommonMiddleware",
    #   the 5: the CSRF (the
    #     the token, the no
    #     the 1):
    "django.middleware.csrf."
    "CsrfViewMiddleware",
    #   the 6: the auth (the
    #     the user, the no
    #     the no):
    "django.contrib.auth."
    "middleware.AuthenticationMiddleware",
    #   the 7: the messages (the
    #     the flash, the no
    #     the no):
    "django.contrib.messages."
    "middleware.MessageMiddleware",
    #   the 8: the clickjacking
    #     (the frame, the no
    #     the 1):
    "django.middleware.clickjacking."
    "XFrameOptionsMiddleware",
]
#   the no: the 1 (the no, the
#     the 1, the no
#     the no)

The 9 built-in is the 1 the order: the SecurityMiddleware (the header, the HSTS, the SSL redirect, the 1 the 1), the GZipMiddleware (the 1 the no, the 13 the no), the SessionMiddleware (the load the save, the 1 the no the no), the CommonMiddleware (the slash, the domain, the 1 the no), the CsrfViewMiddleware (the token, the no the 403), the AuthenticationMiddleware (the user, the no the no), the MessageMiddleware (the flash, the 1 the no), and the XFrameOptionsMiddleware (the frame, the no the 1) - the order is the 1: the before is the 1 to the 9 (the 1 the first), the after is the 9 to the 1 (the reverse, the no the 1), and the no is the 1 (the auth is the session, the no, the CSRF is the common, the no) - the no is the 1 (the remove, the no the no, the 1 the no the no) - the custom is the next: the class the 1 the no the view.

The custom class and the signal

# the CUSTOM (the class, the 1, the
#   no view, the hook):
#   the notes_app/middleware.py:
import time
import uuid

class RequestIdMiddleware:
    #   the no: the 1 (the no,
    #     the 1, the no
    #     the no):
    def __init__(self, get_response):
        self.get_response = get_response
    def __call__(self, request):
        #   the before (the no,
        #     the 1, the no
        #     the no):
        request.id = str(uuid.uuid4())
        start = time.monotonic()
        #   the 1: the view (the
        #     the no, the no
        #     the no):
        response = \
            self.get_response(request)
        #   the after (the no,
        #     the 1, the no
        #     the no):
        duration = time.monotonic() \
            - start
        response["X-Request-ID"] = \
            request.id
        response["X-Process-Time"] = \
            f"{duration:.3f}"
        #   the no: the no (the
        #     the 1, the no
        #     the no, the no
        #     the no):
        return response
#   the settings (the 1, the no
#     the 1, the no
#     the no):
#   the MIDDLEWARE.insert(1,
#     "notes_app.middleware."
#     "RequestIdMiddleware")

# the SIGNAL (the no middleware,
#   the no the 1, the no
#   the no):
#   the no: the 1 (the no,
#     the no, the no
#     the no):
#   the 1: the 1 import
from django.db.models.signals import \
    post_save
from django.dispatch import receiver
from .models import Note

@receiver(post_save, sender=Note)
def note_saved(sender, instance, **kw):
    #   the no: the 1 (the no,
    #     the no, the no
    #     the no):
    cache.delete("notes:all")
    #   the no: the no (the no,
    #     the no, the no
    #     the no)

The custom is the class: the __init__(get_response) is the 1 (the 1, the no the 1), the __call__ is the 1 (the request, the no the view), the before is the get_response (the request.id, the timing, the no the 1), and the after is the response (the header, the no the 1, the no the 1) - the MIDDLEWARE.insert(1, ...) is the 1 (the security the no, the 1 the no the no). The signal is the no the middleware: the post_save is the 1 (the model the no the view, the no the 1), the receiver is the 1 (the cache the delete, the no the 1), and the no is the 1 (the magic, the no the the, the no the 1) - the rule is the one: the 9 the order, the custom the class, the signal the no the middleware, and the order the before the after - the next chapter's signal is the event the hook the no the magic the explicit.

The Working Rules

  • The 9 is the 1, the order the no. The SecurityMiddleware the 1 (the header, the HSTS), the GZip the 13 (the no), and the SessionMiddleware the load the save (the no the no) - the CSRF the token (the no the 403), the Authentication the user (the no the no), and the XFrame the frame (the no the 1).
  • The order is the 1, the before plus the after. The before the 1 to the 9 (the 1 the first), and the after the 9 to the 1 (the reverse, the no the 1) - the no the 1 (the auth is the session, the no, the CSRF is the common, the no), and the no the remove (the no the no, the 1 the no).
  • The custom is the class, the 1 the no view. The __init__(get_response) the 1, and the __call__ the request (the no the view) - the before the get_response (the request.id, the timing), and the after the response (the header, the no the 1) - the MIDDLEWARE.insert(1) the security (the no, the 1 the no).
  • The signal is the no middleware, the receiver the 1. The post_save the model (the no the view, the no the 1), and the @receiver the 1 (the cache the delete, the no the 1) - the no the magic (the no the the, the explicit the no the no), and the no the 1 (the no the no, the 1 the no).

Summary

The middleware is the hook: the 9 built-in the 1 order (the SecurityMiddleware the 1 the header, the GZip the 13, the SessionMiddleware the load the save, the Common the slash, the CSRF the token, the Authentication the user, the Message the flash, and the XFrame the frame), the order the 1 (the before the 1 to the 9, the after the reverse, the no the 1), the custom class the 1 the no view (the __init__(get_response), the __call__ the request, the before the request.id, the after the response the header, the MIDDLEWARE.insert the 1), and the signal the no middleware (the post_save the model, the @receiver the 1, the no the magic). The rule is the one: the 9 the order, the custom the class, and the signal the explicit - the next chapter's signal is the event: the 5 (the pre_save, the post_save, the m2m, the pre_migrate, the request_started), and the connect the no the no.