Coverage for polar/benefit/strategies/downloadables/schemas.py: 78%
31 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 12:42 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 12:42 +0000
1from typing import Annotated, Any, Literal
3from annotated_types import Len
4from pydantic import UUID4, model_validator
6from polar.kit.schemas import Schema
7from polar.models.benefit import BenefitType
9from ..base.schemas import (
10 BenefitBase,
11 BenefitCreateBase,
12 BenefitSubscriberBase,
13 BenefitUpdateBase,
14)
17class BenefitDownloadablesCreateProperties(Schema):
18 archived: dict[UUID4, bool] = {}
19 files: Annotated[list[UUID4], Len(min_length=1)]
22class BenefitDownloadablesProperties(Schema):
23 archived: dict[UUID4, bool]
24 files: list[UUID4]
27def get_active_file_ids(properties: BenefitDownloadablesProperties) -> list[UUID4]:
28 active = []
29 archived_files = properties.archived
30 for file_id in properties.files:
31 archived = archived_files.get(file_id, False)
32 if not archived: 32 ↛ 30line 32 didn't jump to line 30 because the condition on line 32 was always true
33 active.append(file_id)
35 return active
38class BenefitDownloadablesSubscriberProperties(Schema):
39 active_files: list[UUID4]
41 @model_validator(mode="before")
42 @classmethod
43 def assign_active_files(cls, data: dict[str, Any]) -> dict[str, Any]:
44 if "files" not in data:
45 return data
47 schema = BenefitDownloadablesProperties(**data)
48 actives = get_active_file_ids(schema)
49 return dict(active_files=actives)
52class BenefitDownloadablesCreate(BenefitCreateBase):
53 type: Literal[BenefitType.downloadables]
54 properties: BenefitDownloadablesCreateProperties
57class BenefitDownloadablesUpdate(BenefitUpdateBase):
58 type: Literal[BenefitType.downloadables]
59 properties: BenefitDownloadablesCreateProperties | None = None
62class BenefitDownloadables(BenefitBase):
63 type: Literal[BenefitType.downloadables]
64 properties: BenefitDownloadablesProperties
67class BenefitDownloadablesSubscriber(BenefitSubscriberBase):
68 type: Literal[BenefitType.downloadables]
69 properties: BenefitDownloadablesSubscriberProperties