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

0 downloads

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.

danq9 signup: the person chooses the password and name on the confirm page 83c7aa6 2 weeks, 5 days ago History
check_promotion.py · 4.6 KB · 109 lines
1"""action_check_promotion -- does this code work on this basket, and if
2not, why not (all of the whys).
3
4POST {code, subtotal_cents?, shipping_cents?, customer_email?, today?}
5
6The basket page's "apply code" button. It QUOTES and it reserves nothing:
7no redemption row is written, no counter moves, and two shoppers may both
8be told a one-use code will work. That is deliberate and it is the same
9posture action_checkout takes about stock and app-pickup takes about
10collection slots -- the authoritative decision is made once, at checkout,
11against the real basket, by the same fold this object calls. A quote that
12wrote a redemption would burn a code every time somebody typed one to see
13what it did.
14
15**The numbers come from the CALLER and are therefore advisory.** This is
16a public object serving guest checkouts, so it has no session and no
17basket; a shopper who claims a $500 subtotal can talk this object into
18saying a $100-minimum code applies. Nothing is lost by that: checkout
19resolves the same code against the basket it can actually read, and
20refuses with the same sentences. Stating it plainly here beats somebody
21later "hardening" a quote into a gate it was never asked to be.
22
23**Every reason at once**, from object_promotions.blockers -- expired,
24not started, below minimum, exhausted, already used by this customer,
25inactive, unstackable. A page that reveals one problem per attempt is a
26page people stop typing into.
27
28Public on the OBJECT, never on the collection. What comes back is bounded
29to what a shop would print on the offer itself: whether it works, what it
30takes off, and why not. There is no way to list codes from here, which is
31what stops this becoming an enumeration oracle for every unpublished
32campaign the shop is holding back.
33"""
34
35import os
36from datetime import date
37
38import object_promotions
39import object_records
40
41ACTOR = "action_check_promotion"
42
43
44def _base_dir():
45 return os.environ.get("DBBASIC_DATA_DIR", "data")
46
47
48def _text(value):
49 return str(value if value is not None else "").strip()
50
51
52def _int(value, default=0):
53 try:
54 return int(_text(value) or default)
55 except (TypeError, ValueError):
56 return default
57
58
59def POST(request):
60 base = _base_dir()
61 code = object_promotions.normalize_code(request.get("code"))
62 if not code:
63 return {"status": 400, "error": "A promotion code is required."}
64
65 try:
66 promotions = object_records.read_collection_records("promotions",
67 base_dir=base)
68 except Exception:
69 # No promotions app on this box. An honest "that code does not
70 # work here" beats a 500, and beats pretending it did.
71 return {"status": 404, "code": code,
72 "error": f"There is no promotion with the code '{code}'.",
73 "problems": [f"There is no promotion with the code '{code}'."]}
74
75 try:
76 redemptions = object_records.read_collection_records(
77 "promotion_redemptions", base_dir=base)
78 except Exception:
79 # Unreadable redemption log means the limits cannot be counted, and
80 # a limit that cannot be counted has not been met -- refusing is the
81 # safe direction, because a code handed out once too often is money
82 # and a code refused once is a support email.
83 return {"status": 409, "code": code,
84 "error": ("The redemption log cannot be read, so this code "
85 "cannot be checked right now."),
86 "problems": ["The redemption log cannot be read, so this code "
87 "cannot be checked right now."]}
88
89 today = _text(request.get("today")) or date.today().isoformat()
90 subtotal = max(0, _int(request.get("subtotal_cents")))
91 shipping = max(0, _int(request.get("shipping_cents")))
92 email = _text(request.get("customer_email"))
93
94 promo = object_promotions.resolve(code, promotions, on_date=today)
95 problems = object_promotions.blockers(
96 promo, subtotal, email, redemptions, on_date=today, code=code)
97 if problems:
98 return {"status": 409, "code": code,
99 "error": " ".join(problems), "problems": problems,
100 "applies": False}
101
102 discount = object_promotions.discount_for(subtotal, shipping, promo)
103 return {"ok": True, "applies": True, "code": code,
104 "kind": _text(promo.get("kind")),
105 "applies_to": object_promotions.applies_to(promo),
106 "discount_cents": discount,
107 "note": ("a quote, not a reservation -- the code is applied for "
108 "real at checkout, against the basket the server can "
109 "see")}