Coverage for youversion/models/moments.py: 100%

23 statements  

« prev     ^ index     » next       coverage.py v7.14.3, created at 2026-06-26 11:31 +0100

1"""Pydantic models for moment creation and updates.""" 

2 

3from typing import Any 

4 

5from pydantic import BaseModel, Field 

6 

7from ..enums import MomentKinds, StatusEnum 

8 

9 

10class ReferenceCreate(BaseModel): 

11 """Reference data for moment creation. 

12 

13 Args: 

14 human: Human-readable reference (e.g., "Genesis 1:1") 

15 version_id: Bible version ID 

16 usfm: USFM reference as list of strings 

17 """ 

18 

19 human: str 

20 version_id: int 

21 usfm: list[str] 

22 

23 

24class CreateMoment(BaseModel): 

25 """Pydantic model for creating a moment. 

26 

27 Args: 

28 kind: Type of moment (e.g., "note", "highlight", "bookmark") 

29 content: Content text (optional) 

30 references: List of Bible references (optional) 

31 title: Moment title (optional) 

32 status: Status (private, draft or public, optional) 

33 body: Body text (optional) 

34 color: Color hex code (optional) 

35 labels: List of label strings (optional) 

36 language_tag: Language tag (optional, e.g., "en") 

37 """ 

38 

39 kind: MomentKinds = Field(..., description="Type of moment") 

40 content: str = Field(..., description="Content text") 

41 references: list[ReferenceCreate] = Field( 

42 ..., description="List of Bible references" 

43 ) 

44 title: str = Field(..., description="Moment title") 

45 status: StatusEnum = Field(..., description="Status (private, draft or public)") 

46 body: str = Field(..., description="Body text") 

47 color: str = Field(..., description="Color hex code", min_length=6, max_length=6) 

48 labels: list[str] = Field( 

49 ..., description="List of labels", min_length=1, max_length=10 

50 ) 

51 language_tag: str = Field( 

52 ..., 

53 description="Language tag (e.g., 'en')", 

54 min_length=2, 

55 max_length=2, 

56 ) 

57 

58 def model_dump(self, **kwargs: Any) -> dict[str, Any]: 

59 """Convert model to dictionary for API request. 

60 

61 Args: 

62 **kwargs: Additional arguments for model_dump 

63 

64 Returns: 

65 Dictionary representation of the model 

66 """ 

67 # Use mode='python' to get enum values as strings 

68 data = super().model_dump(mode="python", **kwargs) 

69 # Convert enum values to their string values 

70 if "kind" in data and hasattr(data["kind"], "value"): 

71 data["kind"] = data["kind"].value 

72 if "status" in data and hasattr(data["status"], "value"): 

73 data["status"] = data["status"].value 

74 # Convert references to dict format if present 

75 if "references" in data and data["references"]: 

76 data["references"] = [ 

77 ref.model_dump() if isinstance(ref, ReferenceCreate) else ref 

78 for ref in data["references"] 

79 ] 

80 # Remove None values to keep payload clean 

81 return {k: v for k, v in data.items() if v is not None}