Coverage for polar/benefit/strategies/base/schemas.py: 98%

42 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-12-05 15:52 +0000

1from datetime import datetime 1a

2 

3from pydantic import UUID4, Field, computed_field 1a

4from pydantic.json_schema import SkipJsonSchema 1a

5 

6from polar.kit.metadata import MetadataInputMixin, MetadataOutputMixin 1a

7from polar.kit.schemas import IDSchema, Schema, TimestampedSchema 1a

8from polar.models.benefit import BenefitType 1a

9from polar.models.benefit_grant import BenefitGrantError 1a

10from polar.organization.schemas import OrganizationID, OrganizationPublicBase 1a

11 

12BENEFIT_DESCRIPTION_MIN_LENGTH = 3 1a

13BENEFIT_DESCRIPTION_MAX_LENGTH = 42 1a

14 

15 

16class BenefitProperties(Schema): ... 1a

17 

18 

19class BenefitCreateBase(MetadataInputMixin, Schema): 1a

20 type: BenefitType 1a

21 description: str = Field( 1a

22 ..., 

23 min_length=BENEFIT_DESCRIPTION_MIN_LENGTH, 

24 max_length=BENEFIT_DESCRIPTION_MAX_LENGTH, 

25 description=( 

26 "The description of the benefit. " 

27 "Will be displayed on products having this benefit." 

28 ), 

29 ) 

30 organization_id: OrganizationID | None = Field( 1a

31 None, 

32 description=( 

33 "The ID of the organization owning the benefit. " 

34 "**Required unless you use an organization token.**" 

35 ), 

36 ) 

37 

38 

39class BenefitUpdateBase(MetadataInputMixin, Schema): 1a

40 description: str | None = Field( 1a

41 None, 

42 min_length=BENEFIT_DESCRIPTION_MIN_LENGTH, 

43 max_length=BENEFIT_DESCRIPTION_MAX_LENGTH, 

44 description=( 

45 "The description of the benefit. " 

46 "Will be displayed on products having this benefit." 

47 ), 

48 ) 

49 

50 

51class BenefitPublicBase(TimestampedSchema, IDSchema): 1a

52 id: UUID4 = Field(..., description="The ID of the benefit.") 1a

53 type: BenefitType = Field(..., description="The type of the benefit.") 1a

54 description: str = Field(..., description="The description of the benefit.") 1a

55 selectable: bool = Field( 1a

56 ..., description="Whether the benefit is selectable when creating a product." 

57 ) 

58 deletable: bool = Field(..., description="Whether the benefit is deletable.") 1a

59 organization_id: UUID4 = Field( 1a

60 ..., description="The ID of the organization owning the benefit." 

61 ) 

62 

63 

64class BenefitBase(MetadataOutputMixin, BenefitPublicBase): ... 1a

65 

66 

67class BenefitGrantBase(IDSchema, TimestampedSchema): 1a

68 """ 

69 A grant of a benefit to a customer. 

70 """ 

71 

72 id: UUID4 = Field(description="The ID of the grant.") 1a

73 granted_at: datetime | None = Field( 1a

74 None, 

75 description=( 

76 "The timestamp when the benefit was granted. " 

77 "If `None`, the benefit is not granted." 

78 ), 

79 ) 

80 is_granted: bool = Field(description="Whether the benefit is granted.") 1a

81 revoked_at: datetime | None = Field( 1a

82 None, 

83 description=( 

84 "The timestamp when the benefit was revoked. " 

85 "If `None`, the benefit is not revoked." 

86 ), 

87 ) 

88 is_revoked: bool = Field(description="Whether the benefit is revoked.") 1a

89 subscription_id: UUID4 | None = Field( 1a

90 description="The ID of the subscription that granted this benefit.", 

91 ) 

92 order_id: UUID4 | None = Field( 1a

93 description="The ID of the order that granted this benefit." 

94 ) 

95 customer_id: UUID4 = Field( 1a

96 description="The ID of the customer concerned by this grant." 

97 ) 

98 benefit_id: UUID4 = Field( 1a

99 description="The ID of the benefit concerned by this grant." 

100 ) 

101 error: BenefitGrantError | None = Field( 1a

102 None, 

103 description="The error information if the benefit grant failed with an unrecoverable error.", 

104 ) 

105 

106 @computed_field(deprecated="Use `customer_id`.") 1a

107 def user_id(self) -> SkipJsonSchema[UUID4]: 1a

108 return self.customer_id 

109 

110 

111class BenefitSubscriberOrganization(OrganizationPublicBase): ... 1a

112 

113 

114class BenefitSubscriberBase(BenefitBase): 1a

115 organization: BenefitSubscriberOrganization 1a