Skip to main content

django-conditions

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

Metadata

Release files for django-conditions 0.9.18

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-conditions 0.9.18
File Size Uploaded Provenance Actions
django-conditions-0.9.18.tar.gz 15.6 kB Without provenance Details for django-conditions-0.9.18.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-conditions 0.9.18
File Interpreter ABI Platform Actions
django_conditions-0.9.18-py3-none-any.whl Python 3 none any Details for django_conditions-0.9.18-py3-none-any.whl

Total release size: 32.1 kB

Release files / django-conditions-0.9.18.tar.gz

Download URL django-conditions-0.9.18.tar.gz
Size 15.6 kB
Tags Source
SHA-256 checksum
How to use checksums
01caf94f035f713cab415972a30ddf9c2a55a3b5be2379d25c8780dfb1ae1d69
BLAKE2b-256 checksum
How to use checksums
49567d9904c68bad36b5871636f0000f4777b497001fe38c19b6410c2f1ea42d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.8.12 Linux/5.11.0-1028-azure

Release files / django_conditions-0.9.18-py3-none-any.whl

Download URL django_conditions-0.9.18-py3-none-any.whl
Size 16.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1d59d0314df593ed712c16a15a5f77da9685d5e65d1fced5fe66e770cbd9948d
BLAKE2b-256 checksum
How to use checksums
353ab293cc768753b2715664042e49835a0b48c1e55bbe5e23a244cbfbac98f1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.8.12 Linux/5.11.0-1028-azure

Security for django-conditions 0.9.18

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page