Metadata-Version: 2.4
Name: dj-corekit
Version: 0.1.0
Summary: Small Django utilities built on Django core.
Author: Kitcat
License-Expression: MIT
Keywords: django,cache,toolkit
Classifier: Framework :: Django
Classifier: Framework :: Django :: 5.0
Classifier: Framework :: Django :: 5.1
Classifier: Framework :: Django :: 5.2
Classifier: Framework :: Django :: 6.0
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: Django>=5.0
Provides-Extra: dev
Requires-Dist: pytest; extra == "dev"
Requires-Dist: pytest-django; extra == "dev"
Requires-Dist: ruff; extra == "dev"
Requires-Dist: tox; extra == "dev"
Dynamic: license-file

# dj-corekit

Small Django utilities built on Django core.

## Install

```bash
pip install dj-corekit
```

## Cache Page

Function view:

```python
from dj_corekit.cache import cache_page


@cache_page(
    timeout=300,
    key_func=lambda request: f"product:{request.GET.get('id')}",
    headers=lambda request, response: {
        "X-Cache-Scope": "product",
        "Cache-Control": "public, max-age=300",
    },
)
def product_detail(request):
    ...
```

Class-based view with Django's `method_decorator`:

```python
from django.utils.decorators import method_decorator
from django.views import View
from dj_corekit.cache import cache_page


@method_decorator(
    cache_page(timeout=300, key_func=lambda request: "product:list"),
    name="dispatch",
)
class ProductListView(View):
    ...
```

Class-based view with the mixin:

```python
from django.views import View
from dj_corekit.cache import CachePageMixin


class ProductDetailView(CachePageMixin, View):
    cache_timeout = 300
    cache_headers = {"X-Cache-Scope": "product"}

    def get_cache_key(self, request):
        return f"product:{self.kwargs['pk']}"
```

`key_func` returns the full Django cache key. The response is stored through
`django.core.cache.caches[cache_alias]`.

Only `200` responses are cached. `TemplateResponse` objects are cached after
rendering. Set `request.do_not_cache = True` or return `False` from `only_if`
to bypass caching.

## API

```python
cache_page(
    timeout,
    key_func,
    *,
    headers=None,
    cache_alias="default",
    only_if=None,
)
```

```python
class CachePageMixin:
    cache_timeout = 300
    cache_headers = None
    cache_alias = "default"
    cache_only_if = None

    def get_cache_key(self, request):
        ...
```

- `timeout`: seconds, or a callable receiving the response.
- `key_func`: callable receiving the request and returning the full cache key.
- `headers`: dict or callable receiving `(request, response)` and returning a
  header dict. These headers are applied before the response is cached.
- `cache_alias`: Django cache alias, default `"default"`.
- `only_if`: callable receiving the request. Return `False` to bypass cache.

## Development

```bash
python -m pip install -e ".[dev]"
python -m pytest
ruff check .
ruff format --check .
tox
```
