feat(devops): Feature Flagging url.path wrapper

ref: #675 #575
This commit is contained in:
2025-03-14 22:38:43 +09:30
parent 29553474f2
commit b22f4e2ea9
4 changed files with 128 additions and 0 deletions

View File

@ -29,6 +29,8 @@ Within Django the following locations have the feature flagging available
- Django DRF Router(s)
- Django URLs
- Management command to fetch feature flag file
- Caching of flags
@ -65,6 +67,44 @@ class MyView:
```
## Django URLs
To enable feature flagging for urls, substitute `from django.urls import path, re_path` with `from centurion_feature_flag.urls.django import path, re_path`. Then optionally, whilst calling the `path` function include attribute `feature_flag` with a string value of the feature flag id. Once enabled if an attempt to navigate to the url is made and for a disabled feature flag, a `HTTP/404` will be returned.
``` py
# urls.py
from django.contrib import admin
from centurion_feature_flag.urls.django import (
include,
path,
re_path
)
from my_app.views import home
urlpatterns = [
path('', home.HomeView.as_view(), name='home', feature_flag = '2025-00001'),
path('admin/', admin.site.urls, name='_administration'),
re_path(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}, feature_flag = '2025-00003'),
path("some-path/", include("my_app.urls"), feature_flag = '2025-00002'),
]
```
!!! tip
module `centurion_feature_flag.urls.django` also contains function `include` from `django.urls` so there is no need to import `django.urls` for this function.
!!! note
If you use feature flagging on the `path` function and its called with the `include` function as the view attribute, not all paths from the include function are available. As a consequence, sub-paths are unavailable. For example. The `some-path/` url can be navigated to whilst `some-path/sub-path/` can not be. This generally wont be an issue, although if you attempt to use the `reverse` function on the sub-path and the feature flag is disabled; the reverse function will raise an exception.
## DRF Router
Enabling feature flagging for Django DRF Routers is as simple as substituting `from rest_framework.routers import <router name>` with `from centurion_feature_flag.urls.routers import <router name>` then optionally updating the route register method with the feature flag to use. for example, using feature flag `2025-00001`

View File

@ -0,0 +1,46 @@
from django.urls.conf import (
_path as _django_path,
include, # pylint: disable=W0611:unused-import
partial,
RegexPattern as DjangoRegexPattern,
RoutePattern as DjangoRoutePattern,
)
from app import settings
from centurion_feature_flag.lib.feature_flag import CenturionFeatureFlagging
from centurion_feature_flag.views.disabled import FeatureFlagView
_feature_flagging: CenturionFeatureFlagging = None
if getattr(settings,'feature_flag', None):
_feature_flagging = CenturionFeatureFlagging(
url = settings.feature_flag['url'],
user_agent = settings.feature_flag['user_agent'],
cache_dir =settings.feature_flag['cache_dir'],
unique_id = settings.feature_flag.get('unique_id', None),
version = settings.feature_flag.get('version', None),
)
_feature_flagging.get()
def _path(route, view, kwargs=None, name=None, Pattern=None, feature_flag: str =None):
if feature_flag is not None:
if not _feature_flagging[feature_flag]:
view = FeatureFlagView.as_view()
return _django_path(route, view, kwargs=kwargs, name=name, Pattern=Pattern)
path = partial(_path, Pattern=DjangoRoutePattern)
re_path = partial(_path, Pattern=DjangoRegexPattern)

View File

@ -0,0 +1,38 @@
from django.shortcuts import Http404, HttpResponse #, redirect, render
from django.views.generic import View
class FeatureFlagView(View):
"""Featur Flag View
This view serves the purpose of being the stand-in view for a view that
has been disabled via a feature flag.
"""
def delete(self, request):
raise Http404()
def get(self, request):
raise Http404()
def head(self, request):
raise Http404()
def options(self, request):
raise Http404()
def patch(self, request):
raise Http404()
def post(self, request):
raise Http404()
def put(self, request):
raise Http404()