karni-approvals 0.3.1__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.
@@ -0,0 +1,38 @@
1
+ .learning/
2
+
3
+ # Python
4
+ __pycache__/
5
+ *.py[cod]
6
+ *.egg-info/
7
+ .venv/
8
+ venv/
9
+ .pytest_cache/
10
+ .mypy_cache/
11
+ .ruff_cache/
12
+ .coverage
13
+ htmlcov/
14
+
15
+ # Build artifacts
16
+ dist/
17
+ build/
18
+ packages/karni-cli/karni_cli/_templates/
19
+ packages/karni-cli/karni_cli/_agent/
20
+ packages/karni-cli/karni_cli/_changelog/
21
+ packages/karni-cli/karni_cli/_errorcodes/
22
+
23
+ # Node
24
+ node_modules/
25
+ storybook-static/
26
+ *.tsbuildinfo
27
+
28
+ # Local env / editor / tooling
29
+ .env
30
+ .env.*
31
+ !.env.example
32
+ .DS_Store
33
+ .idea/
34
+ .claude/scheduled_tasks.lock
35
+ .claude/settings.local.json
36
+
37
+ # MkDocs build output
38
+ site/
@@ -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
95
+ Derivative 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,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative 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
111
+ of the following places: within a NOTICE text file distributed
112
+ as 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
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying 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 [yyyy] [name of copyright owner]
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,160 @@
1
+ Metadata-Version: 2.5
2
+ Name: karni-approvals
3
+ Version: 0.3.1
4
+ Summary: Maker-checker approvals module for the Karni Django + React framework: a second person approves high-value changes before they are written, with a queue, an API, notifications and a full audit trail.
5
+ Project-URL: Homepage, https://github.com/cbsshekhawat18-lab/karni
6
+ Project-URL: Documentation, https://cbsshekhawat18-lab.github.io/karni/
7
+ Project-URL: Repository, https://github.com/cbsshekhawat18-lab/karni
8
+ Project-URL: Issues, https://github.com/cbsshekhawat18-lab/karni/issues
9
+ License-Expression: Apache-2.0
10
+ License-File: LICENSE
11
+ Keywords: approvals,django,four-eyes,karni,maker-checker,react,workflow
12
+ Classifier: Development Status :: 3 - Alpha
13
+ Classifier: Environment :: Web Environment
14
+ Classifier: Framework :: Django
15
+ Classifier: Framework :: Django :: 5.2
16
+ Classifier: Intended Audience :: Developers
17
+ Classifier: Operating System :: OS Independent
18
+ Classifier: Programming Language :: Python :: 3
19
+ Classifier: Programming Language :: Python :: 3 :: Only
20
+ Classifier: Programming Language :: Python :: 3.12
21
+ Classifier: Programming Language :: Python :: 3.13
22
+ Classifier: Programming Language :: Python :: 3.14
23
+ Classifier: Typing :: Typed
24
+ Requires-Python: >=3.12
25
+ Requires-Dist: karni<0.4,>=0.3
26
+ Description-Content-Type: text/markdown
27
+
28
+ # karni-approvals
29
+
30
+ Second-person (maker-checker) approval for high-value changes in a
31
+ [Karni](https://github.com/cbsshekhawat18-lab/karni) project. A model opts
32
+ in; from then on a guarded change by anyone — through the API, the admin or
33
+ plain code — becomes a **pending change holding the diff instead of a
34
+ write**. A checker holding the approve permission applies or rejects it,
35
+ with a reason. The maker can never be the checker, in code.
36
+
37
+ ## Install
38
+
39
+ ```sh
40
+ pip install karni-approvals
41
+ karni add approvals
42
+ karni migrate
43
+ ```
44
+
45
+ `karni add approvals` adds the dependency, wires `karni_approvals` into the
46
+ `# karni:apps` block and records this module's agent guidance. No URL
47
+ include is needed: the API rides the auto-router. Keep the Celery worker
48
+ running — notifications and the hourly timeout sweep are tasks.
49
+
50
+ ## Opt a model in
51
+
52
+ ```python
53
+ from datetime import timedelta
54
+ from karni.models import BaseModel
55
+
56
+ class Invoice(BaseModel):
57
+ ...
58
+ class Karni:
59
+ requires_approval = ["amount", "status"] # or True: any change, create and delete too
60
+ approvals_required = 2 # default KARNI_APPROVALS_REQUIRED (1)
61
+ approver_roles = ["finance"] # default KARNI_APPROVALS_APPROVER_ROLES ([])
62
+ approval_timeout = timedelta(hours=48) # or hours as a number; default the setting (never)
63
+ approval_on_timeout = "escalate" # or "expire" (the default)
64
+ approval_escalation_roles = ["cfo"] # default KARNI_APPROVALS_ESCALATION_ROLES
65
+ ```
66
+
67
+ The model must be a `karni.models.BaseModel` (every `save()` and `delete()`
68
+ sends `karni.signals.pre_write`; that is the hook). `karni check` fails the
69
+ build for a guarded model that is not, for a malformed policy, and for
70
+ approver roles `KARNI_ROLES` does not declare (`APR007`).
71
+
72
+ ## What happens
73
+
74
+ - **A guarded write is held.** When a signed-in user changes a guarded
75
+ field (a list) or anything at all (`True`), a `PendingChange` records the
76
+ whole change as `{field: [old, new]}` and the write is stopped before any
77
+ SQL: the API answers **202** with the pending change, the admin shows
78
+ "sent for approval" and returns to the list, plain code gets
79
+ `karni.exceptions.KarniWriteDeferred` (code `APR001`). The row is not
80
+ touched; the change is itself history (`karni.audit`).
81
+ - **Writes pass through** with no current user (migrations, seeds,
82
+ management commands), inside the module's own apply, and inside
83
+ `karni_approvals.interception.bypass(reason="...")` for trusted code.
84
+ Bulk SQL (`QuerySet.update()`) is never seen — `karni check`'s
85
+ `audited-bulk-write` rule catches it on audited models.
86
+ - **Deciding** is `karni_approvals.services.decide(pending, checker, "approve" | "reject", reason)`,
87
+ behind the API (`POST /api/pending-changes/{id}/approve/` and `reject/`)
88
+ and the admin queue. Every rule lives there, so it holds wherever a
89
+ decision comes from:
90
+ - the maker can never be the checker (`APR002`), superuser or not;
91
+ - a checker needs the `karni_approvals.pendingchange:approve` (or
92
+ `:reject`) grant and, when the model declares `approver_roles`, one of
93
+ those roles (`APR003`);
94
+ - a checker sees and decides only pending changes whose row lies inside
95
+ their own scope — a branch checker, their branch (`APR003`); a create
96
+ has no row yet and is visible to whoever may view every row of that
97
+ model;
98
+ - one decision per person (`APR004`); a decided change is final (`APR005`);
99
+ - the change is applied as the last approver once `approvals_required`
100
+ approvals are in — the row's own history then names the checker — and
101
+ refused as stale when any recorded old value no longer matches
102
+ (`APR006`, status `failed`).
103
+ - **Timeouts.** A change past `approval_timeout` is swept hourly: `expire`
104
+ marks it expired; `escalate` emails the escalation roles and extends it by
105
+ the same timeout.
106
+ - **Notifications.** On proposal the checkers are emailed from a Celery task
107
+ (`KARNI_APPROVALS_NOTIFY`, a dotted path called with the pending change;
108
+ `""` switches it off; replace it to post elsewhere).
109
+ - **Audit.** `PendingChange` and `Decision` are audited models: every
110
+ proposal, approval and rejection is a `karni.audit` entry with its actor.
111
+
112
+ ## Where it shows
113
+
114
+ | Surface | What |
115
+ | --- | --- |
116
+ | Admin → Karni approvals → Pending changes | the queue in the checker's scope, each change's diff and decisions, **Approve selected** / **Reject selected** with a reason |
117
+ | `GET /api/pending-changes/` | the queue (filters: `status`, `action`, `content_type`, `maker`), scoped; makers with an `own` grant see their own |
118
+ | `POST /api/pending-changes/{id}/approve/`, `…/reject/` | `{"reason": "..."}` → the updated pending change; 403 `APR002`/`APR003`, 400 for the rest |
119
+ | `GET /api/pending-changes/count/` | `{"pending": n}` the requester may decide — the badge |
120
+ | `@karni/approvals-ui` | `<ApprovalsBadge/>`, `<ApprovalsQueue/>`, `usePendingCount`, `usePendingChanges`, `useDecidePendingChange` |
121
+
122
+ ## Settings
123
+
124
+ Every setting arrives through the module's settings fragment
125
+ (`karni_approvals.conf.DEFAULTS`) and is overridable in the project's
126
+ `settings.py` or by its environment variable; each has a per-model override
127
+ in `class Karni`.
128
+
129
+ | Setting | Env var | Default | Meaning |
130
+ | --- | --- | --- | --- |
131
+ | `KARNI_APPROVALS_REQUIRED` | `KARNI_APPROVALS_REQUIRED` | `1` | approvals a change needs |
132
+ | `KARNI_APPROVALS_APPROVER_ROLES` | `KARNI_APPROVALS_APPROVER_ROLES` | `[]` | roles that may approve, on top of the grant (`[]` = any holder) |
133
+ | `KARNI_APPROVALS_TIMEOUT_HOURS` | `KARNI_APPROVALS_TIMEOUT_HOURS` | `0` | hours before a change times out (`0` = never) |
134
+ | `KARNI_APPROVALS_ON_TIMEOUT` | `KARNI_APPROVALS_ON_TIMEOUT` | `expire` | `expire` or `escalate` |
135
+ | `KARNI_APPROVALS_ESCALATION_ROLES` | `KARNI_APPROVALS_ESCALATION_ROLES` | `[]` | roles emailed on escalation |
136
+ | `KARNI_APPROVALS_NOTIFY` | `KARNI_APPROVALS_NOTIFY` | `karni_approvals.notify.email_checkers` | the notifier (`""` = off) |
137
+ | `KARNI_APPROVALS_FROM_EMAIL` | `KARNI_APPROVALS_FROM_EMAIL` | `""` | sender; `""` uses `DEFAULT_FROM_EMAIL` |
138
+
139
+ ## Escape hatches
140
+
141
+ - `requires_approval` per model, and every policy knob per model
142
+ (`class Karni`) or project-wide (the settings above).
143
+ - `karni_approvals.interception.bypass(reason=...)` for trusted code; no
144
+ current user (`karni.middleware.current_user(None)`) has the same effect.
145
+ - `KARNI_APPROVALS_NOTIFY` replaces the notifier; `notify.checkers_for` is
146
+ the reusable half.
147
+ - `services.propose`, `decide`, `apply`, `visible_to`, `sweep` are plain
148
+ functions: call them from your own views, commands or tasks.
149
+ - The API viewset and the admin class are ordinary Karni classes: subclass
150
+ `PendingChangeViewSet` (set `auto_register = False` on it) or register
151
+ your own admin for `PendingChange` (explicit registrations win).
152
+ - This module ships an explicit `admin.py` (the queue); `Decision` is not
153
+ registered on its own (an inline of the queue).
154
+
155
+ ## Limits
156
+
157
+ Many-to-many changes are not part of a diff (they are written after
158
+ `save()` and never pass through `pre_write`); hold them behind a guarded
159
+ field or a service of your own. A change is held whole: an update touching
160
+ a guarded and an unguarded field waits as one.
@@ -0,0 +1,133 @@
1
+ # karni-approvals
2
+
3
+ Second-person (maker-checker) approval for high-value changes in a
4
+ [Karni](https://github.com/cbsshekhawat18-lab/karni) project. A model opts
5
+ in; from then on a guarded change by anyone — through the API, the admin or
6
+ plain code — becomes a **pending change holding the diff instead of a
7
+ write**. A checker holding the approve permission applies or rejects it,
8
+ with a reason. The maker can never be the checker, in code.
9
+
10
+ ## Install
11
+
12
+ ```sh
13
+ pip install karni-approvals
14
+ karni add approvals
15
+ karni migrate
16
+ ```
17
+
18
+ `karni add approvals` adds the dependency, wires `karni_approvals` into the
19
+ `# karni:apps` block and records this module's agent guidance. No URL
20
+ include is needed: the API rides the auto-router. Keep the Celery worker
21
+ running — notifications and the hourly timeout sweep are tasks.
22
+
23
+ ## Opt a model in
24
+
25
+ ```python
26
+ from datetime import timedelta
27
+ from karni.models import BaseModel
28
+
29
+ class Invoice(BaseModel):
30
+ ...
31
+ class Karni:
32
+ requires_approval = ["amount", "status"] # or True: any change, create and delete too
33
+ approvals_required = 2 # default KARNI_APPROVALS_REQUIRED (1)
34
+ approver_roles = ["finance"] # default KARNI_APPROVALS_APPROVER_ROLES ([])
35
+ approval_timeout = timedelta(hours=48) # or hours as a number; default the setting (never)
36
+ approval_on_timeout = "escalate" # or "expire" (the default)
37
+ approval_escalation_roles = ["cfo"] # default KARNI_APPROVALS_ESCALATION_ROLES
38
+ ```
39
+
40
+ The model must be a `karni.models.BaseModel` (every `save()` and `delete()`
41
+ sends `karni.signals.pre_write`; that is the hook). `karni check` fails the
42
+ build for a guarded model that is not, for a malformed policy, and for
43
+ approver roles `KARNI_ROLES` does not declare (`APR007`).
44
+
45
+ ## What happens
46
+
47
+ - **A guarded write is held.** When a signed-in user changes a guarded
48
+ field (a list) or anything at all (`True`), a `PendingChange` records the
49
+ whole change as `{field: [old, new]}` and the write is stopped before any
50
+ SQL: the API answers **202** with the pending change, the admin shows
51
+ "sent for approval" and returns to the list, plain code gets
52
+ `karni.exceptions.KarniWriteDeferred` (code `APR001`). The row is not
53
+ touched; the change is itself history (`karni.audit`).
54
+ - **Writes pass through** with no current user (migrations, seeds,
55
+ management commands), inside the module's own apply, and inside
56
+ `karni_approvals.interception.bypass(reason="...")` for trusted code.
57
+ Bulk SQL (`QuerySet.update()`) is never seen — `karni check`'s
58
+ `audited-bulk-write` rule catches it on audited models.
59
+ - **Deciding** is `karni_approvals.services.decide(pending, checker, "approve" | "reject", reason)`,
60
+ behind the API (`POST /api/pending-changes/{id}/approve/` and `reject/`)
61
+ and the admin queue. Every rule lives there, so it holds wherever a
62
+ decision comes from:
63
+ - the maker can never be the checker (`APR002`), superuser or not;
64
+ - a checker needs the `karni_approvals.pendingchange:approve` (or
65
+ `:reject`) grant and, when the model declares `approver_roles`, one of
66
+ those roles (`APR003`);
67
+ - a checker sees and decides only pending changes whose row lies inside
68
+ their own scope — a branch checker, their branch (`APR003`); a create
69
+ has no row yet and is visible to whoever may view every row of that
70
+ model;
71
+ - one decision per person (`APR004`); a decided change is final (`APR005`);
72
+ - the change is applied as the last approver once `approvals_required`
73
+ approvals are in — the row's own history then names the checker — and
74
+ refused as stale when any recorded old value no longer matches
75
+ (`APR006`, status `failed`).
76
+ - **Timeouts.** A change past `approval_timeout` is swept hourly: `expire`
77
+ marks it expired; `escalate` emails the escalation roles and extends it by
78
+ the same timeout.
79
+ - **Notifications.** On proposal the checkers are emailed from a Celery task
80
+ (`KARNI_APPROVALS_NOTIFY`, a dotted path called with the pending change;
81
+ `""` switches it off; replace it to post elsewhere).
82
+ - **Audit.** `PendingChange` and `Decision` are audited models: every
83
+ proposal, approval and rejection is a `karni.audit` entry with its actor.
84
+
85
+ ## Where it shows
86
+
87
+ | Surface | What |
88
+ | --- | --- |
89
+ | Admin → Karni approvals → Pending changes | the queue in the checker's scope, each change's diff and decisions, **Approve selected** / **Reject selected** with a reason |
90
+ | `GET /api/pending-changes/` | the queue (filters: `status`, `action`, `content_type`, `maker`), scoped; makers with an `own` grant see their own |
91
+ | `POST /api/pending-changes/{id}/approve/`, `…/reject/` | `{"reason": "..."}` → the updated pending change; 403 `APR002`/`APR003`, 400 for the rest |
92
+ | `GET /api/pending-changes/count/` | `{"pending": n}` the requester may decide — the badge |
93
+ | `@karni/approvals-ui` | `<ApprovalsBadge/>`, `<ApprovalsQueue/>`, `usePendingCount`, `usePendingChanges`, `useDecidePendingChange` |
94
+
95
+ ## Settings
96
+
97
+ Every setting arrives through the module's settings fragment
98
+ (`karni_approvals.conf.DEFAULTS`) and is overridable in the project's
99
+ `settings.py` or by its environment variable; each has a per-model override
100
+ in `class Karni`.
101
+
102
+ | Setting | Env var | Default | Meaning |
103
+ | --- | --- | --- | --- |
104
+ | `KARNI_APPROVALS_REQUIRED` | `KARNI_APPROVALS_REQUIRED` | `1` | approvals a change needs |
105
+ | `KARNI_APPROVALS_APPROVER_ROLES` | `KARNI_APPROVALS_APPROVER_ROLES` | `[]` | roles that may approve, on top of the grant (`[]` = any holder) |
106
+ | `KARNI_APPROVALS_TIMEOUT_HOURS` | `KARNI_APPROVALS_TIMEOUT_HOURS` | `0` | hours before a change times out (`0` = never) |
107
+ | `KARNI_APPROVALS_ON_TIMEOUT` | `KARNI_APPROVALS_ON_TIMEOUT` | `expire` | `expire` or `escalate` |
108
+ | `KARNI_APPROVALS_ESCALATION_ROLES` | `KARNI_APPROVALS_ESCALATION_ROLES` | `[]` | roles emailed on escalation |
109
+ | `KARNI_APPROVALS_NOTIFY` | `KARNI_APPROVALS_NOTIFY` | `karni_approvals.notify.email_checkers` | the notifier (`""` = off) |
110
+ | `KARNI_APPROVALS_FROM_EMAIL` | `KARNI_APPROVALS_FROM_EMAIL` | `""` | sender; `""` uses `DEFAULT_FROM_EMAIL` |
111
+
112
+ ## Escape hatches
113
+
114
+ - `requires_approval` per model, and every policy knob per model
115
+ (`class Karni`) or project-wide (the settings above).
116
+ - `karni_approvals.interception.bypass(reason=...)` for trusted code; no
117
+ current user (`karni.middleware.current_user(None)`) has the same effect.
118
+ - `KARNI_APPROVALS_NOTIFY` replaces the notifier; `notify.checkers_for` is
119
+ the reusable half.
120
+ - `services.propose`, `decide`, `apply`, `visible_to`, `sweep` are plain
121
+ functions: call them from your own views, commands or tasks.
122
+ - The API viewset and the admin class are ordinary Karni classes: subclass
123
+ `PendingChangeViewSet` (set `auto_register = False` on it) or register
124
+ your own admin for `PendingChange` (explicit registrations win).
125
+ - This module ships an explicit `admin.py` (the queue); `Decision` is not
126
+ registered on its own (an inline of the queue).
127
+
128
+ ## Limits
129
+
130
+ Many-to-many changes are not part of a diff (they are written after
131
+ `save()` and never pass through `pre_write`); hold them behind a guarded
132
+ field or a service of your own. A change is held whole: an update touching
133
+ a guarded and an unguarded field waits as one.
@@ -0,0 +1,15 @@
1
+ ## Approvals (karni-approvals)
2
+ - A model opts in with `class Karni: requires_approval = ["amount", "status"]`
3
+ (or `True` for any change, create and delete included). A guarded change by
4
+ a signed-in user becomes a PendingChange instead of a write: the API answers
5
+ 202, the admin says so, code gets `KarniWriteDeferred` — never catch it to
6
+ write anyway; trusted scripts use `karni_approvals.interception.bypass(reason=...)`.
7
+ - Decisions only through `karni_approvals.services.decide(pending, checker, verdict, reason)`
8
+ (the API's `/api/pending-changes/{id}/approve/` + `reject/`, the admin queue):
9
+ the maker can never be the checker, checkers need the
10
+ `karni_approvals.pendingchange:approve` grant (+ `approver_roles`), and see
11
+ only rows in their scope. Never write PendingChange/Decision rows by hand.
12
+ - Per model: `approvals_required`, `approver_roles`, `approval_timeout`,
13
+ `approval_on_timeout` ("expire"|"escalate"); project defaults are the
14
+ `KARNI_APPROVALS_*` settings. Notifications and the hourly sweep run in Celery.
15
+ - No URL include needed: the API rides the auto-router; `count/` is the badge.
@@ -0,0 +1,11 @@
1
+ """karni-approvals: maker-checker approvals for Karni projects.
2
+
3
+ A model opts in with ``class Karni: requires_approval = [...]`` (or ``True``).
4
+ From then on a change to those fields by anyone — through the API, the admin
5
+ or plain code — becomes a :class:`~karni_approvals.models.PendingChange`
6
+ holding the diff instead of a write; a second person holding the approve
7
+ permission applies or rejects it, with a reason. The maker can never be the
8
+ checker, in code. See the README.
9
+ """
10
+
11
+ from __future__ import annotations