|
| 1 | +""" |
| 2 | +Parse Products from PretixData for further joins and analysis in other places. |
| 3 | +""" |
| 4 | + |
| 5 | +from decimal import Decimal |
| 6 | +from typing import ClassVar, Iterable |
| 7 | + |
| 8 | +import polars as pl |
| 9 | +from core.models import PretixData |
| 10 | +from pydantic import BaseModel, model_validator |
| 11 | + |
| 12 | + |
| 13 | +class LocalisedFieldsMixin: |
| 14 | + # Marking as ClassVar here is important. It doens't work without it :) |
| 15 | + _localised_fields: ClassVar[Iterable[str]] = () |
| 16 | + |
| 17 | + @model_validator(mode="before") |
| 18 | + @classmethod |
| 19 | + def extract(cls, values): |
| 20 | + for field in cls._localised_fields: |
| 21 | + if isinstance(values[field], dict) and "en" in values[field]: |
| 22 | + values[field] = values[field]["en"] |
| 23 | + continue |
| 24 | + |
| 25 | + return values |
| 26 | + |
| 27 | + |
| 28 | +class ProductVariation(LocalisedFieldsMixin, BaseModel): |
| 29 | + id: int |
| 30 | + value: str |
| 31 | + description: str | dict |
| 32 | + price: Decimal |
| 33 | + |
| 34 | + _localised_fields = ["value", "description"] |
| 35 | + |
| 36 | + |
| 37 | +class Product(LocalisedFieldsMixin, BaseModel): |
| 38 | + id: int |
| 39 | + name: str |
| 40 | + description: str | dict |
| 41 | + variations: list[ProductVariation] |
| 42 | + default_price: Decimal | None = None |
| 43 | + |
| 44 | + _localised_fields = ["name", "description"] |
| 45 | + |
| 46 | + |
| 47 | +class FlatProductDescription(BaseModel): |
| 48 | + product_id: int |
| 49 | + variation_id: int | None |
| 50 | + product_name: str |
| 51 | + type: str |
| 52 | + variant: str |
| 53 | + price: Decimal | None |
| 54 | + |
| 55 | + |
| 56 | +def get_latest_products_data() -> PretixData: |
| 57 | + qs = PretixData.objects.filter(resource=PretixData.PretixResources.products) |
| 58 | + return qs.latest("created_at") |
| 59 | + |
| 60 | + |
| 61 | +def parse_latest_products_to_objects(pretix_data: PretixData) -> list[Product]: |
| 62 | + data = pretix_data.content |
| 63 | + products = [Product.model_validate(entry) for entry in data] |
| 64 | + return products |
| 65 | + |
| 66 | + |
| 67 | +def flat_product_data(products: list[Product]) -> pl.DataFrame: |
| 68 | + """ |
| 69 | + Returns a polars data frame with flat description of available Products. |
| 70 | + Products hold nested `ProductVariation`s; flatten them so every variation |
| 71 | + (or base product) becomes one row in a DataFrame. |
| 72 | + """ |
| 73 | + rows = [] |
| 74 | + for p in products: |
| 75 | + if p.variations: |
| 76 | + for v in p.variations: |
| 77 | + if "Late" in v.value: |
| 78 | + type_ = "Late" |
| 79 | + name = v.value.replace("Late", "").strip() |
| 80 | + else: |
| 81 | + type_ = "Regular" |
| 82 | + name = v.value |
| 83 | + |
| 84 | + if "Combined" in name: |
| 85 | + name = "Combined" |
| 86 | + |
| 87 | + rows.append( |
| 88 | + FlatProductDescription( |
| 89 | + product_id=p.id, |
| 90 | + variation_id=v.id, |
| 91 | + product_name=p.name, |
| 92 | + type=type_, |
| 93 | + variant=name, |
| 94 | + price=v.price, |
| 95 | + ) |
| 96 | + ) |
| 97 | + else: |
| 98 | + rows.append( |
| 99 | + FlatProductDescription( |
| 100 | + product_id=p.id, |
| 101 | + variation_id=None, |
| 102 | + product_name=p.name, |
| 103 | + variant=p.name, |
| 104 | + type="Late" if "Late" in p.name else "Regular", |
| 105 | + price=p.default_price, |
| 106 | + ) |
| 107 | + ) |
| 108 | + |
| 109 | + return pl.DataFrame(rows) |
| 110 | + |
| 111 | + |
| 112 | +def latest_flat_product_data() -> pl.DataFrame: |
| 113 | + """ |
| 114 | + Thin wrapper on getting latest information from the database, and |
| 115 | + converting into a polars data frame |
| 116 | + """ |
| 117 | + pretix_data = get_latest_products_data() |
| 118 | + products = parse_latest_products_to_objects(pretix_data) |
| 119 | + return flat_product_data(products) |
0 commit comments