Git Product home page Git Product logo

Comments (2)

hugorodgerbrown avatar hugorodgerbrown commented on June 26, 2024

Closed as this is possible with a custom Flag model.

from django-waffle.

hugorodgerbrown avatar hugorodgerbrown commented on June 26, 2024

Adding this as an example, for future reference.

import datetime

from django.conf import settings
from django.db import models
from django.utils.timezone import now as tz_now

from waffle.models import AbstractUserFlag


class FeatureFlag(AbstractUserFlag):
    """
    Drop-in replacement for Waffle's Flag model that supports custom
    groups.

    This is a subclass of Waffle's Flag model which adds the ability to
    to target a "custom group" of users. This group is just a string,
    which is then used in the `is_active_for_user` method override to
    determine whether the flag should be active.

    To add a new custom group add a const to the `CustomGroup` class,
    and then add a matching model method with the same name as the
    const. See the `anonymous_only` and `new_registrations` methods for
    examples.

    """

    class CustomGroup(models.TextChoices):
        # when adding a new group, remember to add the appropriate
        # method with the same name to the class.
        ANONYMOUS_ONLY = "anonymous_only", "Anonymous users only"
        NEW_REGISTRATIONS = "new_registrations", "Users registered in the last 30 days"

    custom_group = models.CharField(
        max_length=255,
        blank=True,
        null=True,
        choices=CustomGroup.choices,
        help_text="Custom group to enable this flag for",
    )

    class Meta:
        verbose_name = "Feature Flag"
        verbose_name_plural = "Feature Flags (ex. Waffle)"

    def is_active_for_user(self, user):
        if self.custom_group:
            # will raise AttributeError if the custom group doesn't exist
            return getattr(self, self.custom_group)(user)
        # use the normal Waffle operation if no custom group is set.
        return super().is_active_for_user(user)

    # region: group methods
    def anonymous_only(self, user: settings.AUTH_USER_MODEL) -> bool:
        return user.is_anonymous

    def new_registrations(self, user: settings.AUTH_USER_MODEL) -> bool:
        return user.date_joined > tz_now() - datetime.timedelta(days=30)
    # endregion: group methods

from django-waffle.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.