Coverage for src/cstlcore/assets/models.py: 98%

44 statements  

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

1import enum 

2import uuid 

3from datetime import datetime 

4from typing import TYPE_CHECKING 

5 

6from fastapi import UploadFile 

7from sqlmodel import ( 

8 TIMESTAMP, 

9 Column, 

10 FetchedValue, 

11 Field, 

12 Relationship, 

13 SQLModel, 

14 text, 

15) 

16 

17if TYPE_CHECKING: 

18 from cstlcore.constellations.models import Constellation 

19 

20 

21class AssetType(enum.Enum): 

22 AVATAR = "AVATAR" 

23 ATTACHMENT = "ATTACHMENT" 

24 OTHER = "OTHER" 

25 

26 

27class AssetBase(SQLModel): 

28 name: str 

29 description: str | None = None 

30 type: AssetType = AssetType.OTHER 

31 mime_type: str 

32 

33 

34class AssetCreate(AssetBase): 

35 file: UploadFile 

36 pass 

37 

38 

39class AssetUpdate(SQLModel): 

40 collection_id: uuid.UUID | None = None 

41 name: str | None = None 

42 description: str | None = None 

43 type: AssetType | None = None 

44 filename: str | None = None 

45 mime_type: str | None = None 

46 content: bytes | None = None 

47 

48 

49class AssetPublic(AssetBase): 

50 id: uuid.UUID 

51 constellation_id: uuid.UUID 

52 collection_id: uuid.UUID | None = None 

53 owner_id: uuid.UUID 

54 created_at: datetime 

55 updated_at: datetime 

56 

57 

58class Asset(AssetBase, table=True): 

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

60 constellation_id: uuid.UUID = Field( 

61 foreign_key="constellation.id", nullable=False, index=True, ondelete="CASCADE" 

62 ) 

63 collection_id: uuid.UUID | None = Field( 

64 None, foreign_key="collection.id", nullable=True, index=True 

65 ) 

66 owner_id: uuid.UUID = Field(foreign_key="user.id", nullable=False, index=True) 

67 

68 content: bytes = Field(nullable=False) 

69 created_at: datetime | None = Field( 

70 default=None, 

71 sa_column=Column( 

72 TIMESTAMP(timezone=True), 

73 nullable=False, 

74 server_default=text("CURRENT_TIMESTAMP"), 

75 ), 

76 ) 

77 

78 updated_at: datetime | None = Field( 

79 default=None, 

80 sa_column=Column( 

81 TIMESTAMP(timezone=True), 

82 nullable=False, 

83 server_default=text("CURRENT_TIMESTAMP"), 

84 server_onupdate=FetchedValue(), 

85 ), 

86 ) 

87 

88 constellation: "Constellation" = Relationship(back_populates="assets")