Coverage for geoparser/db/models/document.py: 100%

31 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-20 18:08 +0000

1import typing as t 

2import uuid 

3from typing import List, Optional 

4 

5from pydantic import AfterValidator 

6from sqlalchemy import UUID, Column, ForeignKey 

7from sqlmodel import Field, Relationship, SQLModel 

8 

9from geoparser.db.models.base import GeoparserModel 

10from geoparser.db.models.validators import normalize_newlines 

11 

12if t.TYPE_CHECKING: 

13 from geoparser.db.models.project import Project 

14 from geoparser.db.models.recognition import Recognition 

15 from geoparser.db.models.reference import Reference 

16 

17 

18class DocumentBase(GeoparserModel): 

19 """ 

20 Base model for document data. 

21 

22 Contains the core text content of a document. 

23 """ 

24 

25 text: t.Annotated[str, AfterValidator(normalize_newlines)] 

26 

27 

28class Document(DocumentBase, table=True): 

29 """ 

30 Represents a document to be processed for reference recognition and resolution. 

31 

32 A document belongs to a project and can contain multiple references. 

33 """ 

34 

35 id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True) 

36 project_id: uuid.UUID = Field( 

37 sa_column=Column( 

38 UUID, ForeignKey("project.id", ondelete="CASCADE"), nullable=False 

39 ) 

40 ) 

41 project: "Project" = Relationship(back_populates="documents") 

42 references: list["Reference"] = Relationship( 

43 back_populates="document", 

44 sa_relationship_kwargs={ 

45 "order_by": "Reference.start", 

46 "cascade": "all, delete-orphan", 

47 "passive_deletes": True, 

48 "lazy": "selectin", 

49 }, 

50 ) 

51 recognitions: list["Recognition"] = Relationship( 

52 back_populates="document", 

53 sa_relationship_kwargs={ 

54 "cascade": "all, delete-orphan", 

55 "passive_deletes": True, 

56 }, 

57 ) 

58 

59 _recognizer_id: Optional[str] = None 

60 

61 def _set_recognizer_context(self, recognizer_id: str = None): 

62 """ 

63 Internal method to set the viewing context for references. 

64 

65 Args: 

66 recognizer_id: ID of the recognizer to use for filtering references 

67 """ 

68 self._recognizer_id = recognizer_id 

69 

70 @property 

71 def toponyms(self) -> List["Reference"]: 

72 """ 

73 Return references filtered by the configured recognizer. 

74 

75 Returns: 

76 List of Reference objects matching the configured recognizer ID, 

77 or empty list if no recognizer is configured 

78 """ 

79 if self._recognizer_id is None: 

80 return [] 

81 

82 return [ 

83 ref for ref in self.references if ref.recognizer_id == self._recognizer_id 

84 ] 

85 

86 def __str__(self) -> str: 

87 """ 

88 Return a string representation of the document. 

89 

90 Returns: 

91 String with document indicator and text content 

92 """ 

93 return f'Document("{self.text}")' 

94 

95 def __repr__(self) -> str: 

96 """ 

97 Return a developer representation of the document. 

98 

99 Returns: 

100 Same as __str__ method 

101 """ 

102 return self.__str__() 

103 

104 

105class DocumentCreate(DocumentBase): 

106 """ 

107 Model for creating a new document. 

108 

109 Includes the project_id to associate the document with a project. 

110 """ 

111 

112 project_id: uuid.UUID 

113 

114 

115class DocumentUpdate(SQLModel): 

116 """Model for updating an existing document.""" 

117 

118 id: uuid.UUID 

119 project_id: t.Optional[uuid.UUID] = None 

120 text: t.Optional[str] = None