Skip to main content

Feature flags for Plain.

Project description

plain.flags

Local feature flags via database models.

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

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 %}

Or in Python:

import flags


print(flags.FooEnabled(user).value)

Installation

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

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

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.GET
        ):
            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.GET.get(self.url_param_name) == "1":
                return True

            if self.request.GET.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

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.7.3.tar.gz (14.5 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.7.3-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for plain_flags-0.7.3.tar.gz
Algorithm Hash digest
SHA256 1cf51b38ef58974a7d34cdc9dd0955a4264e9ef18aaa43e137c186ff6dfea0ed
MD5 d44e19a594478a1566b45363d7a4ccfb
BLAKE2b-256 9e88774ae48c9e8e9e34aa9e2db0200ce6214996e996924590875e02a379d28f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for plain_flags-0.7.3-py3-none-any.whl
Algorithm Hash digest
SHA256 07c5a94bdb61c3402800a715c52fe2d6a92769cfaf1dd33c3bf4c6a490eae1fa
MD5 792d47140150772dfd254c54b9daee96
BLAKE2b-256 e66235eb59ba8d27300fd00c51905f05dba910c5945ad38cdb6b444abc4424b1

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