mirror of
https://github.com/pretix/pretix.git
synced 2026-05-03 14:54:04 +00:00
Added voucher redemption
This commit is contained in:
27
src/pretix/base/migrations/0005_auto_20160211_1459.py
Normal file
27
src/pretix/base/migrations/0005_auto_20160211_1459.py
Normal file
@@ -0,0 +1,27 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.9 on 2016-02-11 14:59
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
import pretix.base.models.vouchers
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('pretixbase', '0004_auto_20160209_1023'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='voucher',
|
||||
name='redeemed',
|
||||
field=models.BooleanField(default=False, verbose_name='Redeemed'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='voucher',
|
||||
name='code',
|
||||
field=models.CharField(default=pretix.base.models.vouchers.generate_code, max_length=255, verbose_name='Voucher code'),
|
||||
),
|
||||
]
|
||||
@@ -475,12 +475,24 @@ class Quota(LoggedModel):
|
||||
if size_left <= 0:
|
||||
return Quota.AVAILABILITY_ORDERED, 0
|
||||
|
||||
size_left -= self.count_blocking_vouchers()
|
||||
if size_left <= 0:
|
||||
return Quota.AVAILABILITY_ORDERED, 0
|
||||
|
||||
size_left -= self.count_in_cart()
|
||||
if size_left <= 0:
|
||||
return Quota.AVAILABILITY_RESERVED, 0
|
||||
|
||||
return Quota.AVAILABILITY_OK, size_left
|
||||
|
||||
def count_blocking_vouchers(self) -> int:
|
||||
from pretix.base.models import Voucher
|
||||
return Voucher.objects.filter(
|
||||
item__quotas__in=[self],
|
||||
block_quota=True,
|
||||
redeemed=False
|
||||
).count()
|
||||
|
||||
def count_in_cart(self) -> int:
|
||||
from pretix.base.models import CartPosition
|
||||
|
||||
|
||||
@@ -28,6 +28,10 @@ class Voucher(LoggedModel):
|
||||
verbose_name=_("Voucher code"),
|
||||
max_length=255, default=generate_code
|
||||
)
|
||||
redeemed = models.BooleanField(
|
||||
verbose_name=_("Redeemed"),
|
||||
default=False
|
||||
)
|
||||
valid_until = models.DateTimeField(
|
||||
blank=True, null=True,
|
||||
verbose_name=_("Valid until")
|
||||
@@ -71,6 +75,11 @@ class Voucher(LoggedModel):
|
||||
def save(self, *args, **kwargs):
|
||||
self.code = self.code.upper()
|
||||
super().save(*args, **kwargs)
|
||||
self.event.get_cache().set('vouchers_exist', True)
|
||||
|
||||
def delete(self, using=None, keep_parents=False):
|
||||
super().delete(using, keep_parents)
|
||||
self.event.get_cache().delete('vouchers_exist')
|
||||
|
||||
def is_ordered(self) -> int:
|
||||
return OrderPosition.objects.filter(
|
||||
|
||||
@@ -8,7 +8,7 @@ from django.utils.translation import ugettext_lazy as _
|
||||
from typing import List, Optional, Tuple
|
||||
|
||||
from pretix.base.models import (
|
||||
CartPosition, Event, EventLock, Item, ItemVariation, Quota,
|
||||
CartPosition, Event, EventLock, Item, ItemVariation, Quota, Voucher,
|
||||
)
|
||||
|
||||
|
||||
@@ -27,7 +27,10 @@ error_messages = {
|
||||
'the quantity you selected. Please see below for details.'),
|
||||
'max_items': _("You cannot select more than %s items per order"),
|
||||
'not_started': _('The presale period for this event has not yet started.'),
|
||||
'ended': _('The presale period has ended.')
|
||||
'ended': _('The presale period has ended.'),
|
||||
'voucher_invalid': _('This voucher code is not known in our database.'),
|
||||
'voucher_redeemed': _('This voucher code has already been used an can only be used once.'),
|
||||
'voucher_expired': _('This voucher is expired'),
|
||||
}
|
||||
|
||||
|
||||
@@ -98,7 +101,7 @@ def _add_new_items(event: Event, items: List[Tuple[int, Optional[int], int]],
|
||||
err = err or error_messages['unavailable']
|
||||
continue
|
||||
|
||||
# Assume that all quotas allow us to buy i[2] instances of the object
|
||||
# Check that all quotas allow us to buy i[2] instances of the object
|
||||
quota_ok = i[2]
|
||||
for quota in quotas:
|
||||
avail = quota.availability()
|
||||
@@ -131,7 +134,35 @@ def _add_new_items(event: Event, items: List[Tuple[int, Optional[int], int]],
|
||||
return err
|
||||
|
||||
|
||||
def _add_items_to_cart(event: Event, items: List[Tuple[int, Optional[int], int]], cart_id: str=None) -> None:
|
||||
def _add_voucher(event: Event, voucher: str, expiry: datetime, cart_id: str):
|
||||
try:
|
||||
v = Voucher.objects.get(code=voucher, event=event)
|
||||
if v.redeemed:
|
||||
raise CartError(error_messages['voucher_redeemed'])
|
||||
if v.valid_until is not None and v.valid_until < now():
|
||||
raise CartError(error_messages['voucher_expired'])
|
||||
|
||||
quotas = list(v.item.quotas.all())
|
||||
if len(quotas) == 0 or not v.item.is_available():
|
||||
raise CartError(error_messages['unavailable'])
|
||||
|
||||
if not v.allow_ignore_quota and not v.block_quota:
|
||||
for quota in quotas:
|
||||
avail = quota.availability()
|
||||
if avail[1] is not None and avail[1] < 1:
|
||||
raise CartError(error_messages['unavailable'])
|
||||
|
||||
CartPosition.objects.create(
|
||||
event=event, item=v.item, variation=None,
|
||||
price=v.price if v.price is not None else v.item.default_price,
|
||||
expires=expiry, cart_id=cart_id, voucher=v
|
||||
)
|
||||
except Voucher.DoesNotExist:
|
||||
raise CartError(error_messages['voucher_invalid'])
|
||||
|
||||
|
||||
def _add_items_to_cart(event: Event, items: List[Tuple[int, Optional[int], int]], cart_id: str=None,
|
||||
voucher: str=None) -> None:
|
||||
with event.lock():
|
||||
_check_date(event)
|
||||
existing = CartPosition.objects.filter(Q(cart_id=cart_id) & Q(event=event)).count()
|
||||
@@ -143,31 +174,36 @@ def _add_items_to_cart(event: Event, items: List[Tuple[int, Optional[int], int]]
|
||||
_extend_existing(event, cart_id, expiry)
|
||||
|
||||
expired = _re_add_expired_positions(items, event, cart_id)
|
||||
if not items:
|
||||
if items:
|
||||
err = _add_new_items(event, items, cart_id, expiry)
|
||||
_delete_expired(expired)
|
||||
if err:
|
||||
raise CartError(err)
|
||||
elif not voucher:
|
||||
raise CartError(error_messages['empty'])
|
||||
|
||||
err = _add_new_items(event, items, cart_id, expiry)
|
||||
_delete_expired(expired)
|
||||
if err:
|
||||
raise CartError(err)
|
||||
if voucher:
|
||||
_add_voucher(event, voucher, expiry, cart_id)
|
||||
|
||||
|
||||
def add_items_to_cart(event: int, items: List[Tuple[int, Optional[int], int]], cart_id: str=None) -> None:
|
||||
def add_items_to_cart(event: int, items: List[Tuple[int, Optional[int], int]], cart_id: str=None,
|
||||
voucher: str=None) -> None:
|
||||
"""
|
||||
Adds a list of items to a user's cart.
|
||||
:param event: The event ID in question
|
||||
:param items: A list of tuple of the form (item id, variation id or None, number)
|
||||
:param session: Session ID of a guest
|
||||
:param coupon: A coupon that should also be reeemed
|
||||
:raises CartError: On any error that occured
|
||||
"""
|
||||
event = Event.objects.get(id=event)
|
||||
try:
|
||||
_add_items_to_cart(event, items, cart_id)
|
||||
_add_items_to_cart(event, items, cart_id, voucher)
|
||||
except EventLock.LockTimeoutException:
|
||||
raise CartError(error_messages['busy'])
|
||||
|
||||
|
||||
def _remove_items_from_cart(event: int, items: List[Tuple[int, Optional[int], int]], cart_id: int) -> None:
|
||||
def _remove_items_from_cart(event: int, items: List[Tuple[int, Optional[int], int]], cart_id: str) -> None:
|
||||
with event.lock():
|
||||
for item, variation, cnt in items:
|
||||
cw = Q(cart_id=cart_id) & Q(item_id=item) & Q(event=event)
|
||||
@@ -179,7 +215,7 @@ def _remove_items_from_cart(event: int, items: List[Tuple[int, Optional[int], in
|
||||
cp.delete()
|
||||
|
||||
|
||||
def remove_items_from_cart(event: int, items: List[Tuple[int, Optional[int], int]], cart_id: int=None) -> None:
|
||||
def remove_items_from_cart(event: int, items: List[Tuple[int, Optional[int], int]], cart_id: str=None) -> None:
|
||||
"""
|
||||
Removes a list of items from a user's cart.
|
||||
:param event: The event ID in question
|
||||
@@ -197,10 +233,11 @@ if settings.HAS_CELERY:
|
||||
from pretix.celery import app
|
||||
|
||||
@app.task(bind=True, max_retries=5, default_retry_delay=2)
|
||||
def add_items_to_cart_task(self, event: int, items: List[Tuple[int, Optional[int], int]], cart_id: str):
|
||||
def add_items_to_cart_task(self, event: int, items: List[Tuple[int, Optional[int], int]], cart_id: str,
|
||||
voucher: str=None):
|
||||
event = Event.objects.get(id=event)
|
||||
try:
|
||||
_add_items_to_cart(event, items, cart_id)
|
||||
_add_items_to_cart(event, items, cart_id, voucher)
|
||||
except EventLock.LockTimeoutException:
|
||||
self.retry(exc=CartError(error_messages['busy']))
|
||||
|
||||
|
||||
@@ -28,6 +28,8 @@ error_messages = {
|
||||
'internal': _("An internal error occured, please try again."),
|
||||
'busy': _('We were not able to process your request completely as the '
|
||||
'server was too busy. Please try again.'),
|
||||
'voucher_redeemed': _('A voucher you tried to use already has been used.'),
|
||||
'voucher_expired': _('A voucher you tried to use has expired.'),
|
||||
}
|
||||
|
||||
|
||||
@@ -133,29 +135,51 @@ def _check_positions(event: Event, dt: datetime, positions: List[CartPosition]):
|
||||
cp.delete()
|
||||
continue
|
||||
quotas = list(cp.item.quotas.all()) if cp.variation is None else list(cp.variation.quotas.all())
|
||||
|
||||
if cp.voucher:
|
||||
if cp.voucher.redeemed:
|
||||
err = err or error_messages['voucher_redeemed']
|
||||
continue
|
||||
cp.voucher.redeemed = True
|
||||
cp.voucher.save()
|
||||
|
||||
if cp.expires >= dt:
|
||||
# Other checks are not necessary
|
||||
continue
|
||||
|
||||
price = cp.item.default_price if cp.variation is None else (
|
||||
cp.variation.default_price if cp.variation.default_price is not None else cp.item.default_price)
|
||||
|
||||
if cp.voucher:
|
||||
if cp.voucher.valid_until < now():
|
||||
err = err or error_messages['voucher_expired']
|
||||
continue
|
||||
if price is not False and cp.voucher.price is not None:
|
||||
price = cp.voucher.price
|
||||
|
||||
if price is False or len(quotas) == 0:
|
||||
err = err or error_messages['unavailable']
|
||||
cp.delete()
|
||||
continue
|
||||
|
||||
if price != cp.price:
|
||||
positions[i] = cp
|
||||
cp.price = price
|
||||
cp.save()
|
||||
err = err or error_messages['price_changed']
|
||||
continue
|
||||
|
||||
quota_ok = True
|
||||
for quota in quotas:
|
||||
avail = quota.availability()
|
||||
if avail[0] != Quota.AVAILABILITY_OK:
|
||||
# This quota is sold out/currently unavailable, so do not sell this at all
|
||||
err = err or error_messages['unavailable']
|
||||
quota_ok = False
|
||||
break
|
||||
|
||||
if not cp.voucher or not (cp.voucher.allow_ignore_quota or cp.voucher.block_quota):
|
||||
for quota in quotas:
|
||||
avail = quota.availability()
|
||||
if avail[0] != Quota.AVAILABILITY_OK:
|
||||
# This quota is sold out/currently unavailable, so do not sell this at all
|
||||
err = err or error_messages['unavailable']
|
||||
quota_ok = False
|
||||
break
|
||||
|
||||
if quota_ok:
|
||||
positions[i] = cp
|
||||
cp.expires = now() + timedelta(
|
||||
@@ -167,7 +191,6 @@ def _check_positions(event: Event, dt: datetime, positions: List[CartPosition]):
|
||||
raise OrderError(err)
|
||||
|
||||
|
||||
@transaction.atomic()
|
||||
def _create_order(event: Event, email: str, positions: List[CartPosition], dt: datetime,
|
||||
payment_provider: BasePaymentProvider, locale: str=None):
|
||||
total = sum([c.price for c in positions])
|
||||
@@ -211,9 +234,10 @@ def _perform_order(event: str, payment_provider: str, position_ids: List[str],
|
||||
id__in=position_ids).select_related('item', 'variation'))
|
||||
if len(position_ids) != len(positions):
|
||||
raise OrderError(error_messages['internal'])
|
||||
_check_positions(event, dt, positions)
|
||||
order = _create_order(event, email, positions, dt, pprov,
|
||||
locale=locale)
|
||||
with transaction.atomic():
|
||||
_check_positions(event, dt, positions)
|
||||
order = _create_order(event, email, positions, dt, pprov,
|
||||
locale=locale)
|
||||
|
||||
mail(
|
||||
order.email, _('Your order: %(code)s') % {'code': order.code},
|
||||
|
||||
Reference in New Issue
Block a user