Coverage for src/paperap/models/share_links/queryset.py: 60%

1"""

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

3

4 METADATA:

5

6 File: queryset.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 import datetime

25 import logging

26 from typing import TYPE_CHECKING, Any, Self

27

28 from paperap.models.abstract.queryset import BaseQuerySet, StandardQuerySet

29

30 if TYPE_CHECKING:

31 from paperap.models.share_links.model import ShareLinks

32

33 logger = logging.getLogger(__name__)

34

35

36 class ShareLinksQuerySet(StandardQuerySet["ShareLinks"]):

37 """

38 A lazy-loaded, chainable query interface for Paperless NGX resources.

39

40 BaseQuerySet provides pagination, filtering, and caching functionality similar to Django's BaseQuerySet.

41 It's designed to be lazy - only fetching data when it's actually needed.

42 """

43

44 def expiration_before(self, value: datetime.datetime | str) -> Self:

45 """

46 Filter ShareLinks where expiration is before value

47

48 Args:

49 value (datetime.datetime | str): The value to compare against

50

51 Returns:

52 ShareLinksQuerySet: The filtered queryset

53

54 """

55 return self.filter(expiration__lt=value)

56

57 def expiration_after(self, value: datetime.datetime | str) -> Self:

58 """

59 Filter ShareLinks where expiration is after value

60

61 Args:

62 value (datetime.datetime | str): The value to compare against

63

64 Returns:

65 ShareLinksQuerySet: The filtered queryset

66

67 """

68 return self.filter(expiration__gt=value)

69

70 def slug(self, value: str, *, exact: bool = True, case_insensitive: bool = True) -> Self:

71 """

72 Filter ShareLinks where slug is value

73

74 Args:

75 value (str): The value to compare against

76 exact (bool): Whether the comparison should be exact

77 case_sensitive (bool): Whether the comparison should be case insensitive

78

79 Returns:

80 ShareLinksQuerySet: The filtered queryset

81

82 """

83 return self.filter_field_by_str("slug", value, exact=exact, case_insensitive=case_insensitive)

84

85 def document(self, value: int | list[int]) -> Self:

86 """

87 Filter ShareLinks where document is value

88

89 Args:

90 value (int | list[int]): The value to compare against

91

92 Returns:

93 ShareLinksQuerySet: The filtered queryset

94

95 """

96 if isinstance(value, int):

97 return self.filter(document=value)

98 return self.filter(document__in=value)

99

100 def file_version(self, value: str) -> Self:

101 """

102 Filter ShareLinks where file_version is value

103

104 Args:

105 value (str): The value to compare against

106

107 Returns:

108 ShareLinksQuerySet: The filtered queryset

109

110 """

111 return self.filter(file_version=value)

112

113 def created_before(self, date: datetime.datetime) -> Self:

114 """

115 Filter models created before a given date.

116

117 Args:

118 date: The date to filter by

119

120 Returns:

121 Filtered QuerySet

122

123 """

124 return self.filter(created__lt=date)

125

126 def created_after(self, date: datetime.datetime) -> Self:

127 """

128 Filter models created after a given date.

129

130 Args:

131 date: The date to filter by

132

133 Returns:

134 Filtered QuerySet

135

136 """

137 return self.filter(created__gt=date)

138

139 def created_between(self, start: datetime.datetime, end: datetime.datetime) -> Self:

140 """

141 Filter models created between two dates.

142

143 Args:

144 start: The start date to filter by

145 end: The end date to filter by

146

147 Returns:

148 Filtered QuerySet

149

150 """

151 return self.filter(created__range=(start, end))