django-ninja-aio-crud 0.10.2__py3-none-any.whl → 2.4.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- django_ninja_aio_crud-2.4.0.dist-info/METADATA +382 -0
- django_ninja_aio_crud-2.4.0.dist-info/RECORD +29 -0
- ninja_aio/__init__.py +1 -1
- ninja_aio/api.py +24 -2
- ninja_aio/auth.py +186 -4
- ninja_aio/decorators/__init__.py +23 -0
- ninja_aio/decorators/operations.py +9 -0
- ninja_aio/decorators/views.py +219 -0
- ninja_aio/exceptions.py +36 -1
- ninja_aio/factory/__init__.py +3 -0
- ninja_aio/factory/operations.py +296 -0
- ninja_aio/helpers/__init__.py +0 -0
- ninja_aio/helpers/api.py +506 -0
- ninja_aio/helpers/query.py +108 -0
- ninja_aio/models/__init__.py +4 -0
- ninja_aio/models/serializers.py +738 -0
- ninja_aio/models/utils.py +894 -0
- ninja_aio/renders.py +26 -26
- ninja_aio/schemas/__init__.py +23 -0
- ninja_aio/{schemas.py → schemas/api.py} +0 -5
- ninja_aio/schemas/generics.py +5 -0
- ninja_aio/schemas/helpers.py +170 -0
- ninja_aio/types.py +3 -1
- ninja_aio/views/__init__.py +3 -0
- ninja_aio/views/api.py +582 -0
- ninja_aio/views/mixins.py +275 -0
- django_ninja_aio_crud-0.10.2.dist-info/METADATA +0 -526
- django_ninja_aio_crud-0.10.2.dist-info/RECORD +0 -14
- ninja_aio/models.py +0 -549
- ninja_aio/views.py +0 -522
- {django_ninja_aio_crud-0.10.2.dist-info → django_ninja_aio_crud-2.4.0.dist-info}/WHEEL +0 -0
- {django_ninja_aio_crud-0.10.2.dist-info → django_ninja_aio_crud-2.4.0.dist-info}/licenses/LICENSE +0 -0
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
from ninja_aio.views.api import APIViewSet
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class IcontainsFilterViewSetMixin(APIViewSet):
|
|
5
|
+
"""
|
|
6
|
+
Mixin providing a convenience method to apply case-insensitive substring filters
|
|
7
|
+
to a Django queryset based on request query parameters.
|
|
8
|
+
|
|
9
|
+
This mixin is intended for use with viewsets that support asynchronous handling
|
|
10
|
+
and dynamic filtering. It converts string-type filter values into Django ORM
|
|
11
|
+
`__icontains` lookups, enabling partial, case-insensitive matches.
|
|
12
|
+
|
|
13
|
+
Usage:
|
|
14
|
+
- Include this mixin in a viewset class that exposes a queryset and
|
|
15
|
+
passes a dictionary of filters (e.g., from request query params) to
|
|
16
|
+
`query_params_handler`.
|
|
17
|
+
- Only string values are considered; non-string values are ignored.
|
|
18
|
+
|
|
19
|
+
Example:
|
|
20
|
+
filters = {"name": "john", "email": "example.com", "age": 30}
|
|
21
|
+
# Resulting queryset filter:
|
|
22
|
+
# queryset.filter(name__icontains="john", email__icontains="example.com")
|
|
23
|
+
# Note: "age" is ignored because its value is not a string.
|
|
24
|
+
|
|
25
|
+
Apply `__icontains` filters to the provided queryset based on the given filter
|
|
26
|
+
dictionary.
|
|
27
|
+
|
|
28
|
+
Parameters:
|
|
29
|
+
queryset (django.db.models.QuerySet):
|
|
30
|
+
The base queryset to filter.
|
|
31
|
+
filters (dict[str, Any]):
|
|
32
|
+
A mapping of field names to desired filter values. Only entries with
|
|
33
|
+
string values will be transformed into `field__icontains=value`
|
|
34
|
+
filters. Non-string values are ignored.
|
|
35
|
+
|
|
36
|
+
Returns:
|
|
37
|
+
django.db.models.QuerySet:
|
|
38
|
+
A queryset filtered with `__icontains` lookups for all string-valued
|
|
39
|
+
keys in `filters`.
|
|
40
|
+
|
|
41
|
+
Notes:
|
|
42
|
+
- This method is asynchronous to align with async view workflows, but it
|
|
43
|
+
performs a synchronous queryset filter call typical in Django ORM usage.
|
|
44
|
+
- Ensure that the fields referenced in `filters` exist on the model
|
|
45
|
+
associated with `queryset`, otherwise a FieldError may be raised at
|
|
46
|
+
evaluation time.
|
|
47
|
+
"""
|
|
48
|
+
|
|
49
|
+
async def query_params_handler(self, queryset, filters):
|
|
50
|
+
"""
|
|
51
|
+
Apply icontains filter to the queryset based on provided filters.
|
|
52
|
+
"""
|
|
53
|
+
base_qs = await super().query_params_handler(queryset, filters)
|
|
54
|
+
return base_qs.filter(
|
|
55
|
+
**{
|
|
56
|
+
f"{key}__icontains": value
|
|
57
|
+
for key, value in filters.items()
|
|
58
|
+
if isinstance(value, str)
|
|
59
|
+
}
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class BooleanFilterViewSetMixin(APIViewSet):
|
|
64
|
+
"""
|
|
65
|
+
Mixin providing boolean-based filtering for Django QuerySets.
|
|
66
|
+
|
|
67
|
+
This mixin defines a helper to apply boolean filters from a dictionary of query
|
|
68
|
+
parameters, selecting only those values that are strictly boolean (True/False)
|
|
69
|
+
and ignoring any non-boolean entries. It is intended to be used with viewsets
|
|
70
|
+
that expose queryable endpoints.
|
|
71
|
+
|
|
72
|
+
Methods:
|
|
73
|
+
query_params_handler(queryset, filters):
|
|
74
|
+
Apply boolean filters to the given queryset based on the provided
|
|
75
|
+
dictionary. Only keys with boolean values are included in the filter.
|
|
76
|
+
|
|
77
|
+
Parameters:
|
|
78
|
+
queryset (QuerySet): A Django QuerySet to be filtered.
|
|
79
|
+
filters (dict): A mapping of field names to potential filter values.
|
|
80
|
+
|
|
81
|
+
Returns:
|
|
82
|
+
QuerySet: A new QuerySet filtered by the boolean entries in `filters`.
|
|
83
|
+
|
|
84
|
+
Notes:
|
|
85
|
+
- Non-boolean values in `filters` are ignored.
|
|
86
|
+
- Keys should correspond to valid model fields or lookups.
|
|
87
|
+
- This method does not mutate the original queryset; it returns a
|
|
88
|
+
filtered clone.
|
|
89
|
+
"""
|
|
90
|
+
|
|
91
|
+
async def query_params_handler(self, queryset, filters):
|
|
92
|
+
"""
|
|
93
|
+
Apply boolean filter to the queryset based on provided filters.
|
|
94
|
+
"""
|
|
95
|
+
base_qs = await super().query_params_handler(queryset, filters)
|
|
96
|
+
return base_qs.filter(
|
|
97
|
+
**{key: value for key, value in filters.items() if isinstance(value, bool)}
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
class NumericFilterViewSetMixin(APIViewSet):
|
|
102
|
+
"""
|
|
103
|
+
Mixin providing numeric filtering for Django QuerySets.
|
|
104
|
+
|
|
105
|
+
This mixin defines a helper to apply numeric filters from a dictionary of query
|
|
106
|
+
parameters, selecting only those values that are of numeric type (int or float)
|
|
107
|
+
and ignoring any non-numeric entries. It is intended to be used with viewsets
|
|
108
|
+
that expose queryable endpoints.
|
|
109
|
+
|
|
110
|
+
Methods:
|
|
111
|
+
query_params_handler(queryset, filters):
|
|
112
|
+
Apply numeric filters to the given queryset based on the provided
|
|
113
|
+
dictionary. Only keys with numeric values are included in the filter.
|
|
114
|
+
|
|
115
|
+
Parameters:
|
|
116
|
+
queryset (QuerySet): A Django QuerySet to be filtered.
|
|
117
|
+
filters (dict): A mapping of field names to potential filter values.
|
|
118
|
+
|
|
119
|
+
Returns:
|
|
120
|
+
QuerySet: A new QuerySet filtered by the numeric entries in `filters`.
|
|
121
|
+
|
|
122
|
+
Notes:
|
|
123
|
+
- Non-numeric values in `filters` are ignored.
|
|
124
|
+
- Keys should correspond to valid model fields or lookups.
|
|
125
|
+
- This method does not mutate the original queryset; it returns a
|
|
126
|
+
filtered clone.
|
|
127
|
+
"""
|
|
128
|
+
|
|
129
|
+
async def query_params_handler(self, queryset, filters):
|
|
130
|
+
"""
|
|
131
|
+
Apply numeric filter to the queryset based on provided filters.
|
|
132
|
+
"""
|
|
133
|
+
base_qs = await super().query_params_handler(queryset, filters)
|
|
134
|
+
return base_qs.filter(
|
|
135
|
+
**{
|
|
136
|
+
key: value
|
|
137
|
+
for key, value in filters.items()
|
|
138
|
+
if isinstance(value, (int, float))
|
|
139
|
+
}
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
class DateFilterViewSetMixin(APIViewSet):
|
|
144
|
+
"""
|
|
145
|
+
Mixin enabling date/datetime-based filtering for Django QuerySets.
|
|
146
|
+
|
|
147
|
+
Purpose:
|
|
148
|
+
- Apply dynamic date/datetime filters based on incoming query parameters.
|
|
149
|
+
- Support customizable comparison operators via `_compare_attr` (e.g., "__gt", "__lt", "__gte", "__lte").
|
|
150
|
+
|
|
151
|
+
Behavior:
|
|
152
|
+
- Filters only entries whose values implement `isoformat` (dates or datetimes).
|
|
153
|
+
- Builds lookups as "<field><_compare_attr>" with the provided value.
|
|
154
|
+
|
|
155
|
+
Attributes:
|
|
156
|
+
- _compare_attr (str): Django ORM comparison operator suffix to append to field names.
|
|
157
|
+
|
|
158
|
+
Notes:
|
|
159
|
+
- Ensure provided filter values are compatible with the target model fields.
|
|
160
|
+
- Subclasses should set `_compare_attr` to control comparison semantics.
|
|
161
|
+
"""
|
|
162
|
+
|
|
163
|
+
_compare_attr: str = ""
|
|
164
|
+
|
|
165
|
+
async def query_params_handler(self, queryset, filters):
|
|
166
|
+
"""
|
|
167
|
+
Apply date/datetime filters using `_compare_attr`.
|
|
168
|
+
|
|
169
|
+
- Delegates to `super().query_params_handler` first.
|
|
170
|
+
- Applies filters for keys whose values implement `isoformat`.
|
|
171
|
+
|
|
172
|
+
Returns:
|
|
173
|
+
- QuerySet filtered with lookups in the form: field<_compare_attr>=value.
|
|
174
|
+
"""
|
|
175
|
+
base_qs = await super().query_params_handler(queryset, filters)
|
|
176
|
+
return base_qs.filter(
|
|
177
|
+
**{
|
|
178
|
+
f"{key}{self._compare_attr}": value
|
|
179
|
+
for key, value in filters.items()
|
|
180
|
+
if hasattr(value, "isoformat")
|
|
181
|
+
}
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
class GreaterDateFilterViewSetMixin(DateFilterViewSetMixin):
|
|
186
|
+
"""
|
|
187
|
+
Mixin that configures date filtering to return records with dates strictly greater than a given value.
|
|
188
|
+
This class extends DateFilterViewSetMixin and sets the internal comparison attribute to `__gt`,
|
|
189
|
+
ensuring that any date-based filtering uses the "greater than" operation.
|
|
190
|
+
Attributes:
|
|
191
|
+
_compare_attr (str): The Django ORM comparison operator used for filtering, set to "__gt".
|
|
192
|
+
Usage:
|
|
193
|
+
- Include this mixin in a ViewSet to filter queryset results where the specified date field is
|
|
194
|
+
greater than the provided date value.
|
|
195
|
+
- Typically used in endpoints that need to fetch items created/updated after a certain timestamp.
|
|
196
|
+
Example:
|
|
197
|
+
class MyViewSet(GreaterDateFilterViewSetMixin, ModelViewSet):
|
|
198
|
+
date_filter_field = "created_at"
|
|
199
|
+
...
|
|
200
|
+
# Filtering will apply: queryset.filter(created_at__gt=<provided_date>)
|
|
201
|
+
"""
|
|
202
|
+
|
|
203
|
+
_compare_attr = "__gt"
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
class LessDateFilterViewSetMixin(DateFilterViewSetMixin):
|
|
207
|
+
"""
|
|
208
|
+
Mixin that configures date filtering to return records with dates strictly less than a given value.
|
|
209
|
+
This class extends DateFilterViewSetMixin and sets the internal comparison attribute to `__lt`,
|
|
210
|
+
ensuring that any date-based filtering uses the "less than" operation.
|
|
211
|
+
Attributes:
|
|
212
|
+
_compare_attr (str): The Django ORM comparison operator used for filtering, set to "__lt".
|
|
213
|
+
Usage:
|
|
214
|
+
- Include this mixin in a ViewSet to filter queryset results where the specified date field is
|
|
215
|
+
less than the provided date value.
|
|
216
|
+
- Typically used in endpoints that need to fetch items created/updated before a certain timestamp.
|
|
217
|
+
Example:
|
|
218
|
+
class MyViewSet(LessDateFilterViewSetMixin, ModelViewSet):
|
|
219
|
+
date_filter_field = "created_at"
|
|
220
|
+
...
|
|
221
|
+
# Filtering will apply: queryset.filter(created_at__lt=<provided_date>)
|
|
222
|
+
"""
|
|
223
|
+
|
|
224
|
+
_compare_attr = "__lt"
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
class GreaterEqualDateFilterViewSetMixin(DateFilterViewSetMixin):
|
|
228
|
+
"""
|
|
229
|
+
Mixin for date-based filtering that uses a "greater than or equal to" comparison.
|
|
230
|
+
|
|
231
|
+
This mixin extends DateFilterViewSetMixin by setting the internal comparison
|
|
232
|
+
attribute to "__gte", enabling querysets to be filtered to include records whose
|
|
233
|
+
date or datetime fields are greater than or equal to the provided value.
|
|
234
|
+
|
|
235
|
+
Intended Use:
|
|
236
|
+
- Apply to Django Ninja or DRF viewsets that support date filtering.
|
|
237
|
+
- Combine with DateFilterViewSetMixin to standardize date filter behavior.
|
|
238
|
+
|
|
239
|
+
Behavior:
|
|
240
|
+
- When a date filter parameter is present, the queryset is filtered using
|
|
241
|
+
Django's "__gte" lookup, e.g., MyModel.objects.filter(created_at__gte=value).
|
|
242
|
+
|
|
243
|
+
Attributes:
|
|
244
|
+
- _compare_attr: str
|
|
245
|
+
The Django ORM comparison operator used for filtering; set to "__gte".
|
|
246
|
+
|
|
247
|
+
Notes:
|
|
248
|
+
- Ensure the target field and provided filter value are compatible (date or datetime).
|
|
249
|
+
- Timezone-aware comparisons should be handled consistently within the project settings.
|
|
250
|
+
"""
|
|
251
|
+
|
|
252
|
+
_compare_attr = "__gte"
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
class LessEqualDateFilterViewSetMixin(DateFilterViewSetMixin):
|
|
256
|
+
"""
|
|
257
|
+
Mixin for date-based filtering that uses a "less than or equal to" comparison.
|
|
258
|
+
This mixin extends DateFilterViewSetMixin by setting the internal comparison
|
|
259
|
+
attribute to "__lte", enabling querysets to be filtered to include records whose
|
|
260
|
+
date or datetime fields are less than or equal to the provided value.
|
|
261
|
+
Intended Use:
|
|
262
|
+
- Apply to Django Ninja or DRF viewsets that support date filtering.
|
|
263
|
+
- Combine with DateFilterViewSetMixin to standardize date filter behavior.
|
|
264
|
+
Behavior:
|
|
265
|
+
- When a date filter parameter is present, the queryset is filtered using
|
|
266
|
+
Django's "__lte" lookup, e.g., MyModel.objects.filter(created_at__lte=value).
|
|
267
|
+
Attributes:
|
|
268
|
+
- _compare_attr: str
|
|
269
|
+
The Django ORM comparison operator used for filtering; set to "__lte".
|
|
270
|
+
Notes:
|
|
271
|
+
- Ensure the target field and provided filter value are compatible (date or datetime).
|
|
272
|
+
- Timezone-aware comparisons should be handled consistently within the project settings.
|
|
273
|
+
"""
|
|
274
|
+
|
|
275
|
+
_compare_attr = "__lte"
|