import warnings

from django.utils.deprecation import MiddlewareMixin
from wagtail.core.models import Site
from wagtail.utils.deprecation import RemovedInWagtail28Warning


class SiteMiddleware(MiddlewareMixin):
    def process_request(self, request):
        """
        Set request.site to contain the Site object responsible for handling this request,
        according to hostname matching rules
        """
        warnings.warn(
            'wagtail SiteMiddleware and the use of request.site is deprecated '
            'and will be removed in wagtail 2.8. Update your middleware settings.',
            RemovedInWagtail28Warning, stacklevel=2
        )

        try:
            request._wagtail_site = Site.find_for_request(request)
        except Site.DoesNotExist:
            request._wagtail_site = None

import warnings

from django.utils.deprecation import MiddlewareMixin
from wagtail.core.models import Site
from wagtail.utils.deprecation import RemovedInWagtail211Warning


class SiteMiddleware(MiddlewareMixin):
    def process_request(self, request):
        """
        Set request.site to contain the Site object responsible for handling this request,
        according to hostname matching rules
        """
        warnings.warn(
            'Wagtail SiteMiddleware and the use of request.site is deprecated '
            'and will be removed in Wagtail 2.11. Update your middleware settings.',
            RemovedInWagtail211Warning, stacklevel=2
        )

        try:
            request.site = Site.find_for_request(request)
        except Site.DoesNotExist:
            request.site = None

Revert SiteMiddleware to setting request.site
Revert SiteMiddleware to setting request.site

This way, SiteMiddleware continues to support existing user / third-party code that has not yet been migrated from request.site to Site.find_for_request

  import warnings
  
  from django.utils.deprecation import MiddlewareMixin
  from wagtail.core.models import Site
- from wagtail.utils.deprecation import RemovedInWagtail28Warning
+ from wagtail.utils.deprecation import RemovedInWagtail211Warning
  
  
  class SiteMiddleware(MiddlewareMixin):
      def process_request(self, request):
          """
          Set request.site to contain the Site object responsible for handling this request,
          according to hostname matching rules
          """
          warnings.warn(
-             'wagtail SiteMiddleware and the use of request.site is deprecated '
+             'Wagtail SiteMiddleware and the use of request.site is deprecated '
-             'and will be removed in wagtail 2.8. Update your middleware settings.',
+             'and will be removed in Wagtail 2.11. Update your middleware settings.',
-             RemovedInWagtail28Warning, stacklevel=2
+             RemovedInWagtail211Warning, stacklevel=2
          )
  
          try:
-             request._wagtail_site = Site.find_for_request(request)
+             request.site = Site.find_for_request(request)
          except Site.DoesNotExist:
-             request._wagtail_site = None
+             request.site = None
  
Revert SiteMiddleware to setting request.site
## Code Before:
import warnings

from django.utils.deprecation import MiddlewareMixin
from wagtail.core.models import Site
from wagtail.utils.deprecation import RemovedInWagtail28Warning


class SiteMiddleware(MiddlewareMixin):
    def process_request(self, request):
        """
        Set request.site to contain the Site object responsible for handling this request,
        according to hostname matching rules
        """
        warnings.warn(
            'wagtail SiteMiddleware and the use of request.site is deprecated '
            'and will be removed in wagtail 2.8. Update your middleware settings.',
            RemovedInWagtail28Warning, stacklevel=2
        )

        try:
            request._wagtail_site = Site.find_for_request(request)
        except Site.DoesNotExist:
            request._wagtail_site = None

## Instruction:
Revert SiteMiddleware to setting request.site
## Code After:
import warnings

from django.utils.deprecation import MiddlewareMixin
from wagtail.core.models import Site
from wagtail.utils.deprecation import RemovedInWagtail211Warning


class SiteMiddleware(MiddlewareMixin):
    def process_request(self, request):
        """
        Set request.site to contain the Site object responsible for handling this request,
        according to hostname matching rules
        """
        warnings.warn(
            'Wagtail SiteMiddleware and the use of request.site is deprecated '
            'and will be removed in Wagtail 2.11. Update your middleware settings.',
            RemovedInWagtail211Warning, stacklevel=2
        )

        try:
            request.site = Site.find_for_request(request)
        except Site.DoesNotExist:
            request.site = None
