Metadata-Version: 2.4
Name: nyxa-admin
Version: 0.0.1
Summary: Nyxa admin service
Author-email: Al-Amin Islam Nerob <alamin@aincoder.com>
License-Expression: MIT
Project-URL: Repository, https://github.com/NyxaDev/Nyxa
Classifier: Development Status :: 4 - Beta
Requires-Python: >=3.13
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: nyxa>=0.0.1
Dynamic: license-file

# nyxa-admin

**Status:** beta / experimental on the 0.x line. APIs may change before 1.0.

Nova-lite auto-CRUD for Nyxa — register ORM models and get
`/_admin/<resource>` list/detail/create/update/delete endpoints plus a
dashboard card, gated by a configurable `admin.guard`.

## Features

- **`AdminRegistry` (`app.admin`)** — `admin.register(Model,
  name=None)` exposes a model as a resource; `list`/`get`/`create`/
  `update`/`delete` operate on it (writes filtered to the model's
  writable fields).
- **CRUD routes** — mounted at boot (internal):
  `GET /_admin/{resource}` (list, `limit=50`),
  `GET /_admin/{resource}/{pk}` (detail),
  `POST /_admin/{resource}` (create),
  `PUT /_admin/{resource}/{pk}` (update),
  `DELETE /_admin/{resource}/{pk}` (delete). JSON responses; list/
  detail render minimal HTML tables unless the request `Accept`s JSON.
- **Guarded access** — `admin.guard` names a callable receiving an
  `AdminAuthzContext` (`action`, `resource`, `pk`, `ctx`) per request;
  **without a guard the panel answers only in `app.debug`** — denied
  requests get a bare 404 that doesn't disclose the panel. Mutating
  routes use `csrf=False`; the guard is the real gate. Never use an
  always-allow guard (`lambda _: True`) in production.
- **Dashboard card** — contributes an Admin tab listing registered
  resources to `nyxa-horizon`-style dashboards.

## Install

```bash
nyxa add nyxa-admin
```

## Usage

```python
# in a provider's boot()
app.admin.register(User)
app.admin.register(Post, name="posts")
```

```python
# production guard (debug-only by default) — config/admin.py:
from nyxa_admin import AdminAuthzContext


def guard(authz: AdminAuthzContext) -> bool:
    user = authz.ctx.state.get("user")
    return user is not None and getattr(user, "is_admin", False)


config = {"guard": guard}  # sync or async callables
# or programmatically in a provider's register()/boot():
# app.config.set("admin.guard", guard)
```

`GET /_admin/users` now lists users; POST/PUT bodies are JSON and
filtered to writable fields.

## Public API

- **`AdminRegistry`** — bound as `app.admin`:
  `registry.register(model, name=None)`,
  `registry.resource(name)`, `registry.resources() -> dict`,
  `await registry.list(name, limit=50)`, `await registry.get(name, pk)`,
  `await registry.create(name, data)`, `await registry.update(name,
  pk, data)`, `await registry.delete(name, pk) -> bool`.
- **`AdminAuthzContext`** — dataclass handed to `admin.guard`:
  `action` (`"view"|"create"|"update"|"delete"`), `resource`,
  `pk` (`None` on list/create), `ctx` (`HttpContext`).
- **`AdminProvider`** — binds `app.admin`, mounts the `/_admin`
  routes, contributes the dashboard card, and publishes
  `admin.guard` (default `None` → debug-only).

## Configuration

`config/admin.py` (the `config` dict namespaces as `admin.*`):

```python
config = {
    "guard": None,  # callable(authz: AdminAuthzContext) -> bool
    # (legacy guard(ctx) still accepted, deprecated);
    # None = panel answers only in debug
}
```

## Testing

```python
app.admin.register(User)
res = client.get("/_admin/users", headers={"accept": "application/json"})
assert res.status_code == 200
```

## License

MIT
