Skip to content

Commit

Permalink
feat(tickets,nekocon2023): allow overriding max amount per product in…
Browse files Browse the repository at this point in the history
… ticket sales
  • Loading branch information
japsu committed Apr 21, 2023
1 parent 7847ee8 commit 5621287
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
5 changes: 5 additions & 0 deletions events/nekocon2023/management/commands/setup_nekocon2023.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,15 @@ def setup_tickets(self):
defaults.update(
ticket_sales_starts=t - timedelta(days=60),
ticket_sales_ends=t + timedelta(days=60),
max_count_per_product=5,
)

meta, unused = TicketsEventMeta.objects.get_or_create(event=self.event, defaults=defaults)

if meta.max_count_per_product == 99:
meta.max_count_per_product = 5
meta.save()

def limit_group(description, limit):
limit_group, unused = LimitGroup.objects.get_or_create(
event=self.event,
Expand Down
20 changes: 14 additions & 6 deletions tickets/forms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django import forms
from django.core import validators
from django.utils.translation import gettext_lazy as _

from crispy_forms.helper import FormHelper, Layout
Expand Down Expand Up @@ -62,15 +63,21 @@ class Meta:


class OrderProductForm(forms.ModelForm):
count = forms.IntegerField(label="Määrä", min_value=0, max_value=99)
count = forms.IntegerField(label="Määrä", min_value=0)

def __init__(self, *args, **kwargs):
max_count_per_product = kwargs.pop("max_count_per_product", 99)

super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.label_class = "sr-only"
self.helper.field_class = "col-md-12"
self.helper.form_tag = False

count_field = self.fields["count"]
count_field.validators.append(validators.MaxValueValidator(max_count_per_product))
count_field.widget.attrs["max"] = count_field.max_value = max_count_per_product

@classmethod
def get_for_order(cls, request, order, admin=False):
product_criteria = dict(event=order.event)
Expand All @@ -87,16 +94,17 @@ def get_for_order(cls, request, order, admin=False):
def get_for_order_and_product(cls, request, order, product, admin=False):
order_product, unused = OrderProduct.objects.get_or_create(order=order, product=product)

readonly = admin and (
(order.batch is not None and product.requires_shipping) or (order.is_paid and product.electronic_ticket)
)

return initialize_form(
OrderProductForm,
request,
instance=order_product,
prefix="o%d" % order_product.pk,
# XXX disallow changing amounts of electronic tickets for now
readonly=admin
and (
(order.batch is not None and product.requires_shipping) or (order.is_paid and product.electronic_ticket)
),
readonly=readonly,
max_count_per_product=order.event.tickets_event_meta.max_count_per_product,
)

class Meta:
Expand Down
17 changes: 17 additions & 0 deletions tickets/migrations/0035_ticketseventmeta_max_count_per_product.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2 on 2023-04-21 11:32

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("tickets", "0034_ticketseventmeta_accommodation_access_group"),
]

operations = [
migrations.AddField(
model_name="ticketseventmeta",
name="max_count_per_product",
field=models.SmallIntegerField(default=99),
),
]
2 changes: 2 additions & 0 deletions tickets/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ class TicketsEventMeta(ContactEmailMixin, EventMetaBase, LocalizedModel):
help_text=_("If set, customers will be required to indicate acceptance to finish order."),
)

max_count_per_product = models.SmallIntegerField(default=99)

def __str__(self):
return self.event.name

Expand Down

0 comments on commit 5621287

Please sign in to comment.