Coverage for src/cstlcore/maps/models.py: 95%

42 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 fastapi import UploadFile 

6from sqlalchemy.dialects.postgresql import JSONB 

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

8 

9if TYPE_CHECKING: 

10 from cstlcore.collections.models import Collection 

11 from cstlcore.ydocs.models import YDoc 

12 

13 

14class MapBase(SQLModel): 

15 name: str = Field(index=True, nullable=False) 

16 description: str | None = Field(default=None, nullable=True) 

17 

18 

19class Map(MapBase, table=True): 

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

21 collection_id: uuid.UUID = Field( 

22 foreign_key="collection.id", index=True, nullable=False, ondelete="CASCADE" 

23 ) 

24 created_at: datetime | None = Field( 

25 default=None, 

26 sa_column=Column( 

27 TIMESTAMP(timezone=True), 

28 nullable=False, 

29 server_default=text("CURRENT_TIMESTAMP"), 

30 ), 

31 ) 

32 base_layer: dict | None = Field( 

33 default=None, sa_column=Column(JSONB, nullable=True) 

34 ) 

35 collection: "Collection" = Relationship(back_populates="maps") 

36 

37 layers: list["MapLayer"] = Relationship( 

38 back_populates="map", 

39 cascade_delete=True, 

40 ) 

41 

42 

43class MapCreate(MapBase): 

44 file: UploadFile 

45 

46 

47class MapPublic(MapBase): 

48 id: uuid.UUID 

49 collection_id: uuid.UUID 

50 base_layer: dict | None 

51 

52 

53class MapUpdate(SQLModel): 

54 name: str | None = None 

55 description: str | None = None 

56 base_layer: dict | None = None 

57 

58 

59class MapLayerBase(SQLModel): 

60 map_id: uuid.UUID = Field( 

61 foreign_key="map.id", 

62 primary_key=True, 

63 index=True, 

64 nullable=False, 

65 ondelete="CASCADE", 

66 ) 

67 ydoc_id: uuid.UUID = Field( 

68 foreign_key="ydoc.id", 

69 primary_key=True, 

70 index=True, 

71 nullable=False, 

72 ondelete="CASCADE", 

73 ) 

74 layer: dict = Field(sa_column=Column(JSONB, nullable=False)) 

75 

76 

77class MapLayer(MapLayerBase, table=True): 

78 map: "Map" = Relationship(back_populates="layers") 

79 ydoc: "YDoc" = Relationship(back_populates="layers") 

80 

81 

82class MapLayerCreate(MapLayerBase): 

83 pass 

84 

85 

86class MapLayerPublic(MapLayerBase): 

87 pass 

88 

89 

90class MapLayerUpdate(SQLModel): 

91 layer: dict