Coverage for src/paperap/models/saved_view/queryset.py: 65%

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 enum import Enum

27 from typing import TYPE_CHECKING, Any, Self

28

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

30 from paperap.models.mixins.queryset import HasOwner

31

32 if TYPE_CHECKING:

33 from paperap.models.saved_view.model import SavedView

34

35 logger = logging.getLogger(__name__)

36

37

38 class SavedViewQuerySet(StandardQuerySet["SavedView"], HasOwner):

39 """

40 QuerySet for Paperless-ngx saved views with specialized filtering methods.

41 """

42

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

44 """

45 Filter saved views by name.

46

47 Args:

48 name: The saved view name to filter by

49 exact: If True, match the exact name, otherwise use contains

50

51 Returns:

52 Filtered SavedViewQuerySet

53

54 """

55 return self.filter_field_by_str("name", value, exact=exact, case_insensitive=case_insensitive)

56

57 def show_in_sidebar(self, show: bool = True) -> Self:

58 """

59 Filter saved views by sidebar visibility.

60

61 Args:

62 show: If True, get views shown in sidebar, otherwise those hidden

63

64 Returns:

65 Filtered SavedViewQuerySet

66

67 """

68 return self.filter(show_in_sidebar=show)

69

70 def show_on_dashboard(self, show: bool = True) -> Self:

71 """

72 Filter saved views by dashboard visibility.

73

74 Args:

75 show: If True, get views shown on dashboard, otherwise those hidden

76

77 Returns:

78 Filtered SavedViewQuerySet

79

80 """

81 return self.filter(show_on_dashboard=show)

82

83 def sort_field(self, field: str, *, exact: bool = True, case_insensitive: bool = True) -> Self:

84 """

85 Filter saved views by sort field.

86

87 Args:

88 field: The field to sort by

89 exact: If True, match the exact field, otherwise use contains

90

91 Returns:

92 Filtered SavedViewQuerySet

93

94 """

95 return self.filter_field_by_str("sort_field", field, exact=exact, case_insensitive=case_insensitive)

96

97 def sort_reverse(self, reverse: bool = True) -> Self:

98 """

99 Filter saved views by sort direction.

100

101 Args:

102 reverse: If True, get views sorted in reverse order

103

104 Returns:

105 Filtered SavedViewQuerySet

106

107 """

108 return self.filter(sort_reverse=reverse)

109

110 def page_size(self, size: int) -> Self:

111 """

112 Filter saved views by page size.

113

114 Args:

115 size: The number of items per page

116

117 Returns:

118 Filtered SavedViewQuerySet

119

120 """

121 return self.filter(page_size=size)

122

123 def page_size_under(self, size: int) -> Self:

124 """

125 Filter saved views by page size under a limit.

126

127 Args:

128 size: The maximum number of items per page

129

130 Returns:

131 Filtered SavedViewQuerySet

132

133 """

134 return self.filter(page_size__lt=size)

135

136 def page_size_over(self, size: int) -> Self:

137 """

138 Filter saved views by page size over a limit.

139

140 Args:

141 size: The minimum number of items per page

142

143 Returns:

144 Filtered SavedViewQuerySet

145

146 """

147 return self.filter(page_size__gt=size)

148

149 def page_size_between(self, min_size: int, max_size: int) -> Self:

150 """

151 Filter saved views by page size within a range.

152

153 Args:

154 min_size: The minimum number of items per page

155 max_size: The maximum number of items per page

156

157 Returns:

158 Filtered SavedViewQuerySet

159

160 """

161 return self.filter(page_size__gte=min_size, page_size__lte=max_size)

162

163 def display_mode(self, mode: str) -> Self:

164 """

165 Filter saved views by display mode.

166

167 Args:

168 mode: The display mode to filter by

169

170 Returns:

171 Filtered SavedViewQuerySet

172

173 """

174 return self.filter(display_mode=mode)

175

176 def user_can_change(self, can_change: bool = True) -> Self:

177 """

178 Filter saved views by user change permissions.

179

180 Args:

181 can_change: If True, get views that can be changed by the user

182

183 Returns:

184 Filtered SavedViewQuerySet

185

186 """

187 return self.filter(user_can_change=can_change)