Django Logic - easy way to implement state-based business logic
Django Logic is a lightweight workflow framework aims to solve an open problem "Where to put the business logic in Django?".
Full documentation for the project is available at wiki
The Django-Logic package provides a set of tools helping to build a reliable product within a limited time. Here is the functionality the package offers for you:
- Implement state-based business processes combined into Processes.
- Provides a business logic layer as a set of conditions, side-effects, permissions, and even celery-tasks combined into a transition class.
- In progress states
- REST API actions - every transition could be turned into a POST request action within seconds by extending your ViewSet and Serialiser of Django-Rest-Framework
- Background transitions via django-logic-celery.
- Draw your business processes to get a full picture of all transitions and conditions.
- Easy to read the business process
- One and only one way to implement business logic. You will be able to extend and improve the Django-Logic functionality and available handlers. However, the business logic will remain the same and by following SOLID principles.
- Test your business logic by unit-tests as pure functions.
- Protects from overwritten states, locks, etc. already implemented in Django Logic and you could control the behaviour.
- Several states can be combined under the same Model.
Use the package manager pip to install Django-Logic.
pip install django-logic
- Create a django project and start a new app
- Create a new file
process.pyunder the app and define your process.
from django_logic import Process as BaseProcess, Transition class Process(BaseProcess): states = ( ('draft', 'Draft'), ('paid', 'Paid'), ('void', 'Void'), ) transitions = [ Transition(action_name='approve', sources=['draft'], target='approved'), Transition(action_name='void', sources=['draft', 'approved'], target='void'), ]
- Display the process. It requires to install graphviz.
pip install graphviz
- Bind the process with a model
from django.db import models from django_logic.process import ProcessManager from .process import Process as InvoiceProcess class Invoice(ProcessManager.bind_state_fields(status=InvoiceProcess), models.Model): status = models.CharField(choices=InvoiceProcess.states, default='draft', max_length=16, blank=True)
invoice = Invoice.objects.create() print(list([transition.action_name for transition in invoice.process.get_available_transitions())]) >> ['approve', 'void'] invoice.process.approve() invoice.status >> 'approved' print(list([transition.action_name for transition in invoice.process.get_available_transitions())]) >> ['void'] invoice.process.void() invoice.status >> 'void'
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
Release history Release notifications | RSS feed
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size django_logic-0.0.11-py3-none-any.whl (21.6 kB)||File type Wheel||Python version py3||Upload date||Hashes View|
|Filename, size django-logic-0.0.11.tar.gz (16.4 kB)||File type Source||Python version None||Upload date||Hashes View|
Hashes for django_logic-0.0.11-py3-none-any.whl