Coverage for polar/event_type/schemas.py: 63%

35 statements  

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

1from datetime import datetime 1a

2from typing import Annotated 1a

3 

4from fastapi import Path 1a

5from pydantic import UUID4, Field, field_validator 1a

6 

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

8from polar.models.event import EventSource 1a

9 

10 

11class EventTypeUpdate(Schema): 1a

12 label: str = Field( 1a

13 ..., description="The label for the event type.", min_length=1, max_length=128 

14 ) 

15 label_property_selector: str | None = Field( 1a

16 None, 

17 description="Property path to extract dynamic label from event metadata (e.g., 'subject' or 'metadata.subject').", 

18 min_length=1, 

19 max_length=256, 

20 ) 

21 

22 @field_validator("label") 1a

23 @classmethod 1a

24 def strip_and_validate_label(cls, v: str) -> str: 1a

25 v = v.strip() 

26 if not v: 

27 raise ValueError("Label cannot be empty or only whitespace") 

28 return v 

29 

30 @field_validator("label_property_selector") 1a

31 @classmethod 1a

32 def strip_and_validate_label_property_selector(cls, v: str | None) -> str | None: 1a

33 if v is not None: 

34 v = v.strip() 

35 if not v: 

36 return None 

37 return v 

38 

39 

40class EventType(IDSchema, TimestampedSchema): 1a

41 name: str = Field(..., description="The name of the event type.") 1a

42 label: str = Field(..., description="The label for the event type.") 1a

43 label_property_selector: str | None = Field( 1a

44 None, 

45 description="Property path to extract dynamic label from event metadata.", 

46 ) 

47 organization_id: UUID4 = Field( 1a

48 ..., description="The ID of the organization owning the event type." 

49 ) 

50 

51 

52class EventTypeWithStats(EventType): 1a

53 source: EventSource = Field( 1a

54 description="The source of the events (system or user)." 

55 ) 

56 occurrences: int = Field(description="Number of times the event has occurred.") 1a

57 first_seen: datetime = Field(description="The first time the event occurred.") 1a

58 last_seen: datetime = Field(description="The last time the event occurred.") 1a

59 

60 

61EventTypeID = Annotated[UUID4, Path(description="The event type ID.")] 1a