Coverage for polar/models/file.py: 95%

39 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 12:42 +0000

1from datetime import datetime 

2from enum import StrEnum 

3from typing import TYPE_CHECKING 

4from uuid import UUID 

5 

6from sqlalchemy import ( 

7 TIMESTAMP, 

8 BigInteger, 

9 Boolean, 

10 ForeignKey, 

11 String, 

12 Uuid, 

13) 

14from sqlalchemy.orm import ( 

15 Mapped, 

16 declared_attr, 

17 mapped_column, 

18 relationship, 

19) 

20 

21from polar.kit.db.models import RecordModel 

22 

23if TYPE_CHECKING: 23 ↛ 24line 23 didn't jump to line 24 because the condition on line 23 was never true

24 from polar.models import ( 

25 Organization, 

26 ) 

27 

28 

29class FileServiceTypes(StrEnum): 

30 downloadable = "downloadable" 

31 product_media = "product_media" 

32 organization_avatar = "organization_avatar" 

33 

34 

35class File(RecordModel): 

36 __tablename__ = "files" 

37 

38 organization_id: Mapped[UUID] = mapped_column( 

39 Uuid, 

40 ForeignKey("organizations.id", ondelete="cascade"), 

41 nullable=False, 

42 index=True, 

43 ) 

44 

45 @declared_attr 

46 def organization(cls) -> Mapped["Organization"]: 

47 return relationship("Organization", lazy="raise") 

48 

49 name: Mapped[str] = mapped_column(String, nullable=False) 

50 version: Mapped[str | None] = mapped_column(String, nullable=True) 

51 path: Mapped[str] = mapped_column(String, nullable=False) 

52 mime_type: Mapped[str] = mapped_column(String, nullable=False) 

53 size: Mapped[int] = mapped_column(BigInteger, nullable=False) 

54 

55 service: Mapped[FileServiceTypes] = mapped_column(String, nullable=False) 

56 

57 last_modified_at: Mapped[datetime | None] = mapped_column( 

58 TIMESTAMP(timezone=True), 

59 nullable=True, 

60 ) 

61 

62 storage_version: Mapped[str | None] = mapped_column(String, nullable=True) 

63 checksum_etag: Mapped[str | None] = mapped_column(String, nullable=True) 

64 checksum_sha256_base64: Mapped[str | None] = mapped_column(String, nullable=True) 

65 checksum_sha256_hex: Mapped[str | None] = mapped_column(String, nullable=True) 

66 

67 is_uploaded: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False) 

68 

69 # Flag for Polar to disable consumption of file 

70 is_enabled: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False) 

71 

72 __mapper_args__ = { 

73 "polymorphic_on": "service", 

74 } 

75 

76 

77class DownloadableFile(File): 

78 __mapper_args__ = { 

79 "polymorphic_identity": FileServiceTypes.downloadable, 

80 } 

81 

82 

83class ProductMediaFile(File): 

84 __mapper_args__ = { 

85 "polymorphic_identity": FileServiceTypes.product_media, 

86 } 

87 

88 

89class OrganizationAvatarFile(File): 

90 __mapper_args__ = { 

91 "polymorphic_identity": FileServiceTypes.organization_avatar, 

92 }