benchdog-django 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. benchdog_django-0.1.0/.gitignore +32 -0
  2. benchdog_django-0.1.0/.python-version +1 -0
  3. benchdog_django-0.1.0/LICENSE +21 -0
  4. benchdog_django-0.1.0/PKG-INFO +136 -0
  5. benchdog_django-0.1.0/README.md +119 -0
  6. benchdog_django-0.1.0/pyproject.toml +51 -0
  7. benchdog_django-0.1.0/src/benchdog_django/__init__.py +1 -0
  8. benchdog_django-0.1.0/src/benchdog_django/apps.py +8 -0
  9. benchdog_django-0.1.0/src/benchdog_django/conf.py +52 -0
  10. benchdog_django-0.1.0/src/benchdog_django/dashboard/assets/embroider-virtual-app-3rXH0eIh.css +1860 -0
  11. benchdog_django-0.1.0/src/benchdog_django/dashboard/assets/embroider-virtual-vendor-3fUXwkzh.js +9 -0
  12. benchdog_django-0.1.0/src/benchdog_django/dashboard/assets/embroider-virtual-vendor-B_SY1GJM.css +0 -0
  13. benchdog_django-0.1.0/src/benchdog_django/dashboard/assets/main-DA4FE5ZT.css +1 -0
  14. benchdog_django-0.1.0/src/benchdog_django/dashboard/assets/main-Dg-YB7-H.js +1 -0
  15. benchdog_django-0.1.0/src/benchdog_django/dashboard/favicon.svg +57 -0
  16. benchdog_django-0.1.0/src/benchdog_django/dashboard/index.html +25 -0
  17. benchdog_django-0.1.0/src/benchdog_django/management/__init__.py +0 -0
  18. benchdog_django-0.1.0/src/benchdog_django/management/commands/__init__.py +0 -0
  19. benchdog_django-0.1.0/src/benchdog_django/management/commands/benchdog_schedules.py +19 -0
  20. benchdog_django-0.1.0/src/benchdog_django/middleware.py +43 -0
  21. benchdog_django-0.1.0/src/benchdog_django/migrations/0001_initial.py +100 -0
  22. benchdog_django-0.1.0/src/benchdog_django/migrations/__init__.py +0 -0
  23. benchdog_django-0.1.0/src/benchdog_django/models.py +55 -0
  24. benchdog_django-0.1.0/src/benchdog_django/py.typed +0 -0
  25. benchdog_django-0.1.0/src/benchdog_django/store.py +124 -0
  26. benchdog_django-0.1.0/src/benchdog_django/templatetags/__init__.py +0 -0
  27. benchdog_django-0.1.0/src/benchdog_django/templatetags/benchdog.py +16 -0
  28. benchdog_django-0.1.0/src/benchdog_django/urls.py +17 -0
  29. benchdog_django-0.1.0/src/benchdog_django/views.py +79 -0
  30. benchdog_django-0.1.0/tests/.gitignore +1 -0
  31. benchdog_django-0.1.0/tests/__init__.py +0 -0
  32. benchdog_django-0.1.0/tests/dashboard/index.html +2 -0
  33. benchdog_django-0.1.0/tests/settings.py +40 -0
  34. benchdog_django-0.1.0/tests/test_app.py +122 -0
  35. benchdog_django-0.1.0/tests/test_store.py +35 -0
  36. benchdog_django-0.1.0/tests/urls.py +3 -0
  37. benchdog_django-0.1.0/uv.lock +715 -0
@@ -0,0 +1,32 @@
1
+ node_modules/
2
+ dist/
3
+ declarations/
4
+ test-apps/*/tmp/
5
+ _site/
6
+ # Written by `pnpm guide:build` (docs-app), as in the siblings.
7
+ docs/site/guide/
8
+ docs/site/404.html
9
+ *.tsbuildinfo
10
+ .DS_Store
11
+ .idea/
12
+ coverage/
13
+ tmp/
14
+
15
+ # Ruby
16
+ ruby/*/.bundle/
17
+ ruby/*/vendor/
18
+ ruby/*/pkg/
19
+
20
+ # Python
21
+ __pycache__/
22
+ *.py[cod]
23
+ .venv/
24
+ .mypy_cache/
25
+ .pytest_cache/
26
+ .ruff_cache/
27
+ python/*/dist/
28
+ packages/prisma/tests/.generated/
29
+
30
+ # Copies of the dashboard build, made by scripts/bundle-dashboard.sh for the gem and the wheel.
31
+ ruby/benchdog-rails/dashboard/
32
+ python/benchdog-django/src/benchdog_django/dashboard/
@@ -0,0 +1 @@
1
+ 3.11
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Joe Gaudet
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,136 @@
1
+ Metadata-Version: 2.5
2
+ Name: benchdog-django
3
+ Version: 0.1.0
4
+ Summary: Feature flags for Django: the Benchdog app, admin API, dashboard and template tags.
5
+ Project-URL: Homepage, https://joegaudet.com/benchdog/
6
+ Project-URL: Source, https://github.com/joegaudet/benchdog/tree/main/python/benchdog-django
7
+ Project-URL: Issues, https://github.com/joegaudet/benchdog/issues
8
+ Author-email: Joe Gaudet <joe@joegaudet.com>
9
+ License-Expression: MIT
10
+ License-File: LICENSE
11
+ Classifier: Framework :: Django
12
+ Classifier: Typing :: Typed
13
+ Requires-Python: >=3.11
14
+ Requires-Dist: benchdog==0.1.0
15
+ Requires-Dist: django>=4.2
16
+ Description-Content-Type: text/markdown
17
+
18
+ # benchdog-django
19
+
20
+ [Benchdog](https://joegaudet.com/benchdog/) feature flags for Django 4.2 and later. The app has:
21
+
22
+ - A flag store on the Django ORM, with migrations.
23
+ - The admin API and the dashboard, at one mount point.
24
+ - A middleware that puts a flag client and the actor on each request.
25
+ - The `flag_enabled` template tag.
26
+ - The `benchdog_schedules` command, for cron.
27
+
28
+ ## Install
29
+
30
+ ```bash
31
+ pip install benchdog-django
32
+ python manage.py migrate
33
+ ```
34
+
35
+ ## Settings
36
+
37
+ ```python
38
+ INSTALLED_APPS = [..., "benchdog_django"]
39
+
40
+ MIDDLEWARE = [
41
+ ...,
42
+ "django.contrib.auth.middleware.AuthenticationMiddleware",
43
+ "benchdog_django.middleware.BenchdogMiddleware", # After the authentication middleware.
44
+ ]
45
+
46
+ BENCHDOG = {
47
+ # The order is the promotion order. A protected environment needs an approval for a change.
48
+ "environments": [
49
+ {"name": "development", "promotes_to": "production"},
50
+ {"name": "production", "protected": True},
51
+ ],
52
+ "environment": "production", # The environment of the flag client. Default: the first one.
53
+ "authenticate": "myapp.flags.authenticate", # (request) -> {"id": ..., "name": ...} or None
54
+ "role_for": "myapp.flags.role_for", # (user, environment) -> "viewer" ... "admin" or None
55
+ "actor_for": "myapp.flags.actor_for", # (request, user) -> {"id": "user:42"} or None
56
+ "groups": "myapp.flags.GROUPS", # {"staff": lambda actor: ...}
57
+ "cron_secret": os.environ["BENCHDOG_CRON_SECRET"], # Turns on POST api/cron/schedules.
58
+ "references": "myapp.flags.references", # The output of `benchdog refs --json`.
59
+ "audience": "myapp.flags.AUDIENCE", # {"groups": [...], "actor_types": [...], "lookup_actors": fn}
60
+ }
61
+ ```
62
+
63
+ A text value of an object key is a dotted import path. You can also give the object.
64
+ `environments` and `authenticate` are necessary. The `store` key replaces the ORM store, for
65
+ example with a `benchdog.MemoryStore()` in tests.
66
+
67
+ ```python
68
+ # myapp/flags.py
69
+ def authenticate(request):
70
+ user = request.user
71
+ return {"id": str(user.pk), "name": user.get_full_name()} if user.is_authenticated else None
72
+
73
+
74
+ def role_for(user, environment):
75
+ return "admin"
76
+
77
+
78
+ def actor_for(request, user):
79
+ return {"id": f"user:{user['id']}"} if user else None
80
+ ```
81
+
82
+ The package includes the dashboard. `dashboard_dir` serves another build.
83
+
84
+ ## URLs
85
+
86
+ ```python
87
+ urlpatterns = [path("benchdog/", include("benchdog_django.urls"))]
88
+ ```
89
+
90
+ The dashboard is at `benchdog/`, and the API is at `benchdog/api/`. The API does not use the
91
+ Django CSRF check. To prevent CSRF, a write that has no `Authorization` or `X-Benchdog-Source`
92
+ header is not signed in. The dashboard sends `X-Benchdog-Source`.
93
+
94
+ ## Flags in a view and a template
95
+
96
+ ```python
97
+ from benchdog_django.middleware import BenchdogRequest
98
+
99
+
100
+ def checkout(request: BenchdogRequest):
101
+ if request.benchdog.enabled("new-checkout", request.benchdog_actor):
102
+ ...
103
+ theme = request.benchdog.value("theme", request.benchdog_actor, "light")
104
+ ```
105
+
106
+ ```django
107
+ {% load benchdog %}
108
+ {% flag_enabled "new-checkout" as on %}
109
+ {% if on %}...{% endif %}
110
+ ```
111
+
112
+ The template tag needs the `request` context processor. Evaluation does no I/O. Each request
113
+ reads the store version, and the client loads the flag set again when the version changes.
114
+
115
+ ## Schedules
116
+
117
+ Run this command from cron, for example each minute:
118
+
119
+ ```bash
120
+ python manage.py benchdog_schedules
121
+ ```
122
+
123
+ The evaluator applies due schedule steps at read time. The command writes them into the flags,
124
+ with an audit entry.
125
+
126
+ ## Checks
127
+
128
+ ```bash
129
+ uv run ruff check
130
+ uv run ruff format --check
131
+ uv run mypy
132
+ uv run pytest
133
+ ```
134
+
135
+ `test-apps/django` is a small Django project with this app. The HTTP contract suite
136
+ (`packages/api-tests`) runs against it from `pnpm python:check`.
@@ -0,0 +1,119 @@
1
+ # benchdog-django
2
+
3
+ [Benchdog](https://joegaudet.com/benchdog/) feature flags for Django 4.2 and later. The app has:
4
+
5
+ - A flag store on the Django ORM, with migrations.
6
+ - The admin API and the dashboard, at one mount point.
7
+ - A middleware that puts a flag client and the actor on each request.
8
+ - The `flag_enabled` template tag.
9
+ - The `benchdog_schedules` command, for cron.
10
+
11
+ ## Install
12
+
13
+ ```bash
14
+ pip install benchdog-django
15
+ python manage.py migrate
16
+ ```
17
+
18
+ ## Settings
19
+
20
+ ```python
21
+ INSTALLED_APPS = [..., "benchdog_django"]
22
+
23
+ MIDDLEWARE = [
24
+ ...,
25
+ "django.contrib.auth.middleware.AuthenticationMiddleware",
26
+ "benchdog_django.middleware.BenchdogMiddleware", # After the authentication middleware.
27
+ ]
28
+
29
+ BENCHDOG = {
30
+ # The order is the promotion order. A protected environment needs an approval for a change.
31
+ "environments": [
32
+ {"name": "development", "promotes_to": "production"},
33
+ {"name": "production", "protected": True},
34
+ ],
35
+ "environment": "production", # The environment of the flag client. Default: the first one.
36
+ "authenticate": "myapp.flags.authenticate", # (request) -> {"id": ..., "name": ...} or None
37
+ "role_for": "myapp.flags.role_for", # (user, environment) -> "viewer" ... "admin" or None
38
+ "actor_for": "myapp.flags.actor_for", # (request, user) -> {"id": "user:42"} or None
39
+ "groups": "myapp.flags.GROUPS", # {"staff": lambda actor: ...}
40
+ "cron_secret": os.environ["BENCHDOG_CRON_SECRET"], # Turns on POST api/cron/schedules.
41
+ "references": "myapp.flags.references", # The output of `benchdog refs --json`.
42
+ "audience": "myapp.flags.AUDIENCE", # {"groups": [...], "actor_types": [...], "lookup_actors": fn}
43
+ }
44
+ ```
45
+
46
+ A text value of an object key is a dotted import path. You can also give the object.
47
+ `environments` and `authenticate` are necessary. The `store` key replaces the ORM store, for
48
+ example with a `benchdog.MemoryStore()` in tests.
49
+
50
+ ```python
51
+ # myapp/flags.py
52
+ def authenticate(request):
53
+ user = request.user
54
+ return {"id": str(user.pk), "name": user.get_full_name()} if user.is_authenticated else None
55
+
56
+
57
+ def role_for(user, environment):
58
+ return "admin"
59
+
60
+
61
+ def actor_for(request, user):
62
+ return {"id": f"user:{user['id']}"} if user else None
63
+ ```
64
+
65
+ The package includes the dashboard. `dashboard_dir` serves another build.
66
+
67
+ ## URLs
68
+
69
+ ```python
70
+ urlpatterns = [path("benchdog/", include("benchdog_django.urls"))]
71
+ ```
72
+
73
+ The dashboard is at `benchdog/`, and the API is at `benchdog/api/`. The API does not use the
74
+ Django CSRF check. To prevent CSRF, a write that has no `Authorization` or `X-Benchdog-Source`
75
+ header is not signed in. The dashboard sends `X-Benchdog-Source`.
76
+
77
+ ## Flags in a view and a template
78
+
79
+ ```python
80
+ from benchdog_django.middleware import BenchdogRequest
81
+
82
+
83
+ def checkout(request: BenchdogRequest):
84
+ if request.benchdog.enabled("new-checkout", request.benchdog_actor):
85
+ ...
86
+ theme = request.benchdog.value("theme", request.benchdog_actor, "light")
87
+ ```
88
+
89
+ ```django
90
+ {% load benchdog %}
91
+ {% flag_enabled "new-checkout" as on %}
92
+ {% if on %}...{% endif %}
93
+ ```
94
+
95
+ The template tag needs the `request` context processor. Evaluation does no I/O. Each request
96
+ reads the store version, and the client loads the flag set again when the version changes.
97
+
98
+ ## Schedules
99
+
100
+ Run this command from cron, for example each minute:
101
+
102
+ ```bash
103
+ python manage.py benchdog_schedules
104
+ ```
105
+
106
+ The evaluator applies due schedule steps at read time. The command writes them into the flags,
107
+ with an audit entry.
108
+
109
+ ## Checks
110
+
111
+ ```bash
112
+ uv run ruff check
113
+ uv run ruff format --check
114
+ uv run mypy
115
+ uv run pytest
116
+ ```
117
+
118
+ `test-apps/django` is a small Django project with this app. The HTTP contract suite
119
+ (`packages/api-tests`) runs against it from `pnpm python:check`.
@@ -0,0 +1,51 @@
1
+ [project]
2
+ name = "benchdog-django"
3
+ version = "0.1.0"
4
+ description = "Feature flags for Django: the Benchdog app, admin API, dashboard and template tags."
5
+ readme = "README.md"
6
+ license = "MIT"
7
+ license-files = ["LICENSE"]
8
+ authors = [{ name = "Joe Gaudet", email = "joe@joegaudet.com" }]
9
+ requires-python = ">=3.11"
10
+ dependencies = ["benchdog==0.1.0", "django>=4.2"]
11
+ classifiers = ["Framework :: Django", "Typing :: Typed"]
12
+
13
+ [project.urls]
14
+ Homepage = "https://joegaudet.com/benchdog/"
15
+ Source = "https://github.com/joegaudet/benchdog/tree/main/python/benchdog-django"
16
+ Issues = "https://github.com/joegaudet/benchdog/issues"
17
+
18
+ [build-system]
19
+ requires = ["hatchling>=1.27"]
20
+ build-backend = "hatchling.build"
21
+
22
+ [tool.hatch.build.targets.wheel]
23
+ packages = ["src/benchdog_django"]
24
+ # Git ignores the dashboard copy, so name it here, or the wheel leaves it out.
25
+ artifacts = ["src/benchdog_django/dashboard/**"]
26
+
27
+ [tool.uv.sources]
28
+ benchdog = { path = "../benchdog", editable = true }
29
+
30
+ [dependency-groups]
31
+ dev = ["django-stubs>=5.1", "mypy>=1.15", "pytest>=8.3", "pytest-django>=4.9", "ruff>=0.11"]
32
+
33
+ [tool.ruff]
34
+ target-version = "py311"
35
+ line-length = 100
36
+
37
+ [tool.ruff.lint]
38
+ select = ["E", "F", "W", "I", "B", "UP", "SIM", "RUF"]
39
+
40
+ [tool.mypy]
41
+ strict = true
42
+ files = ["src", "tests"]
43
+ plugins = ["mypy_django_plugin.main"]
44
+
45
+ [tool.django-stubs]
46
+ django_settings_module = "tests.settings"
47
+
48
+ [tool.pytest.ini_options]
49
+ testpaths = ["tests"]
50
+ pythonpath = ["."]
51
+ DJANGO_SETTINGS_MODULE = "tests.settings"
@@ -0,0 +1 @@
1
+ """Benchdog feature flags for Django. See https://joegaudet.com/benchdog/."""
@@ -0,0 +1,8 @@
1
+ from django.apps import AppConfig
2
+
3
+
4
+ class BenchdogConfig(AppConfig):
5
+ name = "benchdog_django"
6
+ label = "benchdog"
7
+ verbose_name = "Benchdog"
8
+ default_auto_field = "django.db.models.BigAutoField"
@@ -0,0 +1,52 @@
1
+ """The ``BENCHDOG`` setting.
2
+
3
+ The keys are the options of the Express host: ``store``, ``environments``, ``authenticate``,
4
+ ``role_for``, ``actor_for``, ``cron_secret``, ``references``, ``groups`` and ``audience``. Also
5
+ ``environment`` (the environment of the flag client; default: the first environment) and
6
+ ``dashboard_dir`` (the folder of the built dashboard). A text value of an object key is a dotted
7
+ import path, for example ``"myapp.flags.authenticate"``.
8
+ """
9
+
10
+ from typing import Any
11
+
12
+ from benchdog import FlagStore
13
+ from benchdog.admin_api import Environment, User
14
+ from django.conf import settings
15
+ from django.http import HttpRequest
16
+ from django.utils.module_loading import import_string
17
+
18
+ OBJECTS = {"store", "authenticate", "role_for", "actor_for", "references", "groups", "audience"}
19
+
20
+
21
+ def setting(name: str, default: Any = None) -> Any:
22
+ """One key of the ``BENCHDOG`` setting. A dotted path of an object key gives the object."""
23
+ value = settings.BENCHDOG.get(name, default)
24
+ return import_string(value) if name in OBJECTS and isinstance(value, str) else value
25
+
26
+
27
+ def environments() -> list[Environment]:
28
+ found: list[Environment] = setting("environments")
29
+ return found
30
+
31
+
32
+ def environment() -> str:
33
+ """The environment of the flag client."""
34
+ name: str = setting("environment") or environments()[0]["name"]
35
+ return name
36
+
37
+
38
+ def actor(request: HttpRequest, user: User | None) -> dict[str, Any] | None:
39
+ """The actor of a request: from the ``actor_for`` setting, or ``{"id": user["id"]}``."""
40
+ custom = setting("actor_for")
41
+ if custom:
42
+ found: dict[str, Any] | None = custom(request, user)
43
+ return found
44
+ return {"id": user["id"]} if user else None
45
+
46
+
47
+ def store() -> FlagStore:
48
+ """The ``store`` setting, or the store on the Django ORM."""
49
+ from .store import DjangoStore
50
+
51
+ found: FlagStore = setting("store") or DjangoStore()
52
+ return found