A Django app that allows creation of conditional logic in admin.
Project description
# django-conditions
[![Build Status](https://travis-ci.org/RevolutionTech/django-conditions.svg?branch=master)](https://travis-ci.org/RevolutionTech/django-conditions)
[![codecov](https://codecov.io/gh/RevolutionTech/django-conditions/branch/master/graph/badge.svg)](https://codecov.io/gh/RevolutionTech/django-conditions)
[![Dependency Status](https://www.versioneye.com/user/projects/56de7e4cdf573d0048dafc52/badge.svg?style=flat)](https://www.versioneye.com/user/projects/56de7e4cdf573d0048dafc52)
[![Documentation Status](https://readthedocs.org/projects/django-conditions/badge/?version=latest)](http://django-conditions.readthedocs.org/en/latest/)
[![PyPI](https://img.shields.io/pypi/dm/django-conditions.svg)](https://pypi.python.org/pypi/django-conditions/)
[![PyPI](https://img.shields.io/pypi/v/django-conditions.svg)](https://pypi.python.org/pypi/django-conditions/)
Move conditional logic that changes often from code into models so that the logic can be easily modified in admin. Some possible use cases:
- Segment your user base into cohorts with targeted messaging
- Provide different rewards to users depending on their expected value
- In a game, define the winning objectives of a mission/quest
- and many more...
## Installation
pip install django-conditions
## Basic Usage
Start by defining a condition in code:
```python
## condition_types.py
from conditions import Condition
class FullName(Condition):
# The name that appears in the db and represents your condition
condstr = 'FULL_NAME'
# Normal conditions define eval_bool, which takes in a user
# and returns a boolean
def eval_bool(self, user, **kwargs):
return bool(user.first_name and user.last_name)
```
Then add a ConditionsField to your model:
```python
## models.py
from django.db import models
from conditions import ConditionsField, conditions_from_module
import condition_types
class Campaign(models.Model):
text = models.TextField()
# The ConditionsField requires the definitions of all possible conditions
# conditions_from_module can take an imported module and sort this out for you
target = ConditionsField(definitions=conditions_from_module(condition_types))
```
In the model's change form on admin, you can enter JSON to represent when you want your condition to be satisfied.
```javascript
{
"all": ["FULL_NAME"]
}
```
Now you can use the logic you created in admin to determine the outcome of an event:
```python
## views.py
from django.http import HttpResponse
from conditions import eval_conditions
from models import Campaign
def profile(request):
for campaign in Campaign.objects.all():
if eval_conditions(campaign, 'target', request.user):
return HttpReponse(campaign.text)
return HttpResponse("Nothing new to see.")
```
Use django-conditions in your Django projects to change simple logic without having to re-deploy, and pass on the power to product managers and other non-engineers.
## More Information
django-conditions is under active development. You can follow my progress [on Trello](https://trello.com/b/XQnzHWYZ).
Full documentation is available [on Read The Docs](http://django-conditions.readthedocs.org/).
[![Build Status](https://travis-ci.org/RevolutionTech/django-conditions.svg?branch=master)](https://travis-ci.org/RevolutionTech/django-conditions)
[![codecov](https://codecov.io/gh/RevolutionTech/django-conditions/branch/master/graph/badge.svg)](https://codecov.io/gh/RevolutionTech/django-conditions)
[![Dependency Status](https://www.versioneye.com/user/projects/56de7e4cdf573d0048dafc52/badge.svg?style=flat)](https://www.versioneye.com/user/projects/56de7e4cdf573d0048dafc52)
[![Documentation Status](https://readthedocs.org/projects/django-conditions/badge/?version=latest)](http://django-conditions.readthedocs.org/en/latest/)
[![PyPI](https://img.shields.io/pypi/dm/django-conditions.svg)](https://pypi.python.org/pypi/django-conditions/)
[![PyPI](https://img.shields.io/pypi/v/django-conditions.svg)](https://pypi.python.org/pypi/django-conditions/)
Move conditional logic that changes often from code into models so that the logic can be easily modified in admin. Some possible use cases:
- Segment your user base into cohorts with targeted messaging
- Provide different rewards to users depending on their expected value
- In a game, define the winning objectives of a mission/quest
- and many more...
## Installation
pip install django-conditions
## Basic Usage
Start by defining a condition in code:
```python
## condition_types.py
from conditions import Condition
class FullName(Condition):
# The name that appears in the db and represents your condition
condstr = 'FULL_NAME'
# Normal conditions define eval_bool, which takes in a user
# and returns a boolean
def eval_bool(self, user, **kwargs):
return bool(user.first_name and user.last_name)
```
Then add a ConditionsField to your model:
```python
## models.py
from django.db import models
from conditions import ConditionsField, conditions_from_module
import condition_types
class Campaign(models.Model):
text = models.TextField()
# The ConditionsField requires the definitions of all possible conditions
# conditions_from_module can take an imported module and sort this out for you
target = ConditionsField(definitions=conditions_from_module(condition_types))
```
In the model's change form on admin, you can enter JSON to represent when you want your condition to be satisfied.
```javascript
{
"all": ["FULL_NAME"]
}
```
Now you can use the logic you created in admin to determine the outcome of an event:
```python
## views.py
from django.http import HttpResponse
from conditions import eval_conditions
from models import Campaign
def profile(request):
for campaign in Campaign.objects.all():
if eval_conditions(campaign, 'target', request.user):
return HttpReponse(campaign.text)
return HttpResponse("Nothing new to see.")
```
Use django-conditions in your Django projects to change simple logic without having to re-deploy, and pass on the power to product managers and other non-engineers.
## More Information
django-conditions is under active development. You can follow my progress [on Trello](https://trello.com/b/XQnzHWYZ).
Full documentation is available [on Read The Docs](http://django-conditions.readthedocs.org/).
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
django-conditions-0.9.9.tar.gz
(16.3 kB
view details)
File details
Details for the file django-conditions-0.9.9.tar.gz
.
File metadata
- Download URL: django-conditions-0.9.9.tar.gz
- Upload date:
- Size: 16.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a018dd5340a150c05ad350128f6983c382ca03be00dc9060fbe5bac67e745986 |
|
MD5 | f12996062f084d761f65f51697ced9e6 |
|
BLAKE2b-256 | ca232d7201ced058cec3e47712fc7e29bcb936b383860a3064038bfb191caf47 |