Coverage for polar/event_type/schemas.py: 90%
35 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 datetime import datetime
2from typing import Annotated
4from fastapi import Path
5from pydantic import UUID4, Field, field_validator
7from polar.kit.schemas import IDSchema, Schema, TimestampedSchema
8from polar.models.event import EventSource
11class EventTypeUpdate(Schema):
12 label: str = Field(
13 ..., description="The label for the event type.", min_length=1, max_length=128
14 )
15 label_property_selector: str | None = Field(
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 )
22 @field_validator("label")
23 @classmethod
24 def strip_and_validate_label(cls, v: str) -> str:
25 v = v.strip()
26 if not v: 26 ↛ 27line 26 didn't jump to line 27 because the condition on line 26 was never true
27 raise ValueError("Label cannot be empty or only whitespace")
28 return v
30 @field_validator("label_property_selector")
31 @classmethod
32 def strip_and_validate_label_property_selector(cls, v: str | None) -> str | None:
33 if v is not None:
34 v = v.strip()
35 if not v: 35 ↛ 36line 35 didn't jump to line 36 because the condition on line 35 was never true
36 return None
37 return v
40class EventType(IDSchema, TimestampedSchema):
41 name: str = Field(..., description="The name of the event type.")
42 label: str = Field(..., description="The label for the event type.")
43 label_property_selector: str | None = Field(
44 None,
45 description="Property path to extract dynamic label from event metadata.",
46 )
47 organization_id: UUID4 = Field(
48 ..., description="The ID of the organization owning the event type."
49 )
52class EventTypeWithStats(EventType):
53 source: EventSource = Field(
54 description="The source of the events (system or user)."
55 )
56 occurrences: int = Field(description="Number of times the event has occurred.")
57 first_seen: datetime = Field(description="The first time the event occurred.")
58 last_seen: datetime = Field(description="The last time the event occurred.")
61EventTypeID = Annotated[UUID4, Path(description="The event type ID.")]