Coverage for src/cstlcore/glossary/models.py: 97%

36 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2026-02-19 12:46 +0000

1import uuid 

2from datetime import datetime 

3from typing import TYPE_CHECKING 

4 

5from sqlalchemy import JSON, Column as SAColumn 

6from sqlmodel import TIMESTAMP, Column, Field, Relationship, SQLModel, text 

7 

8if TYPE_CHECKING: 

9 from cstlcore.constellations.models import Constellation 

10 

11 

12class GlossaryTermBase(SQLModel): 

13 term: str = Field(index=True, nullable=False, min_length=1) 

14 definition: str = Field(nullable=False, min_length=1) 

15 

16 

17class GlossaryTerm(GlossaryTermBase, table=True): 

18 __tablename__ = "glossary_terms" 

19 

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

21 constellation_id: uuid.UUID = Field( 

22 foreign_key="constellation.id", 

23 nullable=False, 

24 index=True, 

25 ) 

26 # Liste d'exemples stockée en JSON 

27 examples: list[str] = Field( 

28 default_factory=list, 

29 sa_column=SAColumn(JSON, nullable=False, server_default="[]"), 

30 ) 

31 # Liste d'IDs de termes synonymes stockée en JSON 

32 synonym_ids: list[str] = Field( 

33 default_factory=list, 

34 sa_column=SAColumn(JSON, nullable=False, server_default="[]"), 

35 ) 

36 # Liste d'IDs de termes liés stockée en JSON 

37 related_term_ids: list[str] = Field( 

38 default_factory=list, 

39 sa_column=SAColumn(JSON, nullable=False, server_default="[]"), 

40 ) 

41 created_at: datetime | None = Field( 

42 default=None, 

43 sa_column=Column( 

44 TIMESTAMP(timezone=True), 

45 nullable=False, 

46 server_default=text("CURRENT_TIMESTAMP"), 

47 ), 

48 ) 

49 updated_at: datetime | None = Field( 

50 default=None, 

51 sa_column=Column( 

52 TIMESTAMP(timezone=True), 

53 nullable=False, 

54 server_default=text("CURRENT_TIMESTAMP"), 

55 onupdate=text("CURRENT_TIMESTAMP"), 

56 ), 

57 ) 

58 

59 constellation: "Constellation" = Relationship(back_populates="glossary_terms") 

60 

61 

62class GlossaryTermPublic(GlossaryTermBase): 

63 id: uuid.UUID 

64 constellation_id: uuid.UUID 

65 examples: list[str] = [] 

66 synonym_ids: list[str] = [] 

67 related_term_ids: list[str] = [] 

68 

69 

70class GlossaryTermCreate(GlossaryTermBase): 

71 examples: list[str] = [] 

72 synonym_ids: list[str] = [] 

73 related_term_ids: list[str] = [] 

74 

75 

76class GlossaryTermUpdate(SQLModel): 

77 term: str | None = None 

78 definition: str | None = None 

79 examples: list[str] | None = None 

80 synonym_ids: list[str] | None = None 

81 related_term_ids: list[str] | None = None