This commit is contained in:
Kara Engelhardt
2026-04-07 19:26:14 +02:00
parent 2e7d54174d
commit 66882eb115
10 changed files with 318 additions and 27 deletions

View File

@@ -6,18 +6,22 @@ from django.core.exceptions import ValidationError
from i18nfield.strings import LazyI18nString
from .models import WalletLayout
class WalletPlatform:
identifier: str
name: str
class ApplePlatform(WalletPlatform):
identifier = "apple"
name = _("Apple")
class GooglePlatform(WalletPlatform):
identifier = "google"
name = _("Google")
class PlaceholderFieldType(enum.Enum):
TEXT = "text"
CODE = "qr"
@@ -30,10 +34,11 @@ class PlaceholderFieldType(enum.Enum):
class PlaceholderField:
type: PlaceholderFieldType
label: LazyI18nString
value: LazyI18nString
value: str
def asdict(self):
return {'type': self.type.value, 'label': self.label, 'value': self.value}
return {"type": self.type.value, "label": self.label.data, "value": self.value}
@dataclass
class FieldGroupDefinition:
@@ -44,7 +49,13 @@ class FieldGroupDefinition:
max_entries: int | None = None
def asdict(self):
return {"identifier": self.identifier, "name": self.name, "min_entries": self.min_entries, "max_entries": self.max_entries}
return {
"identifier": self.identifier,
"name": self.name,
"entry_type": self.entry_type.value,
"min_entries": self.min_entries,
"max_entries": self.max_entries,
}
@dataclass
@@ -54,7 +65,7 @@ class PlaceholderFieldGroup(FieldGroupDefinition):
def asdict(self):
asdict = super().asdict()
asdict['default_entries'] = [x.asdict() for x in self.default_entries]
asdict["default_entries"] = [x.asdict() for x in self.default_entries]
return asdict
@@ -67,7 +78,7 @@ class PredefinedFieldGroup(FieldGroupDefinition):
class PassStyle:
identifier: str # unique within platform
name: str
name: str
platform: Literal["apple"] | Literal["google"]
fields: list[FieldGroupDefinition]
# preview_image: str # TODO: preview
@@ -94,18 +105,28 @@ class AppleWalletEventTicket(PassStyle):
min_entries=1,
max_entries=1,
default_entries=[
PlaceholderField(PlaceholderFieldType.IMAGE, "logo", "event:image")
PlaceholderField(PlaceholderFieldType.IMAGE, LazyI18nString("logo"), "event:image")
],
entry_type=PlaceholderFieldType.IMAGE,
),
PlaceholderFieldGroup(identifier="primary", name=_("Primary"), min_entries=1, max_entries=1),
PlaceholderFieldGroup(
identifier="primary",
name=_("Primary"),
min_entries=1,
max_entries=1,
default_entries=[
PlaceholderField(PlaceholderFieldType.TEXT, LazyI18nString("Ticket type"), "item")
],
),
PlaceholderFieldGroup(
identifier="secondary", name=_("Secondary"), max_entries=4
), # TODO: validation of max field count if combined "Coupons, store cards, and generic passes with a square barcode can have a total of up to four secondary and auxiliary fields, combined."
PlaceholderFieldGroup(
identifier="headers", name=_("Header"), max_entries=3
), # TODO: header image
PlaceholderFieldGroup(identifier="auxillary", name=_("Auxillary"), max_entries=4),
PlaceholderFieldGroup(
identifier="auxillary", name=_("Auxillary"), max_entries=4
),
PlaceholderFieldGroup(identifier="back", name=_("Back")),
]
# preview_image = "apple/event_ticket.svg"
@@ -117,13 +138,16 @@ class GoogleWalletEventTicket(PassStyle):
platform = "google"
fields = [
PredefinedFieldGroup(identifier="seating", name=_("Seating")),
PlaceholderFieldGroup(identifier="qrcode", name=_("QR-Code"), entry_type=PlaceholderFieldType.CODE),
PlaceholderFieldGroup(
identifier="qrcode", name=_("QR-Code"), entry_type=PlaceholderFieldType.CODE
),
]
AVAILABLE_PLATFORMS = {"apple": ApplePlatform, "google": GooglePlatform}
AVAILABLE_STYLES = [AppleWalletEventTicket(), GoogleWalletEventTicket()]
def get_platforms_with_styles():
platforms_with_styles = {}
for style in AVAILABLE_STYLES:
@@ -133,6 +157,7 @@ def get_platforms_with_styles():
platforms_with_styles[platform][style.identifier] = style
return platforms_with_styles
def get_platform_styles(platform):
platform_styles = {}
for style in AVAILABLE_STYLES:
@@ -140,9 +165,11 @@ def get_platform_styles(platform):
platform_styles[style.identifier] = style
return platform_styles
def get_platforms():
return AVAILABLE_PLATFORMS
class PassLayout:
style: PassStyle
layout: dict
@@ -156,15 +183,19 @@ class PassLayout:
def validate_fields(self):
style_fields = self.style.fields
if 'fields' not in self.layout:
if "fields" not in self.layout:
raise ValidationError(_("Layout did not contain any fields"))
layout_fields = self.layout['fields']
layout_fields = self.layout["fields"]
if not isinstance(layout_fields, dict):
raise ValidationError(_("'fields' must be dict"))
for fieldgroup in style_fields:
layout_field_data = layout_fields.get(fieldgroup.identifier, [])
if fieldgroup.min_entries and fieldgroup.min_entries < len(layout_field_data):
raise ValidationError(_("At least {min_entries} must be specified for {name}").format(min_entries=fieldgroup.min_entries, name=fieldgroup.name))
layout_field_data = layout_fields.get(fieldgroup.identifier, {})
if fieldgroup.min_entries and fieldgroup.min_entries < len(
layout_field_data.get('entries')
):
raise ValidationError(
_("At least {min_entries} must be specified for {name}").format(
min_entries=fieldgroup.min_entries, name=fieldgroup.name
)
)