# Path: feincms3/root/middleware.py
# def add_redirect_handler(handler):
#     """
#     Wrap the page handler in a redirect mixin handler
#     """
# 
#     @wraps(handler)
#     def inner(request, page):
#         if redirect_to := page.get_redirect_url():
#             return HttpResponseRedirect(redirect_to)
#         return handler(request, page)
# 
#     return inner
# 
# def create_page_if_404_middleware(*, queryset, handler, language_code_redirect=False):
#     """
#     Create a middleware for handling pages
# 
#     This utility is there for your convenience, you do not have to use it. The
#     returned middleware already handles returning non-404 responses as-is,
#     fetching a page instance from the database and calling a user-defined
#     handler on success. It optionally also supports redirecting requests to the
#     root of the app to a language-specific landing page.
# 
#     Required arguments:
# 
#     - ``queryset``: A page queryset or a callable accepting the request and
#       returning a page queryset.
#     - ``handler``: A callable accepting the request and a page and returning a
#       response.
# 
#     Optional arguments:
# 
#     - ``language_code_redirect`` (``False``): Redirect visitor to the language
#       code prefix (e.g. ``/en/``, ``/de-ch/``) if request path equals the
#       script prefix (generally ``/``) and no active page for ``/`` exists.
#     """
# 
#     def outer(get_response):
#         def inner(request):
#             response = get_response(request)
#             if response.status_code != 404 or (
#                 request.resolver_match
#                 and not isinstance(response, _UseRootMiddlewareResponse)
#             ):
#                 # Response is not a 404 OR the 404 comes from a resolved view
#                 # which also didn't return a _UseRootMiddlewareResponse.
#                 return response
#             qs = queryset(request) if callable(queryset) else queryset._clone()
#             if page := qs.filter(path=request.path_info).first():
#                 return handler(request, page)
#             if language_code_redirect and request.path_info == "/":
#                 target = f"/{request.LANGUAGE_CODE}/"
#                 if qs.filter(path=target).exists():
#                     return HttpResponseRedirect(target)
#             return response
# 
#         return inner
# 
#     return outer


from django.shortcuts import render
from testapp.models import Page
from testapp.renderer import page_context
from feincms3.root.middleware import add_redirect_handler, create_page_if_404_middleware



@add_redirect_handler
def handler(request, page):
    return render(request, page.type.template_name, page_context(request, page=page))


page_if_404_middleware = create_page_if_404_middleware(
    queryset=Page.objects.active(),
    handler=handler,
    language_code_redirect=True,

# Path: feincms3/root/middleware.py
# def add_redirect_handler(handler):
#     """
#     Wrap the page handler in a redirect mixin handler
#     """
# 
#     @wraps(handler)
#     def inner(request, page):
#         if redirect_to := page.get_redirect_url():
#             return HttpResponseRedirect(redirect_to)
#         return handler(request, page)
# 
#     return inner
# 
# def create_page_if_404_middleware(*, queryset, handler, language_code_redirect=False):
#     """
#     Create a middleware for handling pages
# 
#     This utility is there for your convenience, you do not have to use it. The
#     returned middleware already handles returning non-404 responses as-is,
#     fetching a page instance from the database and calling a user-defined
#     handler on success. It optionally also supports redirecting requests to the
#     root of the app to a language-specific landing page.
# 
#     Required arguments:
# 
#     - ``queryset``: A page queryset or a callable accepting the request and
#       returning a page queryset.
#     - ``handler``: A callable accepting the request and a page and returning a
#       response.
# 
#     Optional arguments:
# 
#     - ``language_code_redirect`` (``False``): Redirect visitor to the language
#       code prefix (e.g. ``/en/``, ``/de-ch/``) if request path equals the
#       script prefix (generally ``/``) and no active page for ``/`` exists.
#     """
# 
#     def outer(get_response):
#         def inner(request):
#             response = get_response(request)
#             if response.status_code != 404 or (
#                 request.resolver_match
#                 and not isinstance(response, _UseRootMiddlewareResponse)
#             ):
#                 # Response is not a 404 OR the 404 comes from a resolved view
#                 # which also didn't return a _UseRootMiddlewareResponse.
#                 return response
#             qs = queryset(request) if callable(queryset) else queryset._clone()
#             if page := qs.filter(path=request.path_info).first():
#                 return handler(request, page)
#             if language_code_redirect and request.path_info == "/":
#                 target = f"/{request.LANGUAGE_CODE}/"
#                 if qs.filter(path=target).exists():
#                     return HttpResponseRedirect(target)
#             return response
# 
#         return inner
# 
#     return outer


# Path: tests/testapp/middleware.py
from django.shortcuts import render
from testapp.models import Page
from testapp.renderer import page_context
from feincms3.root.middleware import add_redirect_handler, create_page_if_404_middleware




@add_redirect_handler
def handler(request, page):
    return render(request, page.type.template_name, page_context(request, page=page))


page_if_404_middleware = create_page_if_404_middleware(
    queryset=Page.objects.active(),
    handler=handler,
    language_code_redirect=True,

)