<gh_stars>1-10
from django.middleware.csrf import CsrfViewMiddleware, rotate_token


class CsrfExpirationMiddleware(CsrfViewMiddleware):
    SAFE_METHODS = ('GET', 'HEAD', 'OPTIONS', 'TRACE')

    def process_response(self, request, response):
        if request.method not in self.SAFE_METHODS:
            rotate_token(request)
        return super().process_response(request, response)
