django-small-view-set 0.2.4__tar.gz → 0.2.6__tar.gz

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,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: django-small-view-set
3
- Version: 0.2.4
3
+ Version: 0.2.6
4
4
  Summary: A lightweight Django ViewSet alternative with minimal abstraction.
5
5
  Home-page: https://github.com/nateonguitar/django-small-view-set
6
6
  License: MIT
@@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"
4
4
 
5
5
  [tool.poetry]
6
6
  name = "django-small-view-set"
7
- version = "0.2.4"
7
+ version = "0.2.6"
8
8
  description = "A lightweight Django ViewSet alternative with minimal abstraction."
9
9
  readme = "README.md"
10
10
  authors = ["Nate Brooks"]
@@ -10,6 +10,7 @@ def endpoint(
10
10
  func_name = func.__name__
11
11
  def sync_wrapper(viewset, *args, **kwargs):
12
12
  request = args[0]
13
+ args = args[1:]
13
14
  try:
14
15
  config: SmallViewSetConfig = getattr(settings, 'SMALL_VIEW_SET_CONFIG', SmallViewSetConfig())
15
16
  pre_response = config.options_and_head_handler(request, allowed_methods)
@@ -17,14 +18,15 @@ def endpoint(
17
18
  return pre_response
18
19
  pk = kwargs.pop('pk', None)
19
20
  if pk is None:
20
- return func(viewset, request=request)
21
+ return func(viewset, request=request, *args, **kwargs)
21
22
  else:
22
- return func(viewset, request=request, pk=pk)
23
+ return func(viewset, request=request, pk=pk, *args, **kwargs)
23
24
  except Exception as e:
24
25
  return config.exception_handler(request, func_name, e)
25
26
 
26
27
  async def async_wrapper(viewset, *args, **kwargs):
27
28
  request = args[0]
29
+ args = args[1:]
28
30
  try:
29
31
  config: SmallViewSetConfig = getattr(settings, 'SMALL_VIEW_SET_CONFIG', SmallViewSetConfig())
30
32
  pre_response = config.options_and_head_handler(request, allowed_methods)
@@ -32,9 +34,9 @@ def endpoint(
32
34
  return pre_response
33
35
  pk = kwargs.pop('pk', None)
34
36
  if pk is None:
35
- return await func(viewset, request=request)
37
+ return await func(viewset, request=request, *args, **kwargs)
36
38
  else:
37
- return await func(viewset, request=request, pk=pk)
39
+ return await func(viewset, request=request, pk=pk, *args, **kwargs)
38
40
  except Exception as e:
39
41
  return config.exception_handler(request, func_name, e)
40
42
 
@@ -81,7 +81,83 @@ class SmallViewSet:
81
81
  pass
82
82
 
83
83
  @endpoint(allowed_methods=['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'HEAD'])
84
- async def default_router(self, request: Request, pk=None, *args, **kwargs):
84
+ async def default_router_async(self, request: Request, pk=None, *args, **kwargs):
85
+ """
86
+ This method routes requests to the appropriate method based on the HTTP method and presence of a primary key (pk).
87
+
88
+ It also handles errors and returns appropriate JSON responses by using the decorator @endpoint(allowed_method=[]).
89
+
90
+ GET/POST for collection endpoints and GET/PUT/PATCH/DELETE for detail endpoints.
91
+
92
+ Example:
93
+ ```
94
+ # Note: AppViewSet is a subclass of SmallViewSet with overridden protect methods with more specific logic.
95
+
96
+ class CommentViewSet(AppViewSet):
97
+ def urlpatterns(self):
98
+ return [
99
+ path('api/comments/', self.default_router_async, name='comments_collection'),
100
+ path('api/comments/<int:pk>/', self.default_router_async, name='comments_detail'),
101
+ path('api/comments/<int:pk>/custom_put/', self.custom_put, name='comments_custom_put_detail'),
102
+ ]
103
+
104
+ @endpoint(allowed_method=['POST'])
105
+ def create(self, request: Request):
106
+ self.protect_create(request)
107
+ . . .
108
+
109
+ @endpoint(allowed_method=['PUT', 'PATCH'])
110
+ def update(self, request: Request, pk: int):
111
+ self.protect_update(request)
112
+ . . .
113
+
114
+ @endpoint(allowed_method=['PUT'])
115
+ def custom_put(self, request: Request, pk: int):
116
+ self.protect_update(request)
117
+ . . .
118
+
119
+ @endpoint(allowed_method=['GET'])
120
+ @disable_endpoint
121
+ def some_disabled_endpoint(self, request: Request):
122
+ self.protect_retrieve(request)
123
+ . . .
124
+ ```
125
+ """
126
+ func = None
127
+ if pk is None:
128
+ if request.method == 'GET':
129
+ if hasattr(self, 'list'):
130
+ func = self.list
131
+
132
+ elif request.method == 'POST':
133
+ if hasattr(self, 'create'):
134
+ func = self.create
135
+ else:
136
+ if request.method == 'GET':
137
+ if hasattr(self, 'retrieve'):
138
+ func = self.retrieve
139
+
140
+ elif request.method == 'PUT':
141
+ if hasattr(self, 'put'):
142
+ func = self.put
143
+
144
+ elif request.method == 'PATCH':
145
+ if hasattr(self, 'patch'):
146
+ func = self.patch
147
+
148
+ elif request.method == 'DELETE':
149
+ if hasattr(self, 'delete'):
150
+ func = self.delete
151
+
152
+ if func is None:
153
+ raise MethodNotAllowed(request.method)
154
+ if pk is not None:
155
+ kwargs['pk'] = pk
156
+ return await func(request, *args, **kwargs)
157
+
158
+
159
+ @endpoint(allowed_methods=['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'HEAD'])
160
+ def default_router(self, request: Request, pk=None, *args, **kwargs):
85
161
  """
86
162
  This method routes requests to the appropriate method based on the HTTP method and presence of a primary key (pk).
87
163
 
@@ -151,11 +227,6 @@ class SmallViewSet:
151
227
 
152
228
  if func is None:
153
229
  raise MethodNotAllowed(request.method)
154
-
155
230
  if pk is not None:
156
231
  kwargs['pk'] = pk
157
-
158
- if inspect.iscoroutinefunction(func):
159
- return await func(request, *args, **kwargs)
160
- else:
161
- return func(request, *args, **kwargs)
232
+ return func(request, *args, **kwargs)