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.
@@ -1,6 +1,3 @@
1
- from ._retrieve_object_mixin import RetrieveObjectMixin, QuerysetNotDefinedError
1
+ from ._retrieve_object_mixin import RetrieveObjectMixin
2
2
 
3
- __all__ = [
4
- "RetrieveObjectMixin",
5
- "QuerysetNotDefinedError",
6
- ]
3
+ __all__ = ["RetrieveObjectMixin"]
@@ -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
- pass
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
- Mixin to provide a method for retrieving a single model object by filters.
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 | None:
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 QuerysetNotDefinedError(
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
- return None
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.0.0
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
- ### 3. BaseEnvConfig — API Reference
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 get(self, request, *args, **kwargs):
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
- if book:
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
- - Returns `None` instead of raising `DoesNotExist`, making error handling easier.
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=K-1tk5d8tCVViMynw5DdffJ3Oo5uHpEx32E3_4X2UxM,154
52
- djresttoolkit/views/mixins/_retrieve_object_mixin.py,sha256=Q9znYPb07YXXUhsL7VIrk3BC-zDwjOhwLJKe2GPJ-k0,1155
53
- djresttoolkit-1.0.0.dist-info/METADATA,sha256=pyiY9lXJeoLC9xHgZSuE4f72eRkugl0Zkt00hWaDrSU,31832
54
- djresttoolkit-1.0.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
55
- djresttoolkit-1.0.0.dist-info/entry_points.txt,sha256=YMhfTF-7mYppO8QqqWnvR_hyMWvoYxD6XI94_ViFu3k,60
56
- djresttoolkit-1.0.0.dist-info/licenses/LICENSE,sha256=8-oZM3yuuTRjySMbVKX9YXYA7Y4M_KhQNBYXPFjeWUo,1074
57
- djresttoolkit-1.0.0.dist-info/RECORD,,
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,,