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

14 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-03-20 13:17 -0400

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 

22from __future__ import annotations 

23 

24import logging 

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

26 

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

28from paperap.models.mixins.queryset import HasDocumentCount 

29 

30if TYPE_CHECKING: 

31 from paperap.models.custom_field.model import CustomField 

32 

33logger = logging.getLogger(__name__) 

34 

35 

36class CustomFieldQuerySet(StandardQuerySet["CustomField"], HasDocumentCount): 

37 """ 

38 QuerySet for Paperless-ngx custom fields with specialized filtering methods. 

39 """ 

40 

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

42 """ 

43 Filter custom fields by name. 

44 

45 Args: 

46 value: The custom field name to filter by 

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

48 case_insensitive: If True, ignore case when matching 

49 

50 Returns: 

51 Filtered CustomFieldQuerySet 

52 

53 """ 

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

55 

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

57 """ 

58 Filter custom fields by data type. 

59 

60 Args: 

61 value: The data type to filter by (e.g., "string", "integer", "boolean", "date") 

62 exact: If True, match the exact data type, otherwise use contains 

63 case_insensitive: If True, ignore case when matching 

64 

65 Returns: 

66 Filtered CustomFieldQuerySet 

67 

68 """ 

69 return self.filter_field_by_str("data_type", value, exact=exact, case_insensitive=case_insensitive) 

70 

71 def extra_data(self, key: str, value: Any) -> Self: 

72 """ 

73 Filter custom fields by a key-value pair in extra_data. 

74 

75 Args: 

76 key: The key in extra_data to filter by 

77 value: The value to filter by 

78 

79 Returns: 

80 Filtered CustomFieldQuerySet 

81 

82 """ 

83 filter_key = f"extra_data__{key}" 

84 return self.filter(**{filter_key: value})