Coverage for src/paperap/models/correspondent/queryset.py: 71%

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 logging

25 from typing import TYPE_CHECKING, Any, Optional, Self, Union

26

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

28 from paperap.models.mixins.queryset import HasDocumentCount, HasOwner

29

30 if TYPE_CHECKING:

31 from paperap.models.correspondent.model import Correspondent

32

33 logger = logging.getLogger(__name__)

34

35

36 class CorrespondentQuerySet(StandardQuerySet["Correspondent"], HasOwner, HasDocumentCount):

37 """

38 QuerySet for Paperless-ngx correspondents with specialized filtering methods.

39 """

40

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

42 """

43 Filter correspondents by name.

44

45 Args:

46 name: The correspondent name to filter by

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

48

49 Returns:

50 Filtered CorrespondentQuerySet

51

52 """

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

54

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

56 """

57 Filter correspondents by their matching rule pattern.

58

59 Args:

60 rule_pattern: The pattern to search for in matching rules

61

62 Returns:

63 Filtered CorrespondentQuerySet

64

65 """

66 return self.filter_field_by_str("matching_algorithm", value, exact=exact, case_insensitive=case_insensitive)

67

68 def match(self, match: str, *, exact: bool = True, case_insensitive: bool = True) -> Self:

69 """

70 Filter correspondents by match.

71

72 Args:

73 match: The match to filter by

74 exact: If True, match the exact match, otherwise use contains

75

76 Returns:

77 Filtered CorrespondentQuerySet

78

79 """

80 return self.filter_field_by_str("match", match, exact=exact, case_insensitive=case_insensitive)

81

82 def case_insensitive(self, insensitive: bool = True) -> Self:

83 """

84 Filter correspondents by case sensitivity setting.

85

86 Args:

87 insensitive: If True, get correspondents with case insensitive matching

88

89 Returns:

90 Filtered CorrespondentQuerySet

91

92 """

93 return self.filter(is_insensitive=insensitive)

94

95 def user_can_change(self, value: bool) -> Self:

96 """

97 Filter correspondents by user_can_change.

98

99 Args:

100 value: The value to filter by

101

102 Returns:

103 Filtered CorrespondentQuerySet

104

105 """

106 return self.filter(user_can_change=value)