mirror of
https://github.com/pretix/pretix.git
synced 2026-05-04 15:04:03 +00:00
UI for creating and chacnging vouchers
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.9 on 2016-02-09 09:59
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('pretixbase', '0002_auto_20160209_0940'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='eventpermission',
|
||||
name='can_change_vouchers',
|
||||
field=models.BooleanField(default=True, verbose_name='Can change vouchers'),
|
||||
),
|
||||
]
|
||||
31
src/pretix/base/migrations/0004_auto_20160209_1023.py
Normal file
31
src/pretix/base/migrations/0004_auto_20160209_1023.py
Normal file
@@ -0,0 +1,31 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.9 on 2016-02-09 10:23
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('pretixbase', '0003_eventpermission_can_change_vouchers'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='voucher',
|
||||
name='item',
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='voucher',
|
||||
name='item',
|
||||
field=models.ForeignKey(default=None, help_text="This product is added to the user's cart if the voucher is redeemed.", on_delete=django.db.models.deletion.CASCADE, related_name='vouchers', to='pretixbase.Item', verbose_name='Product'),
|
||||
preserve_default=False,
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='voucher',
|
||||
name='price',
|
||||
field=models.DecimalField(blank=True, decimal_places=2, help_text='If empty, the product will cost its normal price.', max_digits=10, null=True, verbose_name='Set product price to'),
|
||||
),
|
||||
]
|
||||
@@ -237,6 +237,10 @@ class EventPermission(models.Model):
|
||||
default=True,
|
||||
verbose_name=_("Can change orders")
|
||||
)
|
||||
can_change_vouchers = models.BooleanField(
|
||||
default=True,
|
||||
verbose_name=_("Can change vouchers")
|
||||
)
|
||||
|
||||
class Meta:
|
||||
verbose_name = _("Event permission")
|
||||
|
||||
@@ -1,12 +1,23 @@
|
||||
import random
|
||||
|
||||
from django.db import models
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from .base import LoggedModel
|
||||
from .event import Event
|
||||
from .items import Item
|
||||
from .orders import CartPosition, OrderPosition
|
||||
|
||||
|
||||
class Voucher(models.Model):
|
||||
def generate_code():
|
||||
charset = list('ABCDEFGHKLMNPQRSTUVWXYZ23456789')
|
||||
while True:
|
||||
code = "".join([random.choice(charset) for i in range(16)])
|
||||
if not Voucher.objects.filter(code=code).exists():
|
||||
return code
|
||||
|
||||
|
||||
class Voucher(LoggedModel):
|
||||
event = models.ForeignKey(
|
||||
Event,
|
||||
on_delete=models.CASCADE,
|
||||
@@ -15,7 +26,7 @@ class Voucher(models.Model):
|
||||
)
|
||||
code = models.CharField(
|
||||
verbose_name=_("Voucher code"),
|
||||
max_length=255
|
||||
max_length=255, default=generate_code
|
||||
)
|
||||
valid_until = models.DateTimeField(
|
||||
blank=True, null=True,
|
||||
@@ -38,9 +49,10 @@ class Voucher(models.Model):
|
||||
)
|
||||
price = models.DecimalField(
|
||||
verbose_name=_("Set product price to"),
|
||||
decimal_places=2, max_digits=10, null=True, blank=True
|
||||
decimal_places=2, max_digits=10, null=True, blank=True,
|
||||
help_text=_('If empty, the product will cost its normal price.')
|
||||
)
|
||||
item = models.ManyToManyField(
|
||||
item = models.ForeignKey(
|
||||
Item, related_name='vouchers',
|
||||
verbose_name=_("Product"),
|
||||
help_text=_(
|
||||
@@ -53,16 +65,19 @@ class Voucher(models.Model):
|
||||
verbose_name_plural = _("Vouchers")
|
||||
unique_together = (("event", "code"),)
|
||||
|
||||
def __str__(self):
|
||||
return self.code
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
self.code = self.code.upper()
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def is_ordered(self) -> int:
|
||||
return OrderPosition.objects.current.filter(
|
||||
voucher=self.voucher
|
||||
return OrderPosition.objects.filter(
|
||||
voucher=self
|
||||
).exists()
|
||||
|
||||
def is_in_cart(self) -> int:
|
||||
return CartPosition.objects.current.filter(
|
||||
voucher=self.voucher
|
||||
return CartPosition.objects.filter(
|
||||
voucher=self
|
||||
).count()
|
||||
|
||||
Reference in New Issue
Block a user