-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/breatheco-de/capy-core
- Loading branch information
Showing
19 changed files
with
337 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,7 @@ | |
|
||
from celery import Celery | ||
|
||
|
||
app = Celery(task_always_eager=True) | ||
|
||
|
||
|
||
pytest_plugins = ("capyc.pytest.rest_framework",) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# v1.1.1 | ||
|
||
- Add some fixes to Capy Serializers. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Cache control | ||
|
||
Capy Serializers supports cache control. You can set the `cache_control` attribute in the serializer to define the [cache control](https://developer.mozilla.org/es/docs/Web/HTTP/Headers/Cache-Control) headers, which can include directives such as `no-cache`, `no-store`, `must-revalidate`, `max-age`, and `public` or `private` to control how responses are cached by browsers and intermediate caches. | ||
|
||
## Example | ||
|
||
```python | ||
import capyc.django.serializer as capy | ||
|
||
class PermissionSerializer(capy.Serializer): | ||
cache_control = f"max-age={60 * 60}" # 1 hour | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Cache ttl | ||
|
||
Capy Serializers supports set a custom time to live for the cache, you can set `ttl` attribute in the serializer to set the cache ttl, after this time the cache will be invalidated. This value is in seconds. | ||
|
||
## Example | ||
|
||
```python | ||
import capyc.django.serializer as capy | ||
|
||
class PermissionSerializer(capy.Serializer): | ||
ttl = 60 * 60 # 1 hour | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Cache | ||
|
||
Capy Serializers supports caching out of the box, with caching enabled by default. This package utilizes [django-redis](https://github.com/jazzband/django-redis). | ||
|
||
## Settings | ||
|
||
```python | ||
CAPYC = { | ||
"cache": { | ||
"enabled": True, | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Compression | ||
|
||
Capy Serializers supports compression out of the box, with compression enabled by default. This package supports `gzip`, `deflate`, `brotli`, and `zstandard`. | ||
|
||
## Settings | ||
|
||
```python | ||
CAPYC = { | ||
"compression": { | ||
"enabled": True, | ||
"min_kb_size": 10, | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Field sets | ||
|
||
Capy Serializers supports field sets, you can set `sets` attribute in the serializer, by default the fields provided in `default` set always are included in the response. | ||
|
||
## Request | ||
|
||
```http | ||
GET /api/v1/users?sets=default,custom | ||
``` | ||
|
||
## Serializer | ||
|
||
```python | ||
import capyc.django.serializer as capy | ||
|
||
class PermissionSerializer(capy.Serializer): | ||
fields = { | ||
"default": ("id", "name"), | ||
"extra": ("codename", "content_type"), | ||
"ids": ("content_type", "groups"), | ||
"lists": ("groups",), | ||
"expand_ids": ("content_type[]",), | ||
"expand_lists": ("groups[]",), | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Fields and filters overrides. | ||
|
||
Capy Serializers supports overwrites fields names. | ||
|
||
## Example | ||
|
||
```python | ||
import capyc.django.serializer as capy | ||
|
||
class PermissionSerializer(capy.Serializer): | ||
model = Permission | ||
fields = { | ||
"default": ("id", "name"), | ||
"lists": ("groups",), | ||
"expand_lists": ("groups[]",), | ||
} | ||
rewrites = { | ||
"group_set": "groups", | ||
} | ||
filters = ("groups",) | ||
groups = GroupSerializer | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Help | ||
|
||
You can get info about the available field sets and filters by using the `help` query param. | ||
|
||
```http | ||
GET /api/v1/users?help | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Capy Serializers | ||
|
||
Capy Serializers is a propose to replace DRF's Serializer, Serpy, and API View Extensions, the main difference respect to them is that Capy Serializers returns a Django Rest Framework compatible response. | ||
|
||
## Usage | ||
|
||
```python | ||
import capyc.django.serializer as capy | ||
|
||
class PermissionSerializer(capy.Serializer): | ||
model = Permission | ||
path = "/permission" | ||
fields = { | ||
"default": ("id", "name"), | ||
"extra": ("codename",), | ||
"ids": ("content_type",), | ||
"lists": ("groups",), | ||
"expand_ids": ("content_type[]",), | ||
"expand_lists": ("groups[]",), | ||
} | ||
rewrites = { | ||
"group_set": "groups", | ||
} | ||
filters = ("name", "codename", "content_type", "groups") | ||
depth = 2 | ||
content_type = ContentTypeSerializer | ||
groups = GroupSerializer | ||
|
||
``` | ||
|
||
## Features | ||
|
||
- [Field sets](field-sets.md). | ||
- [Fields and filters overrides](fields-overrides.md). | ||
- [Query params](query-params.md). | ||
- [Pagination](pagination.md). | ||
- [Sort by](sort-by.md). | ||
- [Help](help.md). | ||
- [Compression](compression.md). | ||
- [Cache](cache.md). | ||
- [Cache ttl](cache-ttl.md). | ||
- [Cache control](cache-control.md). | ||
- [Query optimizations](query-optimizations.md). | ||
- [Query depth](query-depth.md). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Pagination | ||
|
||
Capy Serializers supports pagination out of the box, this feature cannot be disabled, you must provide the attribute `path` to the serializer to enable the links. This package supports `limit` and `offset` query params. Like GraphQL, all nested queries are automatically paginated. If you want to load more results, you can use the `next` link in the response. | ||
|
||
## Example | ||
|
||
```json | ||
{ | ||
"count": 100, | ||
"previous": "http://localhost:8000/api/v1/users/?limit=10&offset=0", | ||
"next": "http://localhost:8000/api/v1/users/?limit=10&offset=10", | ||
"first": "http://localhost:8000/api/v1/users/?limit=10&offset=0", | ||
"last": "http://localhost:8000/api/v1/users/?limit=10&offset=90", | ||
"results": [ | ||
... | ||
"nested_m2m": { | ||
"count": 100, | ||
"previous": "http://localhost:8000/api/v1/m2m/?limit=10&offset=0", | ||
"next": "http://localhost:8000/api/v1/m2m/?limit=10&offset=10", | ||
"first": "http://localhost:8000/api/v1/m2m/?limit=10&offset=0", | ||
"last": "http://localhost:8000/api/v1/m2m/?limit=10&offset=90", | ||
"results": [ | ||
... | ||
] | ||
} | ||
] | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Query depth | ||
|
||
Capy Serializers supports query depth, you can set `depth` attribute in the serializer to limit the depth of the query. Default depth is 2. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Query optimizations | ||
|
||
Capy Serializers provides some optimizations out of the box, reducing the number of queries and the amount of data transferred. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Query params | ||
|
||
Capy Serializers supports automatic filtering from query params out of the box, this fields are limited from `filters` attribute and inherit the filters from its children. | ||
|
||
## Operations | ||
|
||
### Greater than | ||
|
||
```http | ||
GET /api/v1/users?age>18 | ||
``` | ||
|
||
### Less than | ||
|
||
```http | ||
GET /api/v1/users?age<18 | ||
``` | ||
|
||
### Greater than or equal to | ||
|
||
```http | ||
GET /api/v1/users?age>=18 | ||
``` | ||
|
||
### Less than or equal to | ||
|
||
```http | ||
GET /api/v1/users?age<=18 | ||
``` | ||
|
||
### Equal to | ||
|
||
```http | ||
GET /api/v1/users?age=18 | ||
``` | ||
|
||
### Not equal to | ||
|
||
```http | ||
GET /api/v1/users?age!=18 | ||
``` | ||
|
||
### In | ||
|
||
```http | ||
GET /api/v1/users?age=18,20,22 | ||
``` | ||
|
||
### Django Lookup | ||
|
||
The supported filters are `exact`, `iexact`, `contains`, `icontains`, `gt`, `gte`, `lt`, `lte`, `in`, `startswith`, `istartswith`, `endswith`, `iendswith`, `range`, `year`, `month`, `day`, `hour`, `minute`, `second`, `isnull`, `search`. | ||
|
||
```http | ||
GET /api/v1/users?age[in]=18,20,22 | ||
``` | ||
|
||
### Not | ||
|
||
This operator is able to negate all the supported operations previously mentioned. | ||
|
||
```http | ||
GET /api/v1/users?age!=18 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Sort by | ||
|
||
Capy Serializers supports sort by, you can set `sort_by` attribute in the serializer to sort the results by a field. Default sort by is `pk`, you can override this value using `sort_by` query param. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,4 +31,4 @@ | |
# SPDX-FileCopyrightText: 2024-present jefer94 <[email protected]> | ||
# | ||
# SPDX-License-Identifier: MIT | ||
__version__ = "1.2.0" | ||
__version__ = "1.2.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.