Skip to content

Commit

Permalink
Make ProductGroup types a ChoiceField in the form.
Browse files Browse the repository at this point in the history
It's still just a free text thing in the database.

Fixes #1322.
  • Loading branch information
lukegb authored and russss committed Jan 23, 2024
1 parent f0b02a8 commit 543c13f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
4 changes: 2 additions & 2 deletions apps/admin/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
)
from wtforms.fields.html5 import DateField

from models.product import ProductGroup
from models.product import ProductGroup, PRODUCT_GROUP_TYPES
from models.basket import Basket

from ..common import CURRENCY_SYMBOLS
Expand Down Expand Up @@ -75,7 +75,7 @@ class ProductGroupReparentForm(Form):

class ProductGroupForm(Form):
name = StringField("Name")
type = StringField("Type")
type = SelectField("Type", choices=[(t.slug, t.name) for t in PRODUCT_GROUP_TYPES])
capacity_max = IntegerField("Maximum to sell (Optional)", [Optional()])
expires = DateField("Expiry Date (Optional)", [Optional()])

Expand Down
18 changes: 10 additions & 8 deletions models/basket.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from main import db
from . import Currency
from .exc import CapacityException
from .product import PriceTier, Voucher
from .purchase import Purchase, Ticket, AdmissionTicket
from .product import PriceTier, Voucher, PRODUCT_GROUP_TYPES_DICT
from .purchase import Purchase


class Line:
Expand Down Expand Up @@ -201,12 +201,14 @@ def create_purchases(self):
line.tier.issue_instances(issue_count)

product = line.tier.parent
if product.parent.type == "admissions":
purchase_cls = AdmissionTicket
elif product.parent.type in {"campervan", "parking"}:
purchase_cls = Ticket
else:
purchase_cls = Purchase
product_group_type = PRODUCT_GROUP_TYPES_DICT.get(
product.parent.type
)
purchase_cls = (
product_group_type.purchase_cls
if product_group_type
else Purchase
)

price = line.tier.get_price(self.currency)
purchases = [
Expand Down
20 changes: 19 additions & 1 deletion models/product.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import annotations
from dataclasses import dataclass
from decimal import Decimal
from collections import defaultdict
from datetime import datetime, timedelta
Expand All @@ -15,7 +16,7 @@
from main import db
from .mixins import CapacityMixin, InheritedAttributesMixin
from . import BaseModel
from .purchase import Purchase
from .purchase import Purchase, AdmissionTicket, Ticket
from .payment import Currency

if TYPE_CHECKING:
Expand Down Expand Up @@ -66,6 +67,23 @@ def one_or_none(result):
raise MultipleLoadedResultsFound()


@dataclass(frozen=True)
class ProductGroupType:
slug: str
name: str
purchase_cls: type[Purchase]


PRODUCT_GROUP_TYPES = [
ProductGroupType("admissions", "Admission Ticket", AdmissionTicket),
ProductGroupType("campervan", "Campervan Ticket", Ticket),
ProductGroupType("parking", "Parking", Ticket),
ProductGroupType("merchandise", "Merchandise", Purchase),
ProductGroupType("rental", "Rental", Purchase),
]
PRODUCT_GROUP_TYPES_DICT = {t.slug: t for t in PRODUCT_GROUP_TYPES}


class ProductGroup(BaseModel, CapacityMixin, InheritedAttributesMixin):
"""Represents a logical group of products.
Expand Down

0 comments on commit 543c13f

Please sign in to comment.