-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add global ticket capacity for conferences #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8bb446a
feat: add global ticket capacity for conferences
JacobCoffee 2e97278
test: cover uncovered lines to restore 100% coverage
JacobCoffee 77c8a6f
fix: close race condition in global capacity checks and remove extra …
JacobCoffee 5b8e6c1
fix: prevent oversells from deleted ticket types and stale capacity c…
JacobCoffee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/django_program/conference/migrations/0004_add_total_capacity.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # Generated by Django 5.2.11 on 2026-02-14 03:10 | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("program_conference", "0003_conference_address"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name="conference", | ||
| name="total_capacity", | ||
| field=models.PositiveIntegerField( | ||
| default=0, help_text="Maximum total tickets across all types. 0 means unlimited." | ||
| ), | ||
| ), | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| """Global ticket capacity enforcement for conferences. | ||
|
|
||
| Provides functions to count, check, and validate total ticket sales against | ||
| a conference-level capacity limit. Add-ons are excluded from the global count | ||
| because they do not consume venue seats. | ||
| """ | ||
|
|
||
| from django.core.exceptions import ValidationError | ||
| from django.db import models | ||
| from django.utils import timezone | ||
|
|
||
| from django_program.conference.models import Conference | ||
| from django_program.registration.models import Order, OrderLineItem | ||
|
|
||
|
|
||
| def get_global_sold_count(conference: object) -> int: | ||
| """Return the total number of tickets sold across all ticket types. | ||
|
|
||
| Counts OrderLineItem quantities for ticket-type items (not add-ons) in | ||
| orders that are PAID, PARTIALLY_REFUNDED, or PENDING with an active | ||
| inventory hold. | ||
|
|
||
| Uses ``addon__isnull=True`` rather than ``ticket_type__isnull=False`` so | ||
| that line items whose ticket type was deleted (SET_NULL) are still counted | ||
| toward the sold total, preventing oversells. | ||
|
|
||
| Args: | ||
| conference: The conference to count sales for. | ||
|
|
||
| Returns: | ||
| The total number of tickets sold. | ||
| """ | ||
| now = timezone.now() | ||
| return ( | ||
| OrderLineItem.objects.filter( | ||
| order__conference=conference, | ||
| addon__isnull=True, | ||
| ) | ||
| .filter( | ||
| models.Q(order__status__in=[Order.Status.PAID, Order.Status.PARTIALLY_REFUNDED]) | ||
| | models.Q(order__status=Order.Status.PENDING, order__hold_expires_at__gt=now), | ||
| ) | ||
| .aggregate(total=models.Sum("quantity"))["total"] | ||
| or 0 | ||
| ) | ||
|
|
||
|
|
||
| def get_global_remaining(conference: object) -> int | None: | ||
| """Return the number of tickets still available under the global cap. | ||
|
|
||
| Args: | ||
| conference: The conference to check capacity for. | ||
|
|
||
| Returns: | ||
| The remaining ticket count, or ``None`` if the conference has no | ||
| global capacity limit (``total_capacity == 0``). | ||
| """ | ||
| if conference.total_capacity == 0: | ||
| return None | ||
| sold = get_global_sold_count(conference) | ||
| return conference.total_capacity - sold | ||
|
|
||
|
|
||
| def validate_global_capacity(conference: object, desired_total: int) -> None: | ||
| """Raise ``ValidationError`` if ``desired_total`` would exceed global capacity. | ||
|
|
||
| Acquires a row-level lock on the conference via ``select_for_update()`` to | ||
| prevent race conditions when multiple concurrent requests validate capacity | ||
| at the same time. The caller **must** already be inside a | ||
| ``transaction.atomic`` block. | ||
|
|
||
| The early-return check for unlimited conferences happens **after** the lock | ||
| is acquired so that a stale in-memory instance cannot bypass enforcement. | ||
|
|
||
| Args: | ||
| conference: The conference to validate against. | ||
| desired_total: The total number of ticket items in the cart | ||
| (across all ticket types, excluding add-ons). | ||
|
|
||
| Raises: | ||
| ValidationError: If the desired total exceeds the conference's | ||
| ``total_capacity``. | ||
| """ | ||
| locked = Conference.objects.select_for_update().get(pk=conference.pk) | ||
| if locked.total_capacity == 0: | ||
| return | ||
| sold = get_global_sold_count(locked) | ||
| remaining = locked.total_capacity - sold | ||
| if desired_total > remaining: | ||
| if remaining <= 0: | ||
| raise ValidationError(f"This conference is sold out (venue capacity: {locked.total_capacity}).") | ||
| raise ValidationError( | ||
| f"Only {remaining} tickets remaining for this conference (venue capacity: {locked.total_capacity})." | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.