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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file plain_flags-0.10.0.tar.gz.
File metadata
- Download URL: plain_flags-0.10.0.tar.gz
- Upload date:
- Size: 7.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.6.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d836c662ba9313fc03d6ad156bc682ac28f54da5965814530bd760d387790a8d
|
|
| MD5 |
cac77576c6b2de1a4dc3ffd1442363bf
|
|
| BLAKE2b-256 |
a3009474da726f8362a253bb96c8901acba1b3ac28f9c53594b980faaa5456ed
|
File details
Details for the file plain_flags-0.10.0-py3-none-any.whl.
File metadata
- Download URL: plain_flags-0.10.0-py3-none-any.whl
- Upload date:
- Size: 11.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.6.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
11828846bca03e76c72da6371c7f49cf9c8e7ec8f0df9f686fe763f76b53a264
|
|
| MD5 |
77c46db47302dd7238cbc3c057d7f71e
|
|
| BLAKE2b-256 |
2aba4eaf3c13287438b3804169ced35a3586f50780eaa2392251bb47cdd80e1e
|