Skip to main content

Local feature flags via database models.

Project description

plain.flags

Local feature flags via database models.

Overview

Custom flags are written as subclasses of Flag. You define the flag's "key" and initial value, and the results will be stored in the database for future reference.

# app/flags.py
from plain.flags import Flag


class FooEnabled(Flag):
    def __init__(self, user):
        self.user = user

    def get_key(self):
        return self.user

    def get_value(self):
        # Initially all users will have this feature disabled
        # and we'll enable them manually in the admin
        return False

Usage in templates

Use flags in HTML templates:

{% if flags.FooEnabled(request.user) %}
    <p>Foo is enabled for you!</p>
{% else %}
    <p>Foo is disabled for you.</p>
{% endif %}

Usage in Python

import flags


print(flags.FooEnabled(user).value)

Advanced usage

Ultimately you can do whatever you want inside of get_key and get_value.

class OrganizationFeature(Flag):
    url_param_name = ""

    def __init__(self, request=None, organization=None):
        # Both of these are optional, but will usually both be given
        self.request = request
        self.organization = organization

    def get_key(self):
        if (
            self.url_param_name
            and self.request
            and self.url_param_name in self.request.query_params
        ):
            return None

        if not self.organization:
            # Don't save the flag result for PRs without an organization
            return None

        return self.organization

    def get_value(self):
        if self.url_param_name and self.request:
            if self.request.query_params.get(self.url_param_name) == "1":
                return True

            if self.request.query_params.get(self.url_param_name) == "0":
                return False

        if not self.organization:
            return False

        # All organizations will start with False,
        # and I'll override in the DB for the ones that should be True
        return False


class AIEnabled(OrganizationFeature):
    pass

Installation

Install the plain.flags package from PyPI:

uv add plain.flags

Add to your INSTALLED_PACKAGES:

INSTALLED_PACKAGES = [
    ...
    "plain.flags",
]

Create a flags.py at the top of your app (or point settings.FLAGS_MODULE to a different location).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

plain_flags-0.24.0.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

plain_flags-0.24.0-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file plain_flags-0.24.0.tar.gz.

File metadata

  • Download URL: plain_flags-0.24.0.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.22

File hashes

Hashes for plain_flags-0.24.0.tar.gz
Algorithm Hash digest
SHA256 8e0b7f89e3b80e6b42c688c0f5db7ba9cabf6c3b466117e69c2d7a81d0dabd72
MD5 8e20e34db84948bebaee8289461d8399
BLAKE2b-256 d2883aed3c32bb9322a3251699748289364811c959a59def7f4dded648f51a20

See more details on using hashes here.

File details

Details for the file plain_flags-0.24.0-py3-none-any.whl.

File metadata

File hashes

Hashes for plain_flags-0.24.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c06d1d491f50308d1ef4db7651241951180ca1d2fb48ae088d5fab4d4c15c296
MD5 7edfd419b936f6ef8ae4505d9d575e56
BLAKE2b-256 5d7e73f2365797b3e1be9480fd30a42cda9b138c74e294db3b7068b48e57f84a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page