|
1 |
| -from django_filters import FilterSet |
2 |
| -from django_filters import FilterSet, CharFilter |
| 1 | +""" |
| 2 | +Module containing custom filter classes for various models. |
| 3 | +""" |
| 4 | + |
| 5 | +import uuid |
3 | 6 | import django_filters
|
4 |
| -from .models import Asset,AssetAssignment,AssetCategory,AssetRequest,AssetLot |
5 | 7 | from django import forms
|
6 |
| -import uuid |
| 8 | +from django_filters import FilterSet |
| 9 | +from .models import Asset, AssetAssignment, AssetCategory, AssetRequest |
7 | 10 |
|
8 |
| - |
9 | 11 |
|
10 | 12 | class CustomFilterSet(FilterSet):
|
11 | 13 | def __init__(self, *args, **kwargs):
|
12 | 14 | super().__init__(*args, **kwargs)
|
13 |
| - |
| 15 | + |
14 | 16 | for field_name, field in self.filters.items():
|
15 | 17 | filter_widget = self.filters[field_name]
|
16 | 18 | widget = filter_widget.field.widget
|
17 |
| - if isinstance(widget, (forms.NumberInput, forms.EmailInput,forms.TextInput)): |
18 |
| - filter_widget.field.widget.attrs.update({'class': 'oh-input w-100'}) |
19 |
| - elif isinstance(widget,(forms.Select,)): |
20 |
| - filter_widget.field.widget.attrs.update({'class': 'oh-select oh-select-2',}) |
21 |
| - elif isinstance(widget,(forms.Textarea)): |
22 |
| - filter_widget.field.widget.attrs.update({'class': 'oh-input w-100'}) |
23 |
| - elif isinstance(widget, (forms.CheckboxInput,forms.CheckboxSelectMultiple,)): |
24 |
| - filter_widget.field.widget.attrs.update({'class': 'oh-switch__checkbox'}) |
25 |
| - elif isinstance(widget,(forms.ModelChoiceField)): |
26 |
| - filter_widget.field.widget.attrs.update({'class': 'oh-select oh-select-2 ',}) |
27 |
| - elif isinstance(widget,(forms.DateField)): |
28 |
| - filter_widget.field.widget.attrs.update({'type': 'date','class':'oh-input w-100'}) |
| 19 | + if isinstance( |
| 20 | + widget, (forms.NumberInput, forms.EmailInput, forms.TextInput) |
| 21 | + ): |
| 22 | + filter_widget.field.widget.attrs.update({"class": "oh-input w-100"}) |
| 23 | + elif isinstance(widget, (forms.Select,)): |
| 24 | + filter_widget.field.widget.attrs.update( |
| 25 | + { |
| 26 | + "class": "oh-select oh-select-2", |
| 27 | + } |
| 28 | + ) |
| 29 | + elif isinstance(widget, (forms.Textarea)): |
| 30 | + filter_widget.field.widget.attrs.update({"class": "oh-input w-100"}) |
| 31 | + elif isinstance( |
| 32 | + widget, |
| 33 | + ( |
| 34 | + forms.CheckboxInput, |
| 35 | + forms.CheckboxSelectMultiple, |
| 36 | + ), |
| 37 | + ): |
| 38 | + filter_widget.field.widget.attrs.update( |
| 39 | + {"class": "oh-switch__checkbox"} |
| 40 | + ) |
| 41 | + elif isinstance(widget, (forms.ModelChoiceField)): |
| 42 | + filter_widget.field.widget.attrs.update( |
| 43 | + { |
| 44 | + "class": "oh-select oh-select-2 ", |
| 45 | + } |
| 46 | + ) |
| 47 | + elif isinstance(widget, (forms.DateField)): |
| 48 | + filter_widget.field.widget.attrs.update( |
| 49 | + {"type": "date", "class": "oh-input w-100"} |
| 50 | + ) |
29 | 51 | if isinstance(field, django_filters.CharFilter):
|
30 |
| - field.lookup_expr='icontains' |
31 |
| - |
32 |
| - |
| 52 | + field.lookup_expr = "icontains" |
| 53 | + |
| 54 | + |
33 | 55 | class AssetExportFilter(CustomFilterSet):
|
34 |
| - |
| 56 | + """ |
| 57 | + Custom filter class for exporting filtered Asset data. |
| 58 | + """ |
| 59 | + |
35 | 60 | class Meta:
|
| 61 | + """ |
| 62 | + A nested class that specifies the configuration for the filter. |
| 63 | + model(class): The Asset model is used to filter. |
| 64 | + fields (str): A special value "__all__" to include all fields |
| 65 | + of the model in the filter. |
| 66 | + """ |
| 67 | + |
36 | 68 | model = Asset
|
37 |
| - fields = '__all__' |
| 69 | + fields = "__all__" |
38 | 70 |
|
39 | 71 | def __init__(self, *args, **kwargs):
|
40 | 72 | super(AssetExportFilter, self).__init__(*args, **kwargs)
|
41 |
| - self.form.fields['asset_purchase_date'].widget.attrs.update({'type':'date'}) |
42 |
| - |
| 73 | + self.form.fields["asset_purchase_date"].widget.attrs.update({"type": "date"}) |
| 74 | + |
| 75 | + |
43 | 76 | class AssetFilter(CustomFilterSet):
|
| 77 | + """ |
| 78 | + Custom filter set for Asset instances. |
| 79 | + """ |
| 80 | + |
44 | 81 | class Meta:
|
| 82 | + """ |
| 83 | + A nested class that specifies the configuration for the filter. |
| 84 | + model(class): The Asset model is used to filter. |
| 85 | + fields (str): A special value "__all__" to include all fields |
| 86 | + of the model in the filter. |
| 87 | + """ |
| 88 | + |
45 | 89 | model = Asset
|
46 |
| - fields = '__all__' |
47 |
| - def __init__(self,*args,**kwargs): |
48 |
| - super(AssetFilter,self).__init__(*args,**kwargs) |
| 90 | + fields = "__all__" |
| 91 | + |
| 92 | + def __init__(self, *args, **kwargs): |
| 93 | + super(AssetFilter, self).__init__(*args, **kwargs) |
49 | 94 | for visible in self.form.visible_fields():
|
50 |
| - visible.field.widget.attrs['id'] = str(uuid.uuid4()) |
51 |
| - |
| 95 | + visible.field.widget.attrs["id"] = str(uuid.uuid4()) |
| 96 | + |
52 | 97 |
|
53 | 98 | class CustomAssetFilter(CustomFilterSet):
|
54 |
| - asset_id__asset_name = django_filters.CharFilter(lookup_expr='icontains') |
| 99 | + """ |
| 100 | + Custom filter set for asset assigned to employees instances. |
| 101 | + """ |
| 102 | + |
| 103 | + asset_id__asset_name = django_filters.CharFilter(lookup_expr="icontains") |
55 | 104 |
|
56 | 105 | class Meta:
|
| 106 | + """ |
| 107 | + Specifies the model and fields to be used for filtering AssetAssignment instances. |
| 108 | +
|
| 109 | + Attributes: |
| 110 | + model (class): The model class AssetAssignment to be filtered. |
| 111 | + fields (list): The fields to include in the filter, referring to |
| 112 | + related AssetAssignment fields. |
| 113 | + """ |
| 114 | + |
57 | 115 | model = AssetAssignment
|
58 |
| - fields =['asset_id__asset_name','asset_id__asset_status',] |
59 |
| - def __init__(self,*args,**kwargs): |
60 |
| - super(CustomAssetFilter,self).__init__(*args,**kwargs) |
| 116 | + fields = [ |
| 117 | + "asset_id__asset_name", |
| 118 | + "asset_id__asset_status", |
| 119 | + ] |
| 120 | + |
| 121 | + def __init__(self, *args, **kwargs): |
| 122 | + super(CustomAssetFilter, self).__init__(*args, **kwargs) |
61 | 123 | for visible in self.form.visible_fields():
|
62 |
| - visible.field.widget.attrs['id'] = str(uuid.uuid4()) |
| 124 | + visible.field.widget.attrs["id"] = str(uuid.uuid4()) |
| 125 | + |
63 | 126 |
|
64 |
| - |
65 | 127 | class AssetRequestFilter(CustomFilterSet):
|
| 128 | + """ |
| 129 | + Custom filter set for AssetRequest instances. |
| 130 | + """ |
| 131 | + |
66 | 132 | class Meta:
|
| 133 | + """ |
| 134 | + Specifies the model and fields to be used for filtering AssetRequest instances. |
| 135 | +
|
| 136 | + Attributes: |
| 137 | + model (class): The model class AssetRequest to be filtered. |
| 138 | + fields (str): A special value "__all__" to include all fields of the model in the filter. |
| 139 | + """ |
| 140 | + |
67 | 141 | model = AssetRequest
|
68 |
| - fields = '__all__' |
69 |
| - def __init__(self,*args,**kwargs): |
70 |
| - super(AssetRequestFilter,self).__init__(*args,**kwargs) |
| 142 | + fields = "__all__" |
| 143 | + |
| 144 | + def __init__(self, *args, **kwargs): |
| 145 | + super(AssetRequestFilter, self).__init__(*args, **kwargs) |
71 | 146 | for visible in self.form.visible_fields():
|
72 |
| - visible.field.widget.attrs['id'] = str(uuid.uuid4()) |
73 |
| - |
| 147 | + visible.field.widget.attrs["id"] = str(uuid.uuid4()) |
| 148 | + |
| 149 | + |
74 | 150 | class AssetAllocationFilter(CustomFilterSet):
|
| 151 | + """ |
| 152 | + Custom filter set for AssetAllocation instances. |
| 153 | + """ |
| 154 | + |
75 | 155 | class Meta:
|
| 156 | + """ |
| 157 | + Specifies the model and fields to be used for filtering AssetAllocation instances. |
| 158 | +
|
| 159 | + Attributes: |
| 160 | + model (class): The model class AssetAssignment to be filtered. |
| 161 | + fields (str): A special value "__all__" to include all fields |
| 162 | + of the model in the filter. |
| 163 | + """ |
| 164 | + |
76 | 165 | model = AssetAssignment
|
77 |
| - fields = '__all__' |
78 |
| - def __init__(self,*args,**kwargs): |
79 |
| - super(AssetAllocationFilter,self).__init__(*args,**kwargs) |
| 166 | + fields = "__all__" |
| 167 | + |
| 168 | + def __init__(self, *args, **kwargs): |
| 169 | + super(AssetAllocationFilter, self).__init__(*args, **kwargs) |
80 | 170 | for visible in self.form.visible_fields():
|
81 |
| - visible.field.widget.attrs['id'] = str(uuid.uuid4()) |
82 |
| - |
| 171 | + visible.field.widget.attrs["id"] = str(uuid.uuid4()) |
| 172 | + |
| 173 | + |
83 | 174 | class AssetCategoryFilter(CustomFilterSet):
|
| 175 | + """ |
| 176 | + Custom filter set for AssetCategory instances. |
| 177 | + """ |
| 178 | + |
84 | 179 | class Meta:
|
| 180 | + """ |
| 181 | + Specifies the model and fields to be used for filtering AssetCategory instances. |
| 182 | +
|
| 183 | + Attributes: |
| 184 | + model (class): The model class AssetCategory to be filtered. |
| 185 | + fields (str): A special value "__all__" to include all fields |
| 186 | + of the model in the filter. |
| 187 | + """ |
| 188 | + |
85 | 189 | model = AssetCategory
|
86 |
| - fields = '__all__' |
87 |
| - def __init__(self,*args,**kwargs): |
88 |
| - super(AssetCategoryFilter,self).__init__(*args,**kwargs) |
| 190 | + fields = "__all__" |
| 191 | + |
| 192 | + def __init__(self, *args, **kwargs): |
| 193 | + super(AssetCategoryFilter, self).__init__(*args, **kwargs) |
89 | 194 | for visible in self.form.visible_fields():
|
90 |
| - visible.field.widget.attrs['id'] = str(uuid.uuid4()) |
91 |
| - |
| 195 | + visible.field.widget.attrs["id"] = str(uuid.uuid4()) |
0 commit comments