django_program.registration.services.voucher_service

Voucher bulk generation service.

Provides functions for generating batches of unique, cryptographically random voucher codes within a single database transaction.

Functions

generate_voucher_codes(config)

Generate a batch of unique voucher codes for a conference.

Classes

VoucherBulkConfig

Configuration for a bulk voucher generation request.

class django_program.registration.services.voucher_service.VoucherBulkConfig[source]

Bases: object

Configuration for a bulk voucher generation request.

Bundles all parameters needed to generate a batch of voucher codes into a single value object.

conference

The conference to create vouchers for.

prefix

Fixed string prepended to each generated code.

count

Number of voucher codes to generate (1-500).

voucher_type

One of the Voucher.VoucherType values.

discount_value

Percentage (0-100) or fixed amount depending on type.

max_uses

Maximum number of times each voucher can be redeemed.

valid_from

Optional start of the validity window.

valid_until

Optional end of the validity window.

unlocks_hidden_tickets

Whether the vouchers reveal hidden ticket types.

applicable_ticket_types

Optional queryset of ticket types to restrict to.

applicable_addons

Optional queryset of add-ons to restrict to.

Parameters:
conference: Conference
prefix: str
count: int
voucher_type: str
discount_value: Decimal
max_uses: int = 1
valid_from: datetime | None = None
valid_until: datetime | None = None
unlocks_hidden_tickets: bool = False
applicable_ticket_types: QuerySet | None = None
applicable_addons: QuerySet | None = None
__init__(conference, prefix, count, voucher_type, discount_value, max_uses=1, valid_from=None, valid_until=None, unlocks_hidden_tickets=False, applicable_ticket_types=None, applicable_addons=None)
Parameters:
django_program.registration.services.voucher_service.generate_voucher_codes(config)[source]

Generate a batch of unique voucher codes for a conference.

Creates config.count vouchers with cryptographically random codes, all sharing the same configuration (type, discount, validity window, etc.). The vouchers are inserted in a single bulk_create call wrapped in a transaction for atomicity. M2M relations are set via a single bulk_create on the through tables to avoid N+1 queries.

Parameters:

config (VoucherBulkConfig) – Bulk generation configuration specifying the conference, prefix, count, discount parameters, and optional constraints.

Return type:

list[Voucher]

Returns:

List of newly created Voucher instances.

Raises:
  • ValueError – If config.count is less than 1 or greater than 500.

  • RuntimeError – If unique code generation fails after retries.

  • IntegrityError – If a code collision occurs at the database level despite the in-memory uniqueness check (race condition safeguard).