Strengthening basket and order lifecycle guards

Author: savarinCreated Apr 6, 2026Updated Apr 6, 2026

Description

Three cases where django-oscar's basket and order lifecycle could catch invalid state transitions early, preventing silent data corruption in production. Reproduction of all three issues in full here.

1. Line.delete() missing editability guard

Line.save() already guards against modifying non-editable baskets (abstract_models.py:762-767), but Line.delete() has no such guard. A line can be deleted from a frozen or submitted basket:

python
from oscar.apps.basket.models import Basket

basket = Basket.objects.create()
# ... add a line, then freeze
basket.freeze()

line = basket.lines.first()
line.save()    # PermissionDenied ✓ (existing guard)
line.delete()  # Silently succeeds ✗ (no guard)

Fix: 5 lines mirroring the existing save() guard — same exception, same message pattern. Exact symmetry with existing code.

2. OrderCreator.place_order() missing tax verification

checkout/views.py:574 already asserts basket.is_tax_known before placing an order:

python
assert basket.is_tax_known, "Basket tax must be set before a user can place an order"

But assert is stripped by python -O in production, and this check only exists in the web checkout view — not in OrderCreator.place_order(), which is also called programmatically. A basket with unknown tax can produce an order with incorrect totals.

python
from oscar.apps.order.utils import OrderCreator

# Programmatic order creation bypasses the web view's assert
creator = OrderCreator()
creator.place_order(basket=basket_with_unknown_tax, ...)  # No error

Fix: 4 lines — if not basket.is_tax_known: raise ValueError(...) in place_order(), right after the existing basket.is_empty check. Makes the existing assert production-safe and catches non-web callers.

3. freeze() / thaw() / submit() missing status preconditions

freeze(), thaw(), and submit() accept any basket regardless of current status. This enables the scenario described in #3768: if handle_successful_order throws after basket.submit(), error recovery can call thaw() on a SUBMITTED basket, silently reopening it — enabling duplicate orders.

python
basket = Basket.objects.create()
basket.freeze()
basket.submit()

# Error recovery path — should fail, but silently reopens the basket
basket.thaw()   # Silently succeeds ✗
basket.submit()  # Duplicate order possible

Fix: 15 lines — each method checks current status before transitioning:

  • freeze() requires OPEN
  • thaw() requires FROZEN
  • submit() requires OPEN or FROZEN (backwards-compatible with programmatic usage that skips the freeze step)

thaw() on a SUBMITTED basket now raises PermissionDenied instead of silently corrupting state. This directly addresses #3768.

Context

I found these gaps by scoring each assumption in oscar's basket and order code against how strongly it's enforced — unguarded, convention, runtime check, or type-level guarantee. Writing out the basket state machine and order creation flow as explicit decision trees made the missing guards visible. The invariant analysis methodology is documented at github.com/savarin/lean-agent.

I have working implementations with tests for all three and would like to open two focused PRs:

  • PR #1 (small, trust-building): Line.delete() guard + OrderCreator tax check
  • PR #2 (higher impact): Basket status transition preconditions, addressing #3768

All existing tests pass with zero regressions.

Source: django-oscar/django-oscar