Skip to content

feat(middleware): Enable v2 design for new page via template switching #5975

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions cl/lib/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,27 @@ def __call__(self, request):
def process_template_response(self, request, response):
if settings.TESTING:
return response

use_new_design = flag_is_active(request, "use_new_design")

if (
use_new_design
and isinstance(response, TemplateResponse)
and not response.is_rendered
):
old_template = response.template_name
if isinstance(old_template, str):
new_template = f"v2_{old_template}"
response.template_name = [new_template, old_template]
# The template_name can be a string or a list/tuple.
current_templates = response.template_name
print(current_templates)

# Normalize to a list to simplify logic
if isinstance(current_templates, str):
current_templates = [current_templates]

if isinstance(current_templates, (list, tuple)):
# Create a list of new v2 templates to try first
new_v2_templates = [f"v2_{name}" for name in current_templates]
# The final list allows Django to try the v2 template, and if it
# doesn't exist, fall back to the original.
response.template_name = new_v2_templates + current_templates

return response
Loading