django-harel 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. django_harel-0.1.0/.gitignore +13 -0
  2. django_harel-0.1.0/CHANGELOG.md +42 -0
  3. django_harel-0.1.0/LICENSE +201 -0
  4. django_harel-0.1.0/PKG-INFO +156 -0
  5. django_harel-0.1.0/README.md +128 -0
  6. django_harel-0.1.0/pyproject.toml +84 -0
  7. django_harel-0.1.0/src/django_harel/__init__.py +14 -0
  8. django_harel-0.1.0/src/django_harel/admin.py +641 -0
  9. django_harel-0.1.0/src/django_harel/apps.py +7 -0
  10. django_harel-0.1.0/src/django_harel/conf.py +176 -0
  11. django_harel-0.1.0/src/django_harel/fields.py +194 -0
  12. django_harel-0.1.0/src/django_harel/inspection.py +378 -0
  13. django_harel-0.1.0/src/django_harel/management/__init__.py +0 -0
  14. django_harel-0.1.0/src/django_harel/management/commands/__init__.py +0 -0
  15. django_harel-0.1.0/src/django_harel/management/commands/harel_worker.py +208 -0
  16. django_harel-0.1.0/src/django_harel/migrations/0001_initial.py +178 -0
  17. django_harel-0.1.0/src/django_harel/migrations/__init__.py +0 -0
  18. django_harel-0.1.0/src/django_harel/models.py +46 -0
  19. django_harel-0.1.0/src/django_harel/py.typed +0 -0
  20. django_harel-0.1.0/src/django_harel/schema.py +37 -0
  21. django_harel-0.1.0/src/django_harel/signals.py +61 -0
  22. django_harel-0.1.0/src/django_harel/static/django_harel/admin.css +16 -0
  23. django_harel-0.1.0/src/django_harel/static/django_harel/admin.js +13 -0
  24. django_harel-0.1.0/src/django_harel/store.py +469 -0
  25. django_harel-0.1.0/src/django_harel/templates/admin/django_harel/machine_detail.html +109 -0
  26. django_harel-0.1.0/src/django_harel/templates/admin/django_harel/machines.html +116 -0
  27. django_harel-0.1.0/src/django_harel/templates/admin/django_harel/redrive.html +42 -0
  28. django_harel-0.1.0/src/django_harel/transport.py +342 -0
  29. django_harel-0.1.0/tests/__init__.py +0 -0
  30. django_harel-0.1.0/tests/app/__init__.py +0 -0
  31. django_harel-0.1.0/tests/app/actions.py +65 -0
  32. django_harel-0.1.0/tests/app/migrations/0001_initial.py +33 -0
  33. django_harel-0.1.0/tests/app/migrations/0002_parcel.py +26 -0
  34. django_harel-0.1.0/tests/app/migrations/0003_tick.py +28 -0
  35. django_harel-0.1.0/tests/app/migrations/0004_ticket.py +24 -0
  36. django_harel-0.1.0/tests/app/migrations/__init__.py +0 -0
  37. django_harel-0.1.0/tests/app/models.py +39 -0
  38. django_harel-0.1.0/tests/conftest.py +21 -0
  39. django_harel-0.1.0/tests/machines/fulfilment.stm +42 -0
  40. django_harel-0.1.0/tests/machines/job.stm +16 -0
  41. django_harel-0.1.0/tests/machines/order.stm +20 -0
  42. django_harel-0.1.0/tests/machines/parcel.stm +17 -0
  43. django_harel-0.1.0/tests/settings.py +86 -0
  44. django_harel-0.1.0/tests/test_admin.py +462 -0
  45. django_harel-0.1.0/tests/test_composition.py +88 -0
  46. django_harel-0.1.0/tests/test_concurrency.py +90 -0
  47. django_harel-0.1.0/tests/test_contracts.py +94 -0
  48. django_harel-0.1.0/tests/test_fields.py +100 -0
  49. django_harel-0.1.0/tests/test_runners.py +218 -0
  50. django_harel-0.1.0/tests/test_schema.py +89 -0
  51. django_harel-0.1.0/tests/test_signals.py +124 -0
  52. django_harel-0.1.0/tests/test_worker.py +305 -0
  53. django_harel-0.1.0/tests/urls.py +4 -0
@@ -0,0 +1,13 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ *.egg-info/
4
+ dist/
5
+ build/
6
+ .venv/
7
+ docs/_build/
8
+ *.db
9
+ .pytest_cache/
10
+ .ruff_cache/
11
+ .mypy_cache/
12
+ bench/logs/
13
+ .vscode/
@@ -0,0 +1,42 @@
1
+ # Changelog
2
+
3
+ All notable changes to django-harel. The format follows
4
+ [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and versions follow
5
+ [Semantic Versioning](https://semver.org/) (pre-1.0: a minor version may break the API).
6
+
7
+ ## 0.1.0 - 2026-10-31
8
+
9
+ The first release. It requires Python 3.11+, Django 5.2+ and harel 0.10+.
10
+
11
+ ### Added
12
+
13
+ - **Storage on Django's database.** `DjangoStore` and `DjangoTransport` implement harel's
14
+ `ExecutionStore` and `Transport` protocols over Django's own connection, so an execution's
15
+ state, queue and timers join the caller's transaction. They pass harel's contract suites
16
+ (`harel.testing`).
17
+ - **SQLite, PostgreSQL and MySQL/MariaDB.** One migration creates harel's schema version 6. On
18
+ SQLite and PostgreSQL the tables are harel's own, so harel's native backends and tools can
19
+ read them.
20
+ - **`ExecutionField`.** It ties a machine to a model, as an attribute named after the machine:
21
+ `article.publishing.send("Submit")`, `.state`, `.process(...)` in the request. With
22
+ `autostart=True`, the execution starts with the row.
23
+ - **Runners.** `distributed_runner()` queues events for a worker. `durable_runner()` runs them in
24
+ the request. `definitions()` loads the machines in `DJANGO_HAREL["MACHINES"]`.
25
+ - **`manage.py harel_worker`.** Each event runs in one transaction: its actions' writes, the
26
+ machine's step and the acknowledgement commit together.
27
+ - `--machines` dedicates a worker to some machines.
28
+ - At startup the worker publishes the machine versions it runs.
29
+ - It records a heartbeat every `HEARTBEAT` seconds and removes it when it stops.
30
+ - **Signals.** `step_committed`, `execution_finished` and `execution_failed` are sent once the
31
+ step's transaction has committed.
32
+ - **The admin.**
33
+ - **Executions**: each execution's diagram with its active states, its tree of regions and
34
+ children, what it waits for, and its timeline.
35
+ - **Actions** on the executions list: suspend, resume, cancel, terminate, purge, and redrive
36
+ failed executions at a chosen state.
37
+ - **Machines**: every machine with its version, diagram and top-level executions; counts by
38
+ status; overdue timers; the queue by machine; and every worker's heartbeat. It flags work
39
+ no live worker claims, and a gone worker can be forgotten.
40
+ - **Testing helpers.** `ALL_TABLES`, to empty harel's tables between `TransactionTestCase` tests.
41
+ - **An example app** (`example/`): articles and orders, with a REST API, a worker and demo data,
42
+ on PostgreSQL with `docker compose up`.
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or Derivative
95
+ Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work, excluding
103
+ those notices that do not pertain to any part of the Derivative
104
+ Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one of
111
+ the following places: within a NOTICE text file distributed as
112
+ part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and do
117
+ not modify the License. You may add Your own attribution notices
118
+ within Derivative Works that You distribute, alongside or as an
119
+ addendum to the NOTICE text from the Work, provided that such
120
+ additional attribution notices cannot be construed as modifying
121
+ the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2026 Alberto Casado
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,156 @@
1
+ Metadata-Version: 2.5
2
+ Name: django-harel
3
+ Version: 0.1.0
4
+ Summary: Django integration for harel statecharts — a Celery alternative for state-machine-shaped work
5
+ Project-URL: Homepage, https://github.com/acasadom/django-harel
6
+ Project-URL: Repository, https://github.com/acasadom/django-harel
7
+ Project-URL: Issues, https://github.com/acasadom/django-harel/issues
8
+ Author: Alberto Casado
9
+ License-Expression: Apache-2.0
10
+ License-File: LICENSE
11
+ Keywords: celery,django,durable-execution,state-machine,statechart,workflow
12
+ Classifier: Development Status :: 3 - Alpha
13
+ Classifier: Framework :: Django
14
+ Classifier: Framework :: Django :: 5.2
15
+ Classifier: Framework :: Django :: 6.0
16
+ Classifier: Intended Audience :: Developers
17
+ Classifier: Programming Language :: Python :: 3
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Programming Language :: Python :: 3.13
21
+ Classifier: Programming Language :: Python :: 3.14
22
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
23
+ Classifier: Typing :: Typed
24
+ Requires-Python: <4,>=3.11
25
+ Requires-Dist: django>=5.2
26
+ Requires-Dist: harel>=0.10.0
27
+ Description-Content-Type: text/markdown
28
+
29
+ # django-harel
30
+
31
+ *Django integration for harel statecharts — a Celery alternative for state-machine-shaped work.*
32
+
33
+ [![CI](https://github.com/acasadom/django-harel/actions/workflows/ci.yml/badge.svg)](https://github.com/acasadom/django-harel/actions/workflows/ci.yml)
34
+ [![License: Apache 2.0](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/acasadom/django-harel/blob/main/LICENSE)
35
+ ![Python](https://img.shields.io/badge/python-3.11%20%7C%203.12%20%7C%203.13%20%7C%203.14-blue)
36
+
37
+ **django-harel** runs [harel](https://github.com/acasadom/harel) statecharts on Django's
38
+ database. You write a process as a `.stm` machine and its actions as plain functions that use
39
+ the ORM. Its state, its queue and its timers are rows in your database, next to your models.
40
+ It works with SQLite, PostgreSQL and MySQL, and needs no other infrastructure.
41
+
42
+ Requires Python 3.11+, Django 5.2+ and harel 0.10+. Alpha: the API may still change.
43
+
44
+ ```
45
+ event Submit {}
46
+ event Approve {}
47
+ event Reject { note: string }
48
+
49
+ machine publishing {
50
+ context { article_id: int }
51
+
52
+ initial Draft
53
+
54
+ state Draft {}
55
+ state InReview { on enter articles.actions.on_in_review }
56
+ state Approved {}
57
+
58
+ final Published success { on enter articles.actions.on_published }
59
+ final Rejected failure { on enter articles.actions.on_rejected }
60
+
61
+ from Draft to InReview on Submit
62
+ from InReview to Approved on Approve
63
+ from Approved to Published
64
+ from InReview to Rejected on Reject
65
+ }
66
+ ```
67
+
68
+ ## Two ways to run a machine
69
+
70
+ **In the background**, like Celery, but with the coordination in the machine rather than in
71
+ chained tasks and status fields. The request records the event and answers at once. A worker
72
+ runs it. Waiting days for a webhook, retrying with back-off, timing out, fanning out and
73
+ joining are all part of the machine. A machine waiting for its next event is just a row.
74
+
75
+ **In the request**, for flows the user drives step by step: wizards, checkouts. The request
76
+ runs the event to the end and renders the new state. There is no queue and no worker.
77
+
78
+ ## Transactions
79
+
80
+ Everything goes through the connection Django already has, so it joins your transactions:
81
+
82
+ ```python
83
+ class Article(models.Model):
84
+ ...
85
+ publishing = ExecutionField("publishing", autostart=True)
86
+
87
+
88
+ with transaction.atomic():
89
+ article = Article.objects.create(...) # its publishing execution starts with it
90
+ ```
91
+
92
+ If the block rolls back, the execution and its queued `Start` roll back with the article. No
93
+ worker ever runs a workflow for a row that doesn't exist, and you never need
94
+ `transaction.on_commit()`.
95
+
96
+ On the worker, each event is a transaction too: what its actions write, the machine's step and
97
+ the acknowledgement commit together. A worker that dies halfway leaves nothing behind.
98
+
99
+ ## Install
100
+
101
+ ```bash
102
+ pip install django-harel
103
+ ```
104
+
105
+ ```python
106
+ INSTALLED_APPS = [..., "django_harel"]
107
+
108
+ DJANGO_HAREL = {
109
+ "MACHINES": {"publishing": "articles/machines/publishing.stm"},
110
+ }
111
+ ```
112
+
113
+ ```bash
114
+ python manage.py migrate
115
+ python manage.py harel_worker
116
+ ```
117
+
118
+ ## Use
119
+
120
+ ```python
121
+ article.publishing.send("Submit") # background: queued, the worker runs it
122
+ article.publishing.state # "InReview"
123
+
124
+ order.checkout.process("Pay", token=token) # in the request: runs now, in this transaction
125
+ ```
126
+
127
+ `ExecutionField` ties a machine to a model; the runners underneath (`distributed_runner()`,
128
+ `durable_runner()`) work for executions that don't belong to a row.
129
+
130
+ The Django admin's **Machines** catalog lists every machine: its diagram, what it invokes, its
131
+ top-level executions, and their counts by status. It also shows the queue by machine and every
132
+ worker's heartbeat. It flags work no live worker claims, a timer nothing is firing, and a worker
133
+ that is gone. From the executions list you can suspend, resume, cancel, terminate, redrive or
134
+ purge them.
135
+
136
+ Signals (`step_committed`, `execution_finished`, `execution_failed`) tell your code about every
137
+ step, once its transaction has committed.
138
+
139
+ ## Example
140
+
141
+ [`example/`](https://github.com/acasadom/django-harel/tree/main/example) is a Django app with
142
+ a REST API (django-ninja) and a worker, on PostgreSQL with `docker compose up`. It has two machines:
143
+
144
+ - **articles**: the publishing workflow above, the one the tutorial builds.
145
+ - **orders**: shipping and billing in parallel regions, one parcel per item (a fan-out),
146
+ payment timers, and a refused card to redrive from the admin.
147
+
148
+ See [`example/README.md`](https://github.com/acasadom/django-harel/blob/main/example/README.md).
149
+
150
+ ## Documentation
151
+
152
+ [acasadom.github.io/django-harel](https://acasadom.github.io/django-harel/)
153
+
154
+ ## License
155
+
156
+ Apache 2.0
@@ -0,0 +1,128 @@
1
+ # django-harel
2
+
3
+ *Django integration for harel statecharts — a Celery alternative for state-machine-shaped work.*
4
+
5
+ [![CI](https://github.com/acasadom/django-harel/actions/workflows/ci.yml/badge.svg)](https://github.com/acasadom/django-harel/actions/workflows/ci.yml)
6
+ [![License: Apache 2.0](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/acasadom/django-harel/blob/main/LICENSE)
7
+ ![Python](https://img.shields.io/badge/python-3.11%20%7C%203.12%20%7C%203.13%20%7C%203.14-blue)
8
+
9
+ **django-harel** runs [harel](https://github.com/acasadom/harel) statecharts on Django's
10
+ database. You write a process as a `.stm` machine and its actions as plain functions that use
11
+ the ORM. Its state, its queue and its timers are rows in your database, next to your models.
12
+ It works with SQLite, PostgreSQL and MySQL, and needs no other infrastructure.
13
+
14
+ Requires Python 3.11+, Django 5.2+ and harel 0.10+. Alpha: the API may still change.
15
+
16
+ ```
17
+ event Submit {}
18
+ event Approve {}
19
+ event Reject { note: string }
20
+
21
+ machine publishing {
22
+ context { article_id: int }
23
+
24
+ initial Draft
25
+
26
+ state Draft {}
27
+ state InReview { on enter articles.actions.on_in_review }
28
+ state Approved {}
29
+
30
+ final Published success { on enter articles.actions.on_published }
31
+ final Rejected failure { on enter articles.actions.on_rejected }
32
+
33
+ from Draft to InReview on Submit
34
+ from InReview to Approved on Approve
35
+ from Approved to Published
36
+ from InReview to Rejected on Reject
37
+ }
38
+ ```
39
+
40
+ ## Two ways to run a machine
41
+
42
+ **In the background**, like Celery, but with the coordination in the machine rather than in
43
+ chained tasks and status fields. The request records the event and answers at once. A worker
44
+ runs it. Waiting days for a webhook, retrying with back-off, timing out, fanning out and
45
+ joining are all part of the machine. A machine waiting for its next event is just a row.
46
+
47
+ **In the request**, for flows the user drives step by step: wizards, checkouts. The request
48
+ runs the event to the end and renders the new state. There is no queue and no worker.
49
+
50
+ ## Transactions
51
+
52
+ Everything goes through the connection Django already has, so it joins your transactions:
53
+
54
+ ```python
55
+ class Article(models.Model):
56
+ ...
57
+ publishing = ExecutionField("publishing", autostart=True)
58
+
59
+
60
+ with transaction.atomic():
61
+ article = Article.objects.create(...) # its publishing execution starts with it
62
+ ```
63
+
64
+ If the block rolls back, the execution and its queued `Start` roll back with the article. No
65
+ worker ever runs a workflow for a row that doesn't exist, and you never need
66
+ `transaction.on_commit()`.
67
+
68
+ On the worker, each event is a transaction too: what its actions write, the machine's step and
69
+ the acknowledgement commit together. A worker that dies halfway leaves nothing behind.
70
+
71
+ ## Install
72
+
73
+ ```bash
74
+ pip install django-harel
75
+ ```
76
+
77
+ ```python
78
+ INSTALLED_APPS = [..., "django_harel"]
79
+
80
+ DJANGO_HAREL = {
81
+ "MACHINES": {"publishing": "articles/machines/publishing.stm"},
82
+ }
83
+ ```
84
+
85
+ ```bash
86
+ python manage.py migrate
87
+ python manage.py harel_worker
88
+ ```
89
+
90
+ ## Use
91
+
92
+ ```python
93
+ article.publishing.send("Submit") # background: queued, the worker runs it
94
+ article.publishing.state # "InReview"
95
+
96
+ order.checkout.process("Pay", token=token) # in the request: runs now, in this transaction
97
+ ```
98
+
99
+ `ExecutionField` ties a machine to a model; the runners underneath (`distributed_runner()`,
100
+ `durable_runner()`) work for executions that don't belong to a row.
101
+
102
+ The Django admin's **Machines** catalog lists every machine: its diagram, what it invokes, its
103
+ top-level executions, and their counts by status. It also shows the queue by machine and every
104
+ worker's heartbeat. It flags work no live worker claims, a timer nothing is firing, and a worker
105
+ that is gone. From the executions list you can suspend, resume, cancel, terminate, redrive or
106
+ purge them.
107
+
108
+ Signals (`step_committed`, `execution_finished`, `execution_failed`) tell your code about every
109
+ step, once its transaction has committed.
110
+
111
+ ## Example
112
+
113
+ [`example/`](https://github.com/acasadom/django-harel/tree/main/example) is a Django app with
114
+ a REST API (django-ninja) and a worker, on PostgreSQL with `docker compose up`. It has two machines:
115
+
116
+ - **articles**: the publishing workflow above, the one the tutorial builds.
117
+ - **orders**: shipping and billing in parallel regions, one parcel per item (a fan-out),
118
+ payment timers, and a refused card to redrive from the admin.
119
+
120
+ See [`example/README.md`](https://github.com/acasadom/django-harel/blob/main/example/README.md).
121
+
122
+ ## Documentation
123
+
124
+ [acasadom.github.io/django-harel](https://acasadom.github.io/django-harel/)
125
+
126
+ ## License
127
+
128
+ Apache 2.0
@@ -0,0 +1,84 @@
1
+ [project]
2
+ name = "django-harel"
3
+ dynamic = ["version"]
4
+ description = "Django integration for harel statecharts — a Celery alternative for state-machine-shaped work"
5
+ readme = "README.md"
6
+ license = "Apache-2.0"
7
+ license-files = ["LICENSE"]
8
+ authors = [{ name = "Alberto Casado" }]
9
+ keywords = ["statechart", "state-machine", "django", "celery", "durable-execution", "workflow"]
10
+ classifiers = [
11
+ "Development Status :: 3 - Alpha",
12
+ "Framework :: Django",
13
+ "Framework :: Django :: 5.2",
14
+ "Framework :: Django :: 6.0",
15
+ "Intended Audience :: Developers",
16
+ "Topic :: Software Development :: Libraries :: Python Modules",
17
+ "Programming Language :: Python :: 3",
18
+ "Programming Language :: Python :: 3.11",
19
+ "Programming Language :: Python :: 3.12",
20
+ "Programming Language :: Python :: 3.13",
21
+ "Programming Language :: Python :: 3.14",
22
+ "Typing :: Typed",
23
+ ]
24
+ requires-python = ">=3.11,<4"
25
+ dependencies = [
26
+ "harel>=0.10.0",
27
+ "django>=5.2",
28
+ ]
29
+
30
+ [project.urls]
31
+ Homepage = "https://github.com/acasadom/django-harel"
32
+ Repository = "https://github.com/acasadom/django-harel"
33
+ Issues = "https://github.com/acasadom/django-harel/issues"
34
+
35
+ [dependency-groups]
36
+ dev = [
37
+ "pytest",
38
+ "pytest-django",
39
+ "ruff",
40
+ "mypy",
41
+ "django-stubs",
42
+ "harel[lsp]>=0.10.0",
43
+ "django-ninja",
44
+ "psycopg[binary]>=3.3.6",
45
+ "pymysql>=1.2.3",
46
+ ]
47
+
48
+ [build-system]
49
+ requires = ["hatchling", "hatch-vcs"]
50
+ build-backend = "hatchling.build"
51
+
52
+ [tool.hatch.version]
53
+ source = "vcs"
54
+
55
+ [tool.hatch.build.targets.wheel]
56
+ packages = ["src/django_harel"]
57
+
58
+ [tool.hatch.build.targets.sdist]
59
+ include = ["/src/django_harel", "/tests", "/README.md", "/CHANGELOG.md", "/LICENSE", "/pyproject.toml"]
60
+
61
+ [tool.pytest.ini_options]
62
+ pythonpath = ["src", "."]
63
+ testpaths = ["tests"]
64
+ DJANGO_SETTINGS_MODULE = "tests.settings"
65
+
66
+ [tool.ruff]
67
+ line-length = 110
68
+ target-version = "py311"
69
+
70
+ [tool.ruff.lint]
71
+ select = ["E", "F", "I", "W"]
72
+ ignore = ["E501"]
73
+
74
+ [tool.ruff.lint.isort]
75
+ known-first-party = ["django_harel"]
76
+
77
+ [tool.mypy]
78
+ files = ["src"]
79
+ plugins = ["mypy_django_plugin.main"]
80
+ ignore_missing_imports = true
81
+ check_untyped_defs = true
82
+
83
+ [tool.django-stubs]
84
+ django_settings_module = "tests.settings"
@@ -0,0 +1,14 @@
1
+ from django_harel.conf import definitions, distributed_runner, durable_runner
2
+ from django_harel.fields import ExecutionField, instance_for
3
+ from django_harel.store import DjangoStore
4
+ from django_harel.transport import DjangoTransport
5
+
6
+ __all__ = [
7
+ "DjangoStore",
8
+ "DjangoTransport",
9
+ "ExecutionField",
10
+ "definitions",
11
+ "distributed_runner",
12
+ "durable_runner",
13
+ "instance_for",
14
+ ]