Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jefer94 committed Dec 3, 2024
2 parents 484d9fa + b17b770 commit a200680
Show file tree
Hide file tree
Showing 19 changed files with 337 additions and 23 deletions.
3 changes: 0 additions & 3 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

from celery import Celery


app = Celery(task_always_eager=True)



pytest_plugins = ("capyc.pytest.rest_framework",)

3 changes: 3 additions & 0 deletions docs/changelog/v1.2.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# v1.1.1

- Add some fixes to Capy Serializers.
12 changes: 12 additions & 0 deletions docs/serializers/cache-control.md
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
```
12 changes: 12 additions & 0 deletions docs/serializers/cache-ttl.md
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
```
13 changes: 13 additions & 0 deletions docs/serializers/cache.md
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,
}
}
```
14 changes: 14 additions & 0 deletions docs/serializers/compression.md
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,
}
}
```
25 changes: 25 additions & 0 deletions docs/serializers/field-sets.md
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[]",),
}
```
22 changes: 22 additions & 0 deletions docs/serializers/fields-overrides.md
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
```
7 changes: 7 additions & 0 deletions docs/serializers/help.md
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
```
44 changes: 44 additions & 0 deletions docs/serializers/introduction.md
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).
28 changes: 28 additions & 0 deletions docs/serializers/pagination.md
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": [
...
]
}
]
}
```
3 changes: 3 additions & 0 deletions docs/serializers/query-depth.md
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.
3 changes: 3 additions & 0 deletions docs/serializers/query-optimizations.md
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.
63 changes: 63 additions & 0 deletions docs/serializers/query-params.md
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
```
3 changes: 3 additions & 0 deletions docs/serializers/sort-by.md
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.
16 changes: 16 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,29 @@ nav:
- "feature-flags/variant.md"
- "feature-flags/flags-file.md"
- "feature-flags/reading-flags.md"
- Serializers:
- "serializers/introduction.md"
- "serializers/field-sets.md"
- "serializers/fields-overrides.md"
- "serializers/query-params.md"
- "serializers/pagination.md"
- "serializers/sort-by.md"
- "serializers/help.md"
- "serializers/compression.md"
- "serializers/cache.md"
- "serializers/cache-ttl.md"
- "serializers/cache-control.md"
- "serializers/query-optimizations.md"
- "serializers/query-depth.md"
- Exceptions:
- "exceptions/validation-exception.md"
- "exceptions/payment-exception.md"
- Changelog:
- "changelog/v1.0.0.md"
- "changelog/v1.0.3.md"
- "changelog/v1.1.0.md"
- "changelog/v1.2.0.md"
- "changelog/v1.2.1.md"
- Fixtures:
- circuitbreaker:
- "fixtures/circuitbreaker/dont-close-the-circuit.md"
Expand Down
2 changes: 1 addition & 1 deletion src/capyc/__about__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@
# SPDX-FileCopyrightText: 2024-present jefer94 <[email protected]>
#
# SPDX-License-Identifier: MIT
__version__ = "1.2.0"
__version__ = "1.2.1"
16 changes: 11 additions & 5 deletions src/capyc/django/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,22 @@
CAPYC = getattr(settings, "CAPYC", {})
if "cache" in CAPYC and isinstance(CAPYC["cache"], dict):
is_cache_enabled = CAPYC["cache"].get("enabled", True)
min_compression_size = CAPYC["cache"].get("min_kb_size", 10)

else:
is_cache_enabled = os.getenv("CAPYC_CACHE", "True") not in FALSE_VALUES
min_compression_size = int(os.getenv("CAPYC_MIN_COMPRESSION_SIZE", "10"))

if "compression" in CAPYC and isinstance(CAPYC["compression"], dict):
is_compression_enabled = CAPYC["compression"].get("enabled", True)
min_compression_size = CAPYC["compression"].get("min_kb_size", 10)

else:
is_compression_enabled = os.getenv("CAPYC_COMPRESSION", "True") not in FALSE_VALUES
min_compression_size = int(os.getenv("CAPYC_MIN_COMPRESSION_SIZE", "10"))

settings = {
"min_compression_size": min_compression_size,
"is_cache_enabled": is_cache_enabled,
"is_compression_enabled": is_compression_enabled, # not used yet
}


Expand Down Expand Up @@ -127,9 +133,9 @@ def set_cache(
elif cache_control:
res["headers"]["Cache-Control"] = cache_control

elif ttl:
res["headers"]["Cache-Control"] = f"max-age={ttl}"
res["headers"]["Expires"] = (timezone.now() + timedelta(seconds=ttl)).isoformat()
# elif ttl:
# res["headers"]["Cache-Control"] = f"max-age={ttl}"
# res["headers"]["Expires"] = (timezone.now() + timedelta(seconds=ttl)).isoformat()

else:
res["headers"]["Cache-Control"] = "public"
Expand Down
Loading

0 comments on commit a200680

Please sign in to comment.