# annals.bulk

Bulk actions: one action over a selection of items, in one request.

Every single-item action on a document has a batch form here (anything done to one item can
be done to a selection), so the page sends one request per
action and gets one answer, never N requests and N toasts. Each function answers
`{"done": [...], "refused": [{"id" or "key", "why"}]}`: an item that cannot take the
action is refused with a reason and the rest go ahead, and `done` says what actually
changed, which is exactly what an Undo takes back.

- [`edit_tags()`](#annals.bulk.edit_tags): add and remove tags, per item, atomically (`ops` may differ per item,
  so an Undo can take back only what each item gained or lost).
- [`trash()`](#annals.bulk.trash) and [`restore()`](#annals.bulk.restore): move to and from the recycle bin (each the other’s Undo);
  one already where it was asked to go is listed in `already`, not `done`.
- [`purge()`](#annals.bulk.purge): delete forever, only what is already in the bin. Irreversible: the page asks
  first (with the count; typed above a threshold).

A selection larger than `max_items` is refused whole (`ValueError`): the page says so
rather than starting a request that could time out half way.

```pycon
>>> from annals.store import DocStore
>>> import tempfile
>>> store = DocStore(tempfile.mkdtemp())
>>> a = store.publish(text="# A", title="A", filename="a.md")["id"]
>>> trash(store, [a, "nope"])["refused"]
[{'id': 'nope', 'why': 'no such document'}]
>>> restore(store, [a])["done"] == [a]
True
```

### Functions

| [`edit_tags`](#annals.bulk.edit_tags)(catalog, ops, \*[, max_items])    | Apply `[{"key", "add": [...], "remove": [...]}, ...]`, each item on its own, in one write of the tag store.                                                        |
|----------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [`purge`](#annals.bulk.purge)(store, catalog, ids, \*[, max_items]) | Delete each document forever, only those in the bin (the ids the person confirmed, never "everything in the bin now": one binned since from another tab survives). |
| [`restore`](#annals.bulk.restore)(store, ids, \*[, max_items])        | Bring each document back from the recycle bin (already out counts as done).                                                                                        |
| [`trash`](#annals.bulk.trash)(store, ids, \*[, max_items])          | Move each document to the recycle bin (already there counts as done).                                                                                              |

### annals.bulk.edit_tags(catalog, ops, , max_items=500)

Apply `[{"key", "add": [...], "remove": [...]}, ...]`, each item on its own, in
one write of the tag store.

`done` lists `{"key", "added", "removed", "tags"}` for each item that changed or was
already as asked; `refused` names the rest (no such item, an invalid tag).

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)

### annals.bulk.purge(store, catalog, ids, , max_items=500)

Delete each document forever, only those in the bin (the ids the person confirmed,
never “everything in the bin now”: one binned since from another tab survives).

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)

### annals.bulk.restore(store, ids, , max_items=500)

Bring each document back from the recycle bin (already out counts as done).

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)

### annals.bulk.trash(store, ids, , max_items=500)

Move each document to the recycle bin (already there counts as done).

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)
