Coverage for polar/benefit/strategies/downloadables/schemas.py: 62%
42 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-12-05 16:17 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2025-12-05 16:17 +0000
1from typing import Annotated, Any, Literal 1a
3from annotated_types import Len 1a
4from pydantic import UUID4, model_validator 1a
6from polar.kit.schemas import Schema 1a
7from polar.models.benefit import BenefitType 1a
9from ..base.schemas import ( 1a
10 BenefitBase,
11 BenefitCreateBase,
12 BenefitSubscriberBase,
13 BenefitUpdateBase,
14)
17class BenefitDownloadablesCreateProperties(Schema): 1a
18 archived: dict[UUID4, bool] = {} 1a
19 files: Annotated[list[UUID4], Len(min_length=1)] 1a
22class BenefitDownloadablesProperties(Schema): 1a
23 archived: dict[UUID4, bool] 1a
24 files: list[UUID4] 1a
27def get_active_file_ids(properties: BenefitDownloadablesProperties) -> list[UUID4]: 1a
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:
33 active.append(file_id)
35 return active
38class BenefitDownloadablesSubscriberProperties(Schema): 1a
39 active_files: list[UUID4] 1a
41 @model_validator(mode="before") 1a
42 @classmethod 1a
43 def assign_active_files(cls, data: dict[str, Any]) -> dict[str, Any]: 1a
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): 1a
53 type: Literal[BenefitType.downloadables] 1a
54 properties: BenefitDownloadablesCreateProperties 1a
57class BenefitDownloadablesUpdate(BenefitUpdateBase): 1a
58 type: Literal[BenefitType.downloadables] 1a
59 properties: BenefitDownloadablesCreateProperties | None = None 1a
62class BenefitDownloadables(BenefitBase): 1a
63 type: Literal[BenefitType.downloadables] 1a
64 properties: BenefitDownloadablesProperties 1a
67class BenefitDownloadablesSubscriber(BenefitSubscriberBase): 1a
68 type: Literal[BenefitType.downloadables] 1a
69 properties: BenefitDownloadablesSubscriberProperties 1a