Metadata-Version: 2.4
Name: fastpluggy-example-plugin
Version: 0.3.1
Summary: Example plugin for FastPluggy
Author: FastPluggy Team
License-Expression: MIT
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: FastPluggy<1,>=0.4.113
Requires-Dist: loguru
Provides-Extra: tests
Requires-Dist: pytest>=7.0; extra == "tests"
Requires-Dist: pytest-cov>=4.0; extra == "tests"
Requires-Dist: httpx; extra == "tests"
Requires-Dist: fastpluggy-tasks-worker<1,>=0.3.381; extra == "tests"
Provides-Extra: e2e
Requires-Dist: fastpluggy-cli; extra == "e2e"
Requires-Dist: playwright>=1.40.0; extra == "e2e"
Dynamic: license-file

# Example Plugin for FastPluggy

A small plugin that works, written to be read and copied. Each file shows one thing a
FastPluggy plugin has to get right, on real code with tests.

```bash
pip install fastpluggy-example-plugin
```

The plugin adds an "Example Plugin" menu with a few pages. One of them, **Showcase**, is a
complete small feature: a table in the database, a page that lists it, an action reserved
to administrators, an event, and a background task.

## What each file shows

| File | What it shows |
|---|---|
| `src/plugin.py` | The plugin class: name, menu, settings, routers, declared events, declared companion plugin, and the `on_load_complete` hook |
| `src/router/__init__.py` | Pages and actions; who can reach them |
| `src/permissions.py` | The administrator check, for routes and for pages |
| `src/events.py` | The event the plugin emits, and its own listener |
| `src/models.py`, `src/schema.py` | A table, and how a column is added after the first release |
| `src/tasks/` | Background tasks for the `tasks_worker` plugin |
| `src/config.py` | A setting, read by the Showcase page |
| `src/widgets.py`, `src/templates/` | A widget of the plugin and its template |
| `tests/` | Tests that load the plugin in a real application and call every route |
| `e2e/` | Pages captured by the CI job into `docs/screenshots/e2e/` |

## Who can reach a route

FastPluggy mounts a plugin's routes as they are: it adds no authentication. The plugin
decides, in `src/router/__init__.py`, with two routers:

- `protected_router` carries `Depends(require_authentication)`. Every page goes there, so a
  route added later requires a signed-in user by default.
- `public_router` holds the exceptions. Here it is the About page only.

A route that changes something also takes `Depends(require_admin)` and
`Depends(same_origin_or_403)`: the first refuses a signed-in user who is not an
administrator, the second refuses a request sent by a page of another site.

| Route | Visitor | Signed-in user | Administrator |
|---|---|---|---|
| `GET /about` | yes | yes | yes |
| `GET /`, `/feature`, `/showcase`, `/flash-demo`, `/error-preview…` | no | yes | yes |
| `POST /showcase/add` | no | no | yes |

The Showcase page draws its "Add a sample entry" button only for a user the route accepts.

An application without an authentication plugin has no users: everything is open there.

`tests/test_access.py` calls every route the plugin declares as a visitor, a user and an
administrator. It lists the public routes and the routes that write: a route that is open
or that writes without being in its list fails the test until someone decides it should be.

## Events

Adding a showcase entry emits `example_plugin.showcase_created` with `showcase_id`, `title`
and `author`. The event is declared in `emits` on the plugin class, so the admin **Events**
page lists it and another plugin can subscribe without importing this one:

```python
def on_load_complete(self, fast_pluggy):
    fast_pluggy.events.on("example_plugin.showcase_created", self._on_new_entry)
```

`tests/test_events.py` fails when the code emits an event that is not declared, or declares
one it never emits.

## Database

`src/schema.py` runs at every load. It creates the `showcase` table when it is missing, and
adds the columns that came after the first release (`published_at`) to the installs that
already have the table. A column added to the model alone would exist on new installs only.

`tests/test_schema.py` starts from the table as the first release created it and checks
that the upgrade keeps the rows; it also fails when a model column is missing from
`schema.py`.

## Background task, with `tasks_worker` when it is there

The plugin works without `tasks_worker` and uses it when it is loaded:

- `optional_depends_on` declares it, so it loads first and shows on the plugin's admin page;
- `on_load_complete` subscribes the listener that queues a task only when `tasks_worker` is
  loaded;
- that listener imports `tasks_worker` inside the function.

With `tasks_worker`, a new entry is queued for the `example.showcase.publish` task, which
turns the draft into a published entry. Without it, entries stay drafts.

The modules of `src/tasks/` are different: `tasks_worker` is what imports them when it
discovers tasks, so they import it at the top. They also hold sample tasks to read: simple
and async tasks, progress, retries and errors, a lock, a parent and its child, a schedule,
typed parameters.

## Tests

```bash
pip install -e ".[tests]"
pytest tests/
```

| File | What it proves |
|---|---|
| `test_app_load.py` | The plugin loads in a real application without error or warning, and every page renders |
| `test_access.py` | Each route answers the right callers only |
| `test_events.py` | Emitted events and declared events are the same |
| `test_schema.py` | A table from the first release is upgraded and keeps its rows |
| `test_tasks.py` | The task does its work, and `tasks_worker` finds every task module |
| `test_source_rules.py` | No `print()`, no `async def` handler that awaits nothing, no import of another plugin at module level outside `tasks/`, no hand-written address |

## Starting your own plugin

1. Copy the repository and rename the package: `module_name` in `src/plugin.py`, the
   package name and the entry point in `pyproject.toml`.
2. Keep the two routers and `tests/test_access.py`, and replace the pages.
3. Replace the Showcase with your feature, one file at a time: model and schema, routes,
   event, task.
4. Run `fastpluggy validate .` before the first release.
