django-ninja-aio-crud 0.2.0__py3-none-any.whl → 0.2.2__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,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: django-ninja-aio-crud
3
- Version: 0.2.0
3
+ Version: 0.2.2
4
4
  Summary: Django Ninja AIO CRUD - Rest Framework
5
5
  Author: Giuseppe Casillo
6
6
  Requires-Python: >=3.10
@@ -130,20 +130,21 @@ class Foo(ModelSerializer):
130
130
 
131
131
  - post create method is a custom method that comes out to handle actions which will be excuted after that the object is created. It can be used, indeed, for example to handle custom fields' actions.
132
132
 
133
+
133
134
  ### APIViewSet
134
135
 
135
- - View class used to automatically generate CRUD views. in your views.py import APIViewSet and define your api using NinjaAPI class. As Parser and Render of the API you must use ninja_aio built-in classes which will serialize data using orjson.
136
+ - View class used to automatically generate CRUD views. in your views.py import APIViewSet and define your api using NinjaAIO class. NinjaAIO class uses built-in parser and renderer which use orjson for data serialization.
136
137
 
137
138
  ```Python
138
139
  # views.py
139
- from ninja import NinjAPI
140
+ from ninja_aio import NinjaAIO
140
141
  from ninja_aio.views import APIViewSet
141
142
  from ninja_aio.parsers import ORJSONParser
142
143
  from ninja_aio.renders import ORJSONRender
143
144
 
144
145
  from .models import Foo
145
146
 
146
- api = NinjaAPI(renderer=ORJSONRenderer(), parser=ORJSONParser())
147
+ api = NinjaAIO()
147
148
 
148
149
 
149
150
  class FooAPI(APIViewSet):
@@ -158,14 +159,15 @@ FooAPI().add_views_to_route()
158
159
 
159
160
  ```Python
160
161
  # views.py
161
- from ninja import NinjAPI, Schema
162
+ from ninja import Schema
163
+ from ninja_aio import NinjaAIO
162
164
  from ninja_aio.views import APIViewSet
163
165
  from ninja_aio.parsers import ORJSONParser
164
166
  from ninja_aio.renders import ORJSONRender
165
167
 
166
168
  from .models import Foo
167
169
 
168
- api = NinjaAPI(renderer=ORJSONRenderer(), parser=ORJSONParser())
170
+ api = NinjaAIO()
169
171
 
170
172
 
171
173
  class ExampleSchemaOut(Schema):
@@ -196,12 +198,13 @@ FooAPI().add_views_to_route()
196
198
 
197
199
  ```Python
198
200
  # views.py
199
- from ninja import NinjAPI, Schema
201
+ from ninja import Schema
202
+ from ninja_aio import NinjaAIO
200
203
  from ninja_aio.views import APIView
201
204
  from ninja_aio.parsers import ORJSONParser
202
205
  from ninja_aio.renders import ORJSONRender
203
206
 
204
- api = NinjaAPI(renderer=ORJSONRenderer(), parser=ORJSONParser())
207
+ api = NinjaAIO()
205
208
 
206
209
 
207
210
  class ExampleSchemaOut(Schema):
@@ -228,10 +231,7 @@ SumView().add_views_to_route()
228
231
  ```
229
232
 
230
233
  ### Relations
231
- - You can also set ForeignKey and OneToOne relations into serialization(reverse relations are supported too). Django ninja aio crud will serialize every of these relation automatically.
232
-
233
- > [!WARNING]
234
- > Only ForeignKey and OneToOne relations are supported for serialization, ManyToMany relations are not supported yet.
234
+ - You can also set ForeignKey, OneToOne and ManyToMany relations into serialization(reverse relations are supported too). Django ninja aio crud will serialize every of these relation automatically.
235
235
 
236
236
  - Define models:
237
237
 
@@ -270,14 +270,14 @@ class Foo(ModelSerializer):
270
270
 
271
271
  ```Python
272
272
  # views.py
273
- from ninja import NinjAPI
273
+ from ninja_aio import NinjaAIO
274
274
  from ninja_aio.views import APIViewSet
275
275
  from ninja_aio.parsers import ORJSONParser
276
276
  from ninja_aio.renders import ORJSONRender
277
277
 
278
278
  from .models import Foo, Bar
279
279
 
280
- api = NinjaAPI(renderer=ORJSONRenderer(), parser=ORJSONParser())
280
+ api = NinjaAIO()
281
281
 
282
282
 
283
283
  class FooAPI(APIViewSet):
@@ -336,14 +336,15 @@ class CustomJWTBearer(AsyncJWTBearer):
336
336
 
337
337
  ```Python
338
338
  # views.py
339
- from ninja import NinjAPI, Schema
339
+ from ninja import Schema
340
+ from ninja_aio import NinjaAIO
340
341
  from ninja_aio.views import APIViewSet, APIView
341
342
  from ninja_aio.parsers import ORJSONParser
342
343
  from ninja_aio.renders import ORJSONRender
343
344
 
344
345
  from .models import Foo
345
346
 
346
- api = NinjaAPI(renderer=ORJSONRenderer(), parser=ORJSONParser())
347
+ api = NinjaAIO()
347
348
 
348
349
 
349
350
  class FooAPI(APIViewSet):
@@ -1,11 +1,12 @@
1
- ninja_aio/__init__.py,sha256=jH3E7Jd2rKwk2oPVFUmf7KjKwvfN4qijMsg7QdGJ9SM,70
1
+ ninja_aio/__init__.py,sha256=j1Bp3GY1eYMxYEIv5dpP2Yj2BTv3Jn7ZnNjblltWvf8,120
2
+ ninja_aio/api.py,sha256=qEnCOvviJGfg28N9R2XWzykLGOFpJ0JMqe3rm4AYDkM,1564
2
3
  ninja_aio/auth.py,sha256=hGgiblvffpHmmakjaxdNT3G0tq39-Bvc5oLNqjn4qd8,1300
3
4
  ninja_aio/exceptions.py,sha256=PPNr1CdC7M7Kx1MtiBGuR4hATYQqEuvxCRU6uSG7rgM,543
4
- ninja_aio/models.py,sha256=xUZ0998bYkWumwZgnVC7P-CLdbfTzFflmJVbusU2UMY,11276
5
+ ninja_aio/models.py,sha256=5BIgW6U8Ghlfmkssg6zgSinTgOBwgSss-QkiyJUiM9I,11251
5
6
  ninja_aio/parsers.py,sha256=e_4lGCPV7zs-HTqtdJTc8yQD2KPAn9njbL8nF_Mmgkc,153
6
7
  ninja_aio/renders.py,sha256=wLQisbIsMJlGYJMO4Ud-qiaVCvz4yxiZ4mYommixAeQ,1453
7
8
  ninja_aio/schemas.py,sha256=EgRkfhnzZqwGvdBmqlZixMtMcoD1ZxV_qzJ3fmaAy20,113
8
9
  ninja_aio/views.py,sha256=ejn6adCe91YKo0-0bRePbuARHkz8YWMmP54WxQLLqxY,6322
9
- django_ninja_aio_crud-0.2.0.dist-info/WHEEL,sha256=EZbGkh7Ie4PoZfRQ8I0ZuP9VklN_TvcZ6DSE5Uar4z4,81
10
- django_ninja_aio_crud-0.2.0.dist-info/METADATA,sha256=HPVwB-vF9GmJALzGam7lD_V4WJHMRLswMouJl6-j1w4,10440
11
- django_ninja_aio_crud-0.2.0.dist-info/RECORD,,
10
+ django_ninja_aio_crud-0.2.2.dist-info/WHEEL,sha256=EZbGkh7Ie4PoZfRQ8I0ZuP9VklN_TvcZ6DSE5Uar4z4,81
11
+ django_ninja_aio_crud-0.2.2.dist-info/METADATA,sha256=hwRXbOz9QBLTuamrso72d1SW4HmKFw7UEmuEHqQS8xc,10135
12
+ django_ninja_aio_crud-0.2.2.dist-info/RECORD,,
ninja_aio/__init__.py CHANGED
@@ -1,3 +1,7 @@
1
1
  """ Django Ninja AIO CRUD - Rest Framework """
2
2
 
3
- __version__ = "0.2.0"
3
+ __version__ = "0.2.2"
4
+
5
+ from .api import NinjaAIO
6
+
7
+ __all__ = ["NinjaAIO"]
ninja_aio/api.py ADDED
@@ -0,0 +1,48 @@
1
+ from typing import Any, Sequence
2
+
3
+ from ninja.router import Router
4
+ from ninja.throttling import BaseThrottle
5
+ from ninja import NinjaAPI
6
+ from ninja.openapi.docs import DocsBase, Swagger
7
+ from ninja.constants import NOT_SET, NOT_SET_TYPE
8
+
9
+ from ninja_aio.parsers import ORJSONParser
10
+ from ninja_aio.renders import ORJSONRenderer
11
+
12
+
13
+ class NinjaAIO(NinjaAPI):
14
+ def __init__(
15
+ self,
16
+ title: str = "NinjaAPI",
17
+ version: str = "1.0.0",
18
+ description: str = "",
19
+ openapi_url: str | None = "/openapi.json",
20
+ docs: DocsBase = Swagger(),
21
+ docs_url: str | None = "/docs",
22
+ docs_decorator = None,
23
+ servers: list[dict[str, Any]] | None = None,
24
+ urls_namespace: str | None = None,
25
+ csrf: bool = False,
26
+ auth: Sequence[Any]| NOT_SET_TYPE = NOT_SET,
27
+ throttle: BaseThrottle | list[BaseThrottle] | NOT_SET_TYPE = NOT_SET,
28
+ default_router: Router | None = None,
29
+ openapi_extra: dict[str, Any] | None = None
30
+ ):
31
+ super().__init__(
32
+ title=title,
33
+ version=version,
34
+ description=description,
35
+ openapi_url=openapi_url,
36
+ docs=docs,
37
+ docs_url=docs_url,
38
+ docs_decorator=docs_decorator,
39
+ servers=servers,
40
+ urls_namespace=urls_namespace,
41
+ csrf=csrf,
42
+ auth=auth,
43
+ throttle=throttle,
44
+ default_router=default_router,
45
+ openapi_extra=openapi_extra,
46
+ renderer=ORJSONRenderer(),
47
+ parser=ORJSONParser(),
48
+ )
ninja_aio/models.py CHANGED
@@ -182,12 +182,9 @@ class ModelSerializer(models.Model):
182
182
  except Exception as exc:
183
183
  raise SerializeError({k: ". ".join(exc.args)}, 400)
184
184
  if isinstance(field_obj, models.ForeignKey):
185
- try:
186
- rel: ModelSerializer = await field_obj.related_model.get_object(
187
- request, v
188
- )
189
- except ObjectDoesNotExist:
190
- raise SerializeError({k: "not found"}, 404)
185
+ rel: ModelSerializer = await field_obj.related_model.get_object(
186
+ request, v
187
+ )
191
188
  payload |= {k: rel}
192
189
  new_payload = {k: v for k, v in payload.items() if k not in customs}
193
190
  return new_payload, customs
@@ -292,7 +289,11 @@ class ModelSerializer(models.Model):
292
289
  @classmethod
293
290
  async def read_s(cls, request: HttpRequest, obj: type["ModelSerializer"]):
294
291
  schema = cls.generate_read_s().from_orm(obj)
295
- return await cls.parse_output_data(request, schema)
292
+ try:
293
+ data = await cls.parse_output_data(request, schema)
294
+ except SerializeError as e:
295
+ return e.status_code, e.error
296
+ return data
296
297
 
297
298
  @classmethod
298
299
  async def update_s(cls, request: HttpRequest, data: Schema, pk: int | str):