Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

django-waffle-window

Django app for managing time-bound django-waffle user-flag membership.

Background

If you have used django-waffle for managing feature flags, you will probably have come across the challenge of managing which users have access to which flags at what point in time. The underlying model allows you to assign individual users or groups to a flag, leaving you to manage how and when to add users to a flag, or remove users from a flag. Take the following use case:

Give access to user A to feature B for 30 days, starting Monday.

This use case can be challenging to manage at scale. Someone has to remember to turn the flag, and then again to turn it off (which means, in practice, removing the user from the flag, or the flag-enabled group).

This app attempts to tackle this problem by adding a FlagMember model that enables you to set a date window within which a user will be added to a flag, and outside of which they will be removed. The add / remove process is managed via management command.

$ python manage.py update_flag_membership

Implementation

The flag membership is managed using Django Groups. For each flag, a related group called WAFFLE_<flag_name> is created, and assigned to the flag. The management command then runs through each flag, checks the FlagMember table for users who are assigned to the flag on that date, and assigns them to the relevant group. The user membership window is defined through FlagMember.start_date and FlagMember.end_date.

class FlagMemberQuerySet(QuerySet):

    def active(flag_name: str) -> FlagMemberQuerySet:
        """Return all users assigned to the flag today."""
        on_date = datetime.date.today()
        return self.filter(
            name=flag_name,
            start_date__gte=on_date,
            end_date__lte=on_date
        )

Each FlagMember object can sync its own membership:

>>> member = FlagMember(user, flag_name, start_date, end_date)
>>> member.save()  # save object, but do not add to group
>>> member.sync()  # add to / remove from group as appropriate

The scheduler clears the entire group and re-adds those members who are active.

def _get_or_create_flag_group(flag_name: str) -> Group:
    flag = Flag.objects.get(name=flag_name)
    group = Group.objects.get_or_create(name=f"WAFFLE_{flag_name}")
    flag.group_set.add(group)
    return group

def sync_flag_membership(flag_name: str) -> None:
    """Update the flag group with all members of the queryset."""
    active_members = FlagMember.objects.active(flag_name)
    group = _get_or_create_flag_group(flag_name)
    group.user_set.clear()
    group.add(*active_members)  # this won't scale for large querysets

The management command updates all Flags:

class Command(BaseCommand):

    def handle(self, *args, **options):
        for flag in Flag.objects.all():
            sync_flag_membership(flag.name)

Metadata

Release files for waffle-window 0.1.0.dev0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for waffle-window 0.1.0.dev0
File Size Uploaded
waffle-window-0.1.0.dev0.tar.gz 5.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for waffle-window 0.1.0.dev0
File Interpreter ABI Platform
waffle_window-0.1.0.dev0-py3-none-any.whl Python 3 none any Details

Total release size: 11.7 kB

Release files / waffle-window-0.1.0.dev0.tar.gz

Download URL waffle-window-0.1.0.dev0.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
4b907015517c0548f2299fa6131d7498c758f7b9c6ac2e4f43f4a28285826055
BLAKE2b-256 checksum
How to use checksums
5ab6c24632d65b590d6adec160b9c882028ef2fd82f9601fae7676a2e8d23641
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.2 CPython/3.8.1 Darwin/19.2.0

Release files / waffle_window-0.1.0.dev0-py3-none-any.whl

Download URL waffle_window-0.1.0.dev0-py3-none-any.whl
Size 6.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bd44e59aabd2b6a4445dcb7c1689c380cd0cfd2430cbc9890806cca3ab10c5c7
BLAKE2b-256 checksum
How to use checksums
f756da97bba04651dac8b26b8a5068de2b1899b37af03e41187efa55e008d19e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.2 CPython/3.8.1 Darwin/19.2.0

Release history Release notifications | RSS feed

This release

0.1.0.dev0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page