askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
fringe_benefits.py · 1.1 KB · 27 lines
| 1 | """hook_fringe_benefits -- the one rule the register's fields cannot declare. |
| 2 | |
| 3 | A benefit's period must not end before it starts. Two fields compared |
| 4 | with each other is beyond `validation` (a bound is a constant), so it is a |
| 5 | locked check on the record's own fields (docs/package-authoring.md, "What |
| 6 | can't be declared"): no `reads`, one flock round trip, and it holds for |
| 7 | every writer -- an import of last year's register as much as the form. |
| 8 | Dates are YYYY-MM-DD strings, which the schema has already validated, and |
| 9 | compare in date order. |
| 10 | """ |
| 11 | |
| 12 | |
| 13 | def LOCKED_CHECK(request): |
| 14 | record = request.get("record") or {} |
| 15 | start = str(record.get("period_start") or "").strip() |
| 16 | end = str(record.get("period_end") or "").strip() |
| 17 | if start and end and end < start: |
| 18 | return {"error": f"The period ends ({end}) before it starts ({start}).", "status": 409} |
| 19 | return None |
| 20 | |
| 21 | |
| 22 | def BEFORE_WRITE(request): |
| 23 | """The same answer early, for the API: the check decides it again inside |
| 24 | the lock for every writer. No position on a delete.""" |
| 25 | if request.get("action") not in ("create", "update"): |
| 26 | return None |
| 27 | return LOCKED_CHECK(request) |