django_program.registration.services.cart

Cart management service for conference registration.

Handles cart lifecycle, item management, voucher application, and pricing summary computation. All functions are stateless and operate on Cart model instances directly.

Functions

add_addon(cart, addon[, qty])

Add an add-on to the cart or increase its quantity.

add_ticket(cart, ticket_type[, qty])

Add a ticket to the cart or increase its quantity.

apply_voucher(cart, code)

Apply a voucher code to the cart.

get_or_create_cart(user, conference)

Return the user's open cart, creating one if none exists.

get_summary(cart)

Compute a full pricing summary of the cart.

get_summary_from_items(cart, items)

Compute pricing summary using a pre-fetched cart-item snapshot.

remove_item(cart, item_id)

Remove an item from the cart, cascading add-on removals if needed.

update_quantity(cart, item_id, qty)

Update the quantity of a cart item.

Classes

CartSummary

Full pricing summary of a cart including voucher discounts.

LineItemSummary

Pricing breakdown for a single cart item.

class django_program.registration.services.cart.LineItemSummary[source]

Bases: object

Pricing breakdown for a single cart item.

Parameters:
item_id: int
description: str
quantity: int
unit_price: Decimal
discount: Decimal
line_total: Decimal
__init__(item_id, description, quantity, unit_price, discount, line_total)
Parameters:
class django_program.registration.services.cart.CartSummary[source]

Bases: object

Full pricing summary of a cart including voucher discounts.

Parameters:
items: list[LineItemSummary]
subtotal: Decimal
discount: Decimal
total: Decimal
__init__(items, subtotal, discount, total)
Parameters:
django_program.registration.services.cart.get_or_create_cart(user, conference)[source]

Return the user’s open cart, creating one if none exists.

Expires any stale open carts for this user and conference before looking up or creating a fresh cart.

Parameters:
  • user (object) – The authenticated user (AUTH_USER_MODEL instance).

  • conference (object) – The conference to create the cart for.

Return type:

Cart

Returns:

An open Cart instance with a valid expiry time.

django_program.registration.services.cart.add_ticket(cart, ticket_type, qty=1)[source]

Add a ticket to the cart or increase its quantity.

Validates availability, stock limits, per-user limits, and voucher requirements before modifying the cart.

Parameters:
  • cart (Cart) – The open cart to add the ticket to.

  • ticket_type (TicketType) – The ticket type to add.

  • qty (int) – Number of tickets to add (must be >= 1).

Return type:

CartItem

Returns:

The created or updated CartItem.

Raises:

ValidationError – If the ticket cannot be added due to business rule violations (unavailable, out of stock, limit exceeded, or voucher required).

django_program.registration.services.cart.add_addon(cart, addon, qty=1)[source]

Add an add-on to the cart or increase its quantity.

Validates availability, stock, and ticket-type prerequisites before modifying the cart.

Parameters:
  • cart (Cart) – The open cart to add the add-on to.

  • addon (AddOn) – The add-on to add.

  • qty (int) – Number of add-ons to add (must be >= 1).

Return type:

CartItem

Returns:

The created or updated CartItem.

Raises:

ValidationError – If the add-on cannot be added due to business rule violations (inactive, out of window, prerequisite ticket missing, or out of stock).

django_program.registration.services.cart.remove_item(cart, item_id)[source]

Remove an item from the cart, cascading add-on removals if needed.

When removing a ticket type, any add-ons that require that ticket type (and no other qualifying ticket type remains in the cart) are also removed.

Parameters:
  • cart (Cart) – The cart to remove the item from.

  • item_id (int) – The primary key of the CartItem to remove.

Raises:

ValidationError – If the item does not exist or does not belong to this cart.

Return type:

None

django_program.registration.services.cart.update_quantity(cart, item_id, qty)[source]

Update the quantity of a cart item.

If the new quantity is zero or negative the item is removed instead. Re-validates stock and per-user limits for the new quantity.

Parameters:
  • cart (Cart) – The cart containing the item.

  • item_id (int) – The primary key of the CartItem to update.

  • qty (int) – The new absolute quantity.

Return type:

CartItem | None

Returns:

The updated CartItem, or None if the item was removed.

Raises:

ValidationError – If the new quantity violates stock or per-user limits, or if the item does not belong to this cart.

django_program.registration.services.cart.apply_voucher(cart, code)[source]

Apply a voucher code to the cart.

Parameters:
  • cart (Cart) – The cart to apply the voucher to.

  • code (str) – The voucher code string.

Return type:

Voucher

Returns:

The validated Voucher instance now attached to the cart.

Raises:

ValidationError – If the voucher code is not found, not valid, or does not belong to this cart’s conference.

django_program.registration.services.cart.get_summary(cart)[source]

Compute a full pricing summary of the cart.

Iterates all cart items, applies any voucher discounts, and returns a structured summary with per-item and aggregate totals.

Parameters:

cart (Cart) – The cart to summarise.

Return type:

CartSummary

Returns:

A CartSummary with line items, subtotal, discount, and total.

django_program.registration.services.cart.get_summary_from_items(cart, items)[source]

Compute pricing summary using a pre-fetched cart-item snapshot.

Parameters:
Return type:

CartSummary