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 a batch of unique voucher codes for a conference. |
Classes
Configuration for a bulk voucher generation request. |
- class django_program.registration.services.voucher_service.VoucherBulkConfig[source]¶
Bases:
objectConfiguration 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.VoucherTypevalues.
- 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.
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¶
- __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)¶
- django_program.registration.services.voucher_service.generate_voucher_codes(config)[source]¶
Generate a batch of unique voucher codes for a conference.
Creates
config.countvouchers with cryptographically random codes, all sharing the same configuration (type, discount, validity window, etc.). The vouchers are inserted in a singlebulk_createcall wrapped in a transaction for atomicity. M2M relations are set via a singlebulk_createon 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:
- Returns:
List of newly created
Voucherinstances.- Raises:
ValueError – If
config.countis 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).