# ruff: noqa: S311 import random from typing import Optional from django_components import Component, register, types DESCRIPTION = "Dynamically render different component versions. Use for A/B testing, phased rollouts, etc." @register("offer_card_old") class OfferCardOld(Component): class Kwargs: savings_percent: int def get_template_data(self, args, kwargs, slots, context): return { "savings_percent": kwargs.savings_percent, } template: types.django_html = """

Special Offer!

Get {{ savings_percent }}% off on your next purchase.

""" @register("offer_card_new") class OfferCardNew(OfferCardOld): template: types.django_html = """

FLASH SALE!

Exclusive Offer: {{ savings_percent }}% off everything!

""" @register("offer_card") class OfferCard(Component): class Kwargs: savings_percent: int use_new_version: Optional[bool] = None def on_render(self, context, template): # Pass all kwargs to the child component kwargs_for_child = self.kwargs._asdict() use_new = kwargs_for_child.pop("use_new_version") # If version not specified, choose randomly if use_new is None: use_new = random.choice([True, False]) if use_new: return OfferCardNew.render(context=context, kwargs=kwargs_for_child) else: return OfferCardOld.render(context=context, kwargs=kwargs_for_child)