Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

9 downloads

Install on DigitalOcean Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

Daniel Quellhorst Changelog for this batch. 65ef78d 3 hours, 49 minutes ago History
line_freeze.py · 2.8 KB · 73 lines
1"""system_line_freeze -- an order line whose bill is issued or paid is not
2rewritten in place.
3
4The locked check `settled_line` on order_lines (lock `ledger`, the lock
5payments, refunds and invoice lines take). The rule and its reasons live in
6object_corrections; this object reads the order, the invoices billing it and
7the payments against them, and asks. See object_corrections' docstring for
8what Corner Cup found (a paid soup edited into a salad, an unpaid line
9deleted while the invoice kept billing it).
10
11Fails closed: invoices or payments that are installed and cannot be read
12answer 503 ("try again"), never "nothing has been paid". Not installed is a
13fact: an order nobody invoices on this box is not frozen by an invoice.
14"""
15
16import os
17
18import object_collections
19import object_corrections
20import object_records
21
22
23def _base_dir():
24 return os.environ.get("DBBASIC_DATA_DIR", "data")
25
26
27def _rows(collection, base):
28 try:
29 return object_records.read_collection_records(collection, base_dir=base)
30 except object_collections.CollectionNotFoundError:
31 return []
32
33
34def _order(order_id, base):
35 if not order_id:
36 return None
37 try:
38 return object_records.get_collection_record("orders", order_id, base_dir=base)
39 except (object_records.RecordNotFoundError, object_records.InvalidRecordIdError,
40 object_collections.CollectionNotFoundError):
41 return None
42
43
44def LOCKED_CHECK(request):
45 action = request.get("action")
46 record = request.get("record") or {}
47 existing = request.get("existing")
48 base = request.get("base_dir") or _base_dir()
49 text = object_corrections.text
50 order_ids = {text(record.get("order_id"))}
51 if existing:
52 order_ids.add(text(existing.get("order_id")))
53 try:
54 invoices = _rows("invoices", base)
55 payments = _rows("payments", base)
56 worst = None
57 for order_id in sorted(i for i in order_ids if i):
58 bills = object_corrections.bills_for_order(_order(order_id, base), invoices)
59 received = object_corrections.received_cents(
60 [b.get("id") for b in bills], payments)
61 refusal = object_corrections.line_refusal(
62 kind="order", action=action, existing=existing, record=record,
63 issued_bill=any(object_corrections.issued(b) for b in bills),
64 received=received)
65 worst = worst or refusal
66 except Exception as exc: # noqa: BLE001 -- unknown is not "unpaid"
67 return {"status": 503,
68 "error": (f"Whether this order has been billed or paid cannot be read "
69 f"right now ({str(exc)[:120]}), so the line was not changed. "
70 f"Try again in a moment.")}
71 if worst:
72 return {"status": 409, "error": worst}
73 return None