1# plain.flags 2 3Localfeatureflagsviadatabasemodels. 4 5Customflagsarewrittenassubclassesof[`Flag`](./flags.py). 6Youdefinetheflag's "key" and initial value, 7andtheresultswillbestoredinthedatabaseforfuturereference. 8 9```python 10# app/flags.py 11fromplain.flagsimportFlag 12 13 14classFooEnabled(Flag): 15def__init__(self,user): 16self.user=user 17 18defget_key(self): 19returnself.user 20 21defget_value(self): 22# Initially all users will have this feature disabled 23# and we'll enable them manually in the admin 24returnFalse 25``` 26 27UseflagsinHTMLtemplates: 28 29```html 30{%ifflags.FooEnabled(request.user)%} 31<p>Fooisenabledforyou!</p> 32{%else%} 33<p>Fooisdisabledforyou.</p> 34{%endif%} 35``` 36 37OrinPython: 38 39```python 40importflags 41 42 43print(flags.FooEnabled(user).value) 44``` 45 46## Installation 47 48```python 49INSTALLED_PACKAGES=[ 50... 51"plain.flags", 52] 53``` 54 55Createa`flags.py`atthetopofyour`app`(orpoint`settings.FLAGS_MODULE`toadifferentlocation). 56 57## Advanced usage 58 59Ultimatelyyoucandowhateveryouwantinsideof`get_key`and`get_value`. 60 61```python 62classOrganizationFeature(Flag): 63url_param_name="" 64 65def__init__(self,request=None,organization=None): 66# Both of these are optional, but will usually both be given 67self.request=request 68self.organization=organization 69 70defget_key(self): 71if( 72self.url_param_name 73andself.request 74andself.url_param_nameinself.request.query_params 75): 76returnNone 77 78ifnotself.organization: 79# Don't save the flag result for PRs without an organization 80returnNone 81 82returnself.organization 83 84defget_value(self): 85ifself.url_param_nameandself.request: 86ifself.request.query_params.get(self.url_param_name)=="1": 87returnTrue 88 89ifself.request.query_params.get(self.url_param_name)=="0": 90returnFalse 91 92ifnotself.organization: 93returnFalse 94 95# All organizations will start with False, 96# and I'll override in the DB for the ones that should be True 97returnFalse 98 99100classAIEnabled(OrganizationFeature):101pass102103```