Skip to main content

A Django app that allows creation of conditional logic in admin.

Project description

django-conditions

Build Status codecov Codacy Badge Dependency Status Documentation Status

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

First install the django-conditions package:

pip install django-conditions

Then add conditions to your INSTALLED_APPS setting:

## settings.py
INSTALLED_APPS = [
    ...
    'conditions',
]

Basic Usage

Start by defining a condition in code:

## 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:

## 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.

{
    "all": ["FULL_NAME"]
}

Now you can use the logic you created in admin to determine the outcome of an event:

## 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

Full documentation is available on Read The Docs.

Project details


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.17.tar.gz (15.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_conditions-0.9.17-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

Details for the file django-conditions-0.9.17.tar.gz.

File metadata

  • Download URL: django-conditions-0.9.17.tar.gz
  • Upload date:
  • Size: 15.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.8.7 Linux/4.15.0-1077-gcp

File hashes

Hashes for django-conditions-0.9.17.tar.gz
Algorithm Hash digest
SHA256 312abea2d3ee3db1e1e3264a06e17cd7c86004585b36169e9903d7b555521f18
MD5 c14191bf54d2e8bd76f45ed6c08e72a7
BLAKE2b-256 5bcfc4f474e1718635ac68b4daa2376b6a3c18d32149ad77e903cc1c91eb0b2b

See more details on using hashes here.

File details

Details for the file django_conditions-0.9.17-py3-none-any.whl.

File metadata

  • Download URL: django_conditions-0.9.17-py3-none-any.whl
  • Upload date:
  • Size: 16.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.8.7 Linux/4.15.0-1077-gcp

File hashes

Hashes for django_conditions-0.9.17-py3-none-any.whl
Algorithm Hash digest
SHA256 a4b803d080d402453c4e6defd2d24e745d6afa27cc6940f6a307b764e511ad3d
MD5 7fb48ab9103a1f00eeedcb5c187e172a
BLAKE2b-256 8348de26447e1e2fb5f57ca9f778d61a8ed920bfddcf387da13376dbb3e257ad

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page