swindl 3.2.1 · source on github

An example application

Keep track of what you collect: vinyl records, LEGO sets, anything. Built with SWL for the web side and swindl for the storage side (sjdbl’s files, sil’s search). It is meant to be read: every part of swindl is used in a real way, with comments that point to the rest.

What you can do in it:

Running it

From the root of the swindl repository:

pip install -e .                       # swindl (with sjdbl and sil)
pip install -e examples/collection     # this example (and SWL)
python -m swindl_collection            # or: swindl-collection

Open http://127.0.0.1:8000/ and log in as demo / demo-password. A few records and LEGO sets are there to search. Everything is stored in ./collection-data (change it with --data-dir).

Run the tests with pytest examples/collection/swindl_collection.

The files

examples/collection/
└── swindl_collection/
    ├── model.py          Collection and Item: FIELDS, INDEX, index_values, ADAPTATIONS
    ├── catalog.py        searching, deleting a collection, importing: no web in here
    ├── views.py          the actions (SWL): what the page asks the server
    ├── app.py            the store, SWL, the routes, login; the reindex step
    ├── demo.py           the demo data
    ├── app_test.py       tests, through the actions
    ├── templates/collection/   the page, the results, the facets, the dialogs
    └── static/           a few styles, and one small function

How swindl is used

One store, two models

app.py makes the store once:

store = Store(data_dir / "data", MODELS, commit=Commit(immediate=True))

data/store/ gets one JSON file per collection and item (sjdbl), data/index/ an index per model (sil). Commit(immediate=True): a saved item is searchable at once, so “do I have it?” right after adding it says yes. That costs some milliseconds per save; for an application with many writes the default Commit() (every 5 seconds) is faster.

The model says what is stored, searched and accepted

model.py, for an item:

Searching: one query gives the page, the total and the facets

catalog.search:

store.query(QueryParams(
    Item,
    query="+blue +no*",                          # what was typed; see search_text
    filters={"owner": "demo", "__archived__": "False", "decade": ["1950"]},
    sort=[{"field": "year", "reverse": True}],
    page=0, size=20,
    drilldown=[{"name": name, "count": 12} for name in FACETS],
))

Every search filters on owner, so you never see someone else’s items, not even in a count. A clicked facet value is one more filter term. The artist suggestions in the dialog are a drilldown too (catalog.maker_suggestions): a prefix query on maker, counted per maker_exact, so only your own artists are suggested.

Changing: a put, or a transaction

A single save is store.put(item): the file is written and synced, then the index follows. More than one change that belongs together is a transaction:

with store.transaction():                       # catalog.delete_collection
    for item in items:
        store.delete(item)
    store.delete(collection)

All of it is stored, or none of it: also when the server crashes halfway; it is finished or undone when the store is opened again. The import does the same with its puts, after checking every row first (catalog.import_csv).

Ids from the outside are checked

Every qid that comes in with a request goes through owned_collection or owned_item: store.get(qid, assert_type=Item) refuses a qid of another model (and sjdbl refuses qids that are not a file of the store), and the owner must be you. Anything else is “no longer exists”.

Reindexing is a step you take

When a new version changes INDEX or index_values, its CHANGELOG says so, and you leave an empty file <data dir>/actions/reindex before starting it. app.py sees it, reindexes from the files and removes it (catalog.reindex_if_asked). The application never decides that by itself.

Checking

swindl-check collection-data/data swindl_collection.model:MODELS tells whether the index matches the files, without changing anything.