djresttoolkit 1.0.0__py3-none-any.whl → 1.1.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.
- djresttoolkit/views/mixins/__init__.py +2 -5
- djresttoolkit/views/mixins/_retrieve_object_mixin.py +28 -10
- {djresttoolkit-1.0.0.dist-info → djresttoolkit-1.1.0.dist-info}/METADATA +9 -7
- {djresttoolkit-1.0.0.dist-info → djresttoolkit-1.1.0.dist-info}/RECORD +7 -7
- {djresttoolkit-1.0.0.dist-info → djresttoolkit-1.1.0.dist-info}/WHEEL +0 -0
- {djresttoolkit-1.0.0.dist-info → djresttoolkit-1.1.0.dist-info}/entry_points.txt +0 -0
- {djresttoolkit-1.0.0.dist-info → djresttoolkit-1.1.0.dist-info}/licenses/LICENSE +0 -0
@@ -1,19 +1,20 @@
|
|
1
1
|
from typing import Any
|
2
|
-
from django.db.models import Model, QuerySet
|
3
|
-
|
4
|
-
|
5
|
-
class QuerysetNotDefinedError(Exception):
|
6
|
-
"""Exception raised when the `queryset` attribute is not set in the class."""
|
7
2
|
|
8
|
-
|
3
|
+
from django.core.exceptions import ImproperlyConfigured
|
4
|
+
from django.db.models import Model, QuerySet
|
5
|
+
from django.http import Http404
|
9
6
|
|
10
7
|
|
11
8
|
class RetrieveObjectMixin[T: Model]:
|
12
9
|
"""
|
13
|
-
|
10
|
+
Retrieve a single model object by filters.
|
14
11
|
|
15
12
|
Requires the `queryset` attribute to be set in the class that inherits this mixin.
|
16
13
|
|
14
|
+
Raises `Http404` when the object is missing.
|
15
|
+
|
16
|
+
This works in both Django views and DRF views.
|
17
|
+
|
17
18
|
Example:
|
18
19
|
```
|
19
20
|
class MyView(RetrieveModelMixin[Book], APIView):
|
@@ -27,15 +28,32 @@ class RetrieveObjectMixin[T: Model]:
|
|
27
28
|
|
28
29
|
queryset: QuerySet[T] | None = None
|
29
30
|
|
30
|
-
def get_object(self, **filters: Any) -> T
|
31
|
+
def get_object(self, **filters: Any) -> T:
|
31
32
|
"""Retrieve a model object based on provided filters."""
|
32
33
|
|
33
34
|
if self.queryset is None:
|
34
|
-
raise
|
35
|
+
raise ImproperlyConfigured(
|
35
36
|
"Queryset attribute is not set in the class.",
|
36
37
|
)
|
37
38
|
|
38
39
|
try:
|
39
40
|
return self.queryset.get(**filters)
|
40
41
|
except self.queryset.model.DoesNotExist:
|
41
|
-
|
42
|
+
raise Http404(self.not_found_detail())
|
43
|
+
|
44
|
+
def not_found_detail(self) -> dict[str, str] | str:
|
45
|
+
"""
|
46
|
+
Hook for customizing the 404 message.
|
47
|
+
Can be overridden per view.
|
48
|
+
"""
|
49
|
+
|
50
|
+
if self.queryset is None:
|
51
|
+
raise ImproperlyConfigured(
|
52
|
+
"Queryset attribute is not set in the class.",
|
53
|
+
)
|
54
|
+
|
55
|
+
verbose_name = self.queryset.model._meta.verbose_name
|
56
|
+
model_name = (
|
57
|
+
verbose_name.title() if verbose_name else self.queryset.model.__name__
|
58
|
+
)
|
59
|
+
return f"The requested {model_name} was not found."
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: djresttoolkit
|
3
|
-
Version: 1.
|
3
|
+
Version: 1.1.0
|
4
4
|
Summary: A collection of Django and DRF utilities to simplify API development.
|
5
5
|
Project-URL: Homepage, https://github.com/shaileshpandit141/djresttoolkit
|
6
6
|
Project-URL: Documentation, https://shaileshpandit141.github.io/djresttoolkit
|
@@ -240,7 +240,7 @@ or
|
|
240
240
|
Flushed 120 records from all models and reset IDs.
|
241
241
|
```
|
242
242
|
|
243
|
-
###
|
243
|
+
### 3. BaseEnvConfig — API Reference
|
244
244
|
|
245
245
|
```python
|
246
246
|
from djresttoolkit.envconfig import BaseEnvConfig
|
@@ -801,6 +801,7 @@ Retrieve a single model object using the provided filter criteria.
|
|
801
801
|
|
802
802
|
```python
|
803
803
|
from rest_framework.views import APIView
|
804
|
+
from rest_framework.response import Respone
|
804
805
|
from django.http import JsonResponse
|
805
806
|
from myapp.models import Book
|
806
807
|
from djresttoolkit.mixins import RetrieveObjectMixin
|
@@ -808,17 +809,18 @@ from djresttoolkit.mixins import RetrieveObjectMixin
|
|
808
809
|
class BookDetailView(RetrieveObjectMixin[Book], APIView):
|
809
810
|
queryset = Book.objects.all()
|
810
811
|
|
811
|
-
def
|
812
|
+
def not_found_detail(self) -> dict[str, str] | str:
|
813
|
+
return "The requested Book was not found."
|
814
|
+
|
815
|
+
def get(self, request, *args, **kwargs) -> Respone:
|
812
816
|
book = self.get_object(id=kwargs["id"])
|
813
|
-
|
814
|
-
return JsonResponse({"title": book.title, "author": book.author})
|
815
|
-
return JsonResponse({"detail": "Not found"}, status=404)
|
817
|
+
return Respone({"title": book.title, "author": book.author})
|
816
818
|
```
|
817
819
|
|
818
820
|
#### Features of Retrieve Object Mixin
|
819
821
|
|
820
822
|
- Simplifies object retrieval in class-based views or DRF views.
|
821
|
-
-
|
823
|
+
- Raise `http404` if requested resource does not extst.
|
822
824
|
- Works with any Django model and queryset.
|
823
825
|
|
824
826
|
### 13. build_absolute_uri — API Reference
|
@@ -48,10 +48,10 @@ djresttoolkit/views/_apiviews/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5N
|
|
48
48
|
djresttoolkit/views/_apiviews/_choice_fields_apiview.py,sha256=zABPgqxMVaWd814B_sC64bWL61fDJkyYQZmJXQCa6Xc,1395
|
49
49
|
djresttoolkit/views/_exceptions/__init__.py,sha256=DrCUxuPNyBR4WhzNutn5HDxLa--q51ykIxSG7_bFsOI,83
|
50
50
|
djresttoolkit/views/_exceptions/_exception_handler.py,sha256=_o7If47bzWLl57LeSXSWsIDsJGo2RIpwYAwNQ-hsHVY,2839
|
51
|
-
djresttoolkit/views/mixins/__init__.py,sha256=
|
52
|
-
djresttoolkit/views/mixins/_retrieve_object_mixin.py,sha256=
|
53
|
-
djresttoolkit-1.
|
54
|
-
djresttoolkit-1.
|
55
|
-
djresttoolkit-1.
|
56
|
-
djresttoolkit-1.
|
57
|
-
djresttoolkit-1.
|
51
|
+
djresttoolkit/views/mixins/__init__.py,sha256=mHD49OUxuJ9v81tGfM0hLnUJuJlYi7E-5cTVdplh-vs,91
|
52
|
+
djresttoolkit/views/mixins/_retrieve_object_mixin.py,sha256=v7CQDUkRWjtevFZnAYRBdDl7wcfYWF3evWoKWHAcckA,1749
|
53
|
+
djresttoolkit-1.1.0.dist-info/METADATA,sha256=0Ln7HqEDc0-fHHiJZhZv0uipTH5TC9IsoWH9dWCswvI,31878
|
54
|
+
djresttoolkit-1.1.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
55
|
+
djresttoolkit-1.1.0.dist-info/entry_points.txt,sha256=YMhfTF-7mYppO8QqqWnvR_hyMWvoYxD6XI94_ViFu3k,60
|
56
|
+
djresttoolkit-1.1.0.dist-info/licenses/LICENSE,sha256=8-oZM3yuuTRjySMbVKX9YXYA7Y4M_KhQNBYXPFjeWUo,1074
|
57
|
+
djresttoolkit-1.1.0.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|