mirror of
https://github.com/pretix/pretix.git
synced 2026-05-04 15:04:03 +00:00
Add waiting list
This commit is contained in:
@@ -13,7 +13,7 @@ from django.utils.timezone import now
|
||||
|
||||
from pretix.base.models import (
|
||||
CachedFile, CartPosition, Event, Item, ItemCategory, ItemVariation, Order,
|
||||
OrderPosition, Organizer, Question, Quota, User, Voucher,
|
||||
OrderPosition, Organizer, Question, Quota, User, Voucher, WaitingListEntry,
|
||||
)
|
||||
from pretix.base.services.orders import (
|
||||
OrderError, cancel_order, mark_order_paid, perform_order,
|
||||
@@ -316,6 +316,104 @@ class QuotaTestCase(BaseQuotaTestCase):
|
||||
self.assertEqual(self.quota.count_in_cart(), 1)
|
||||
self.assertEqual(self.item1.check_quotas(), (Quota.AVAILABILITY_OK, 1))
|
||||
|
||||
def test_waitinglist_item_active(self):
|
||||
self.quota.items.add(self.item1)
|
||||
self.quota.size = 1
|
||||
self.quota.save()
|
||||
WaitingListEntry.objects.create(
|
||||
event=self.event, item=self.item1, email='foo@bar.com'
|
||||
)
|
||||
self.assertEqual(self.item1.check_quotas(), (Quota.AVAILABILITY_RESERVED, 0))
|
||||
self.assertEqual(self.item1.check_quotas(count_waitinglist=False), (Quota.AVAILABILITY_OK, 1))
|
||||
|
||||
def test_waitinglist_variation_active(self):
|
||||
self.quota.variations.add(self.var1)
|
||||
self.quota.size = 1
|
||||
self.quota.save()
|
||||
WaitingListEntry.objects.create(
|
||||
event=self.event, item=self.item2, variation=self.var1, email='foo@bar.com'
|
||||
)
|
||||
self.assertEqual(self.var1.check_quotas(), (Quota.AVAILABILITY_RESERVED, 0))
|
||||
self.assertEqual(self.var1.check_quotas(count_waitinglist=False), (Quota.AVAILABILITY_OK, 1))
|
||||
|
||||
def test_waitinglist_variation_fulfilled(self):
|
||||
self.quota.variations.add(self.var1)
|
||||
self.quota.size = 1
|
||||
self.quota.save()
|
||||
v = Voucher.objects.create(quota=self.quota, event=self.event, block_quota=True, redeemed=1)
|
||||
WaitingListEntry.objects.create(
|
||||
event=self.event, item=self.item2, variation=self.var1, email='foo@bar.com', voucher=v
|
||||
)
|
||||
self.assertEqual(self.var1.check_quotas(), (Quota.AVAILABILITY_OK, 1))
|
||||
self.assertEqual(self.var1.check_quotas(count_waitinglist=False), (Quota.AVAILABILITY_OK, 1))
|
||||
|
||||
def test_waitinglist_variation_other(self):
|
||||
self.quota.variations.add(self.var1)
|
||||
self.quota.size = 1
|
||||
self.quota.save()
|
||||
WaitingListEntry.objects.create(
|
||||
event=self.event, item=self.item2, variation=self.var2, email='foo@bar.com'
|
||||
)
|
||||
self.assertEqual(self.var1.check_quotas(), (Quota.AVAILABILITY_OK, 1))
|
||||
self.assertEqual(self.var1.check_quotas(count_waitinglist=False), (Quota.AVAILABILITY_OK, 1))
|
||||
|
||||
def test_quota_cache(self):
|
||||
self.quota.variations.add(self.var1)
|
||||
self.quota.size = 1
|
||||
self.quota.save()
|
||||
WaitingListEntry.objects.create(
|
||||
event=self.event, item=self.item2, variation=self.var1, email='foo@bar.com'
|
||||
)
|
||||
|
||||
cache = {}
|
||||
|
||||
self.assertEqual(self.var1.check_quotas(_cache=cache), (Quota.AVAILABILITY_RESERVED, 0))
|
||||
|
||||
with self.assertNumQueries(1):
|
||||
self.assertEqual(self.var1.check_quotas(_cache=cache), (Quota.AVAILABILITY_RESERVED, 0))
|
||||
|
||||
# Do not reuse cache for count_waitinglist=False
|
||||
self.assertEqual(self.var1.check_quotas(_cache=cache, count_waitinglist=False), (Quota.AVAILABILITY_OK, 1))
|
||||
|
||||
with self.assertNumQueries(1):
|
||||
self.assertEqual(self.var1.check_quotas(_cache=cache, count_waitinglist=False), (Quota.AVAILABILITY_OK, 1))
|
||||
|
||||
|
||||
class WaitingListTestCase(BaseQuotaTestCase):
|
||||
|
||||
def test_duplicate(self):
|
||||
w1 = WaitingListEntry.objects.create(
|
||||
event=self.event, item=self.item2, variation=self.var1, email='foo@bar.com'
|
||||
)
|
||||
w1.clean()
|
||||
w2 = WaitingListEntry(
|
||||
event=self.event, item=self.item2, variation=self.var1, email='foo@bar.com'
|
||||
)
|
||||
with self.assertRaises(ValidationError):
|
||||
w2.clean()
|
||||
|
||||
def test_duplicate_of_successful(self):
|
||||
v = Voucher.objects.create(quota=self.quota, event=self.event, block_quota=True, redeemed=1)
|
||||
w1 = WaitingListEntry.objects.create(
|
||||
event=self.event, item=self.item2, variation=self.var1, email='foo@bar.com',
|
||||
voucher=v
|
||||
)
|
||||
w1.clean()
|
||||
w2 = WaitingListEntry(
|
||||
event=self.event, item=self.item2, variation=self.var1, email='foo@bar.com'
|
||||
)
|
||||
w2.clean()
|
||||
|
||||
def test_missing_variation(self):
|
||||
w2 = WaitingListEntry(
|
||||
event=self.event, item=self.item2, email='foo@bar.com'
|
||||
)
|
||||
with self.assertRaises(ValidationError):
|
||||
w2.clean()
|
||||
|
||||
|
||||
class VoucherTestCase(BaseQuotaTestCase):
|
||||
|
||||
def test_voucher_reuse(self):
|
||||
self.quota.items.add(self.item1)
|
||||
v = Voucher.objects.create(quota=self.quota, event=self.event, valid_until=now() + timedelta(days=5))
|
||||
@@ -396,9 +494,6 @@ class QuotaTestCase(BaseQuotaTestCase):
|
||||
v = Voucher(variation=self.var1, event=self.event)
|
||||
v.clean()
|
||||
|
||||
|
||||
class VoucherTestCase(BaseQuotaTestCase):
|
||||
|
||||
def test_calculate_price_none(self):
|
||||
v = Voucher.objects.create(event=self.event, price_mode='none', value=Decimal('10.00'))
|
||||
v.calculate_price(Decimal('23.42')) == Decimal('23.42')
|
||||
|
||||
132
src/tests/base/test_waitinglist.py
Normal file
132
src/tests/base/test_waitinglist.py
Normal file
@@ -0,0 +1,132 @@
|
||||
from django.core import mail as djmail
|
||||
from django.test import TestCase
|
||||
from django.utils.timezone import now
|
||||
|
||||
from pretix.base.models import (
|
||||
Event, Item, ItemVariation, Organizer, Quota, Voucher, WaitingListEntry,
|
||||
)
|
||||
from pretix.base.models.waitinglist import WaitingListException
|
||||
from pretix.base.services.waitinglist import (
|
||||
assign_automatically, process_waitinglist,
|
||||
)
|
||||
|
||||
|
||||
class WaitingListTestCase(TestCase):
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
o = Organizer.objects.create(name='Dummy', slug='dummy')
|
||||
cls.event = Event.objects.create(
|
||||
organizer=o, name='Dummy', slug='dummy',
|
||||
date_from=now(),
|
||||
)
|
||||
|
||||
def setUp(self):
|
||||
djmail.outbox = []
|
||||
self.quota = Quota.objects.create(name="Test", size=2, event=self.event)
|
||||
self.item1 = Item.objects.create(event=self.event, name="Ticket", default_price=23,
|
||||
admission=True)
|
||||
self.item2 = Item.objects.create(event=self.event, name="T-Shirt", default_price=23)
|
||||
self.item3 = Item.objects.create(event=self.event, name="Goodie", default_price=23)
|
||||
self.var1 = ItemVariation.objects.create(item=self.item2, value='S')
|
||||
self.var2 = ItemVariation.objects.create(item=self.item2, value='M')
|
||||
self.var3 = ItemVariation.objects.create(item=self.item3, value='Fancy')
|
||||
|
||||
def test_send_unavailable(self):
|
||||
self.quota.items.add(self.item1)
|
||||
self.quota.size = 0
|
||||
self.quota.save()
|
||||
wle = WaitingListEntry.objects.create(
|
||||
event=self.event, item=self.item1, email='foo@bar.com'
|
||||
)
|
||||
with self.assertRaises(WaitingListException):
|
||||
wle.send_voucher()
|
||||
|
||||
def test_send_double(self):
|
||||
self.quota.variations.add(self.var1)
|
||||
self.quota.size = 1
|
||||
self.quota.save()
|
||||
v = Voucher.objects.create(quota=self.quota, event=self.event, block_quota=True, redeemed=1)
|
||||
wle = WaitingListEntry.objects.create(
|
||||
event=self.event, item=self.item2, variation=self.var1, email='foo@bar.com', voucher=v
|
||||
)
|
||||
with self.assertRaises(WaitingListException):
|
||||
wle.send_voucher()
|
||||
|
||||
def test_send_variation(self):
|
||||
wle = WaitingListEntry.objects.create(
|
||||
event=self.event, item=self.item2, variation=self.var1, email='foo@bar.com'
|
||||
)
|
||||
wle.send_voucher()
|
||||
wle.refresh_from_db()
|
||||
|
||||
assert wle.voucher
|
||||
assert wle.voucher.item == wle.item
|
||||
assert wle.voucher.variation == wle.variation
|
||||
assert wle.email in wle.voucher.comment
|
||||
assert wle.voucher.block_quota
|
||||
assert wle.voucher.max_usages == 1
|
||||
assert wle.voucher.event == self.event
|
||||
|
||||
assert len(djmail.outbox) == 1
|
||||
assert djmail.outbox[0].to == [wle.email]
|
||||
|
||||
def test_send_custom_validity(self):
|
||||
self.event.settings.set('waiting_list_hours', 24)
|
||||
wle = WaitingListEntry.objects.create(
|
||||
event=self.event, item=self.item2, variation=self.var1, email='foo@bar.com'
|
||||
)
|
||||
wle.send_voucher()
|
||||
wle.refresh_from_db()
|
||||
|
||||
assert 3600 * 23 < (wle.voucher.valid_until - now()).seconds < 3600 * 24
|
||||
|
||||
def test_send_auto(self):
|
||||
self.quota.variations.add(self.var1)
|
||||
self.quota.size = 7
|
||||
self.quota.save()
|
||||
for i in range(10):
|
||||
WaitingListEntry.objects.create(
|
||||
event=self.event, item=self.item2, variation=self.var1, email='foo{}@bar.com'.format(i)
|
||||
)
|
||||
WaitingListEntry.objects.create(
|
||||
event=self.event, item=self.item1, email='bar{}@bar.com'.format(i)
|
||||
)
|
||||
|
||||
assign_automatically.apply(args=(self.event.pk,))
|
||||
assert WaitingListEntry.objects.filter(voucher__isnull=True).count() == 3
|
||||
assert Voucher.objects.count() == 17
|
||||
assert sorted(list(WaitingListEntry.objects.filter(voucher__isnull=True).values_list('email', flat=True))) == [
|
||||
'foo7@bar.com', 'foo8@bar.com', 'foo9@bar.com'
|
||||
]
|
||||
|
||||
def test_send_periodic(self):
|
||||
self.event.settings.set('waiting_list_enabled', True)
|
||||
self.event.settings.set('waiting_list_auto', True)
|
||||
for i in range(5):
|
||||
WaitingListEntry.objects.create(
|
||||
event=self.event, item=self.item2, variation=self.var1, email='foo{}@bar.com'.format(i)
|
||||
)
|
||||
process_waitinglist(None)
|
||||
assert Voucher.objects.count() == 5
|
||||
|
||||
def test_send_periodic_disabled(self):
|
||||
self.event.settings.set('waiting_list_enabled', True)
|
||||
self.event.settings.set('waiting_list_auto', False)
|
||||
for i in range(5):
|
||||
WaitingListEntry.objects.create(
|
||||
event=self.event, item=self.item2, variation=self.var1, email='foo{}@bar.com'.format(i)
|
||||
)
|
||||
process_waitinglist(None)
|
||||
assert WaitingListEntry.objects.filter(voucher__isnull=True).count() == 5
|
||||
assert Voucher.objects.count() == 0
|
||||
|
||||
def test_send_periodic_disabled2(self):
|
||||
self.event.settings.set('waiting_list_enabled', False)
|
||||
self.event.settings.set('waiting_list_auto', True)
|
||||
for i in range(5):
|
||||
WaitingListEntry.objects.create(
|
||||
event=self.event, item=self.item2, variation=self.var1, email='foo{}@bar.com'.format(i)
|
||||
)
|
||||
process_waitinglist(None)
|
||||
assert WaitingListEntry.objects.filter(voucher__isnull=True).count() == 5
|
||||
assert Voucher.objects.count() == 0
|
||||
Reference in New Issue
Block a user