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

1from typing import Annotated, Any, Literal 1a

2 

3from annotated_types import Len 1a

4from pydantic import UUID4, model_validator 1a

5 

6from polar.kit.schemas import Schema 1a

7from polar.models.benefit import BenefitType 1a

8 

9from ..base.schemas import ( 1a

10 BenefitBase, 

11 BenefitCreateBase, 

12 BenefitSubscriberBase, 

13 BenefitUpdateBase, 

14) 

15 

16 

17class BenefitDownloadablesCreateProperties(Schema): 1a

18 archived: dict[UUID4, bool] = {} 1a

19 files: Annotated[list[UUID4], Len(min_length=1)] 1a

20 

21 

22class BenefitDownloadablesProperties(Schema): 1a

23 archived: dict[UUID4, bool] 1a

24 files: list[UUID4] 1a

25 

26 

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) 

34 

35 return active 

36 

37 

38class BenefitDownloadablesSubscriberProperties(Schema): 1a

39 active_files: list[UUID4] 1a

40 

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 

46 

47 schema = BenefitDownloadablesProperties(**data) 

48 actives = get_active_file_ids(schema) 

49 return dict(active_files=actives) 

50 

51 

52class BenefitDownloadablesCreate(BenefitCreateBase): 1a

53 type: Literal[BenefitType.downloadables] 1a

54 properties: BenefitDownloadablesCreateProperties 1a

55 

56 

57class BenefitDownloadablesUpdate(BenefitUpdateBase): 1a

58 type: Literal[BenefitType.downloadables] 1a

59 properties: BenefitDownloadablesCreateProperties | None = None 1a

60 

61 

62class BenefitDownloadables(BenefitBase): 1a

63 type: Literal[BenefitType.downloadables] 1a

64 properties: BenefitDownloadablesProperties 1a

65 

66 

67class BenefitDownloadablesSubscriber(BenefitSubscriberBase): 1a

68 type: Literal[BenefitType.downloadables] 1a

69 properties: BenefitDownloadablesSubscriberProperties 1a