Coverage for src/paperap/models/share_links/model.py: 86%

1"""

2----------------------------------------------------------------------------

3

4 METADATA:

5

6 File: share_links.py

7 Project: paperap

8 Created: 2025-03-04

9 Version: 0.0.5

10 Author: Jess Mann

11 Email: jess@jmann.me

12 Copyright (c) 2025 Jess Mann

13

14----------------------------------------------------------------------------

15

16 LAST MODIFIED:

17

18 2025-03-04 By Jess Mann

19

20"""

21

22 from __future__ import annotations

23

24 from datetime import datetime

25 from typing import TYPE_CHECKING, Any

26

27 from pydantic import Field, field_serializer

28 from yarl import URL

29

30 from paperap.models.abstract.model import StandardModel

31 from paperap.models.share_links.queryset import ShareLinksQuerySet

32

33 if TYPE_CHECKING:

34 from paperap.models.correspondent import Correspondent

35 from paperap.models.document import Document

36 from paperap.models.document_type import DocumentType

37 from paperap.models.storage_path import StoragePath

38 from paperap.models.tag import Tag

39

40

41 class ShareLinks(StandardModel):

42 expiration: datetime | None = None

43 slug: str | None = None

44 document: int | None = None

45 created: datetime | None = Field(description="Creation timestamp", default=None, alias="created_on")

46 file_version: str | None = None

47

48 class Meta(StandardModel.Meta):

49 queryset = ShareLinksQuerySet

50

51 @field_serializer("expiration", "created")

52 def serialize_datetime(self, value: datetime | None) -> str | None:

53 """

54 Serialize a datetime object to an ISO 8601 formatted string

55

56 Args:

57 value (datetime): The datetime object to serialize

58

59 Returns:

60 str: The serialized datetime

61

62 """

63 return value.isoformat() if value else None

64

65 def get_document(self) -> "Document":

66 """

67 Get the document associated with this share link

68

69 Returns:

70 Document: The document object

71

72 """

73 if not self.document:

74 raise ValueError("Document ID not set")

75 return self._client.documents().get(pk=self.document)