repository

BlowfishCMS

A small content management system for OpenBSD

version 0.45.6 · ISC · 2026-12-03 · releases

57 lines · 3.6 KB · download this file

"""Sample content for test catalogues. A new site's catalogue has no photos,
tracks or articles yet, so a few placeholder rows are added where a kind is
missing. The files themselves are never needed: the tests only read the
catalogue."""
import secrets

def ensure_samples(con):
    def missing(sql): return not con.execute(sql).fetchone()[0]
    rid = lambda: secrets.token_hex(16)
    if missing("select count(*) from uploads where kind='photography'"):
        # In a "Samples" folder of the picture library, as the code itself would file them.
        con.execute("insert or ignore into picture_folders (path,name,created_at) values ('samples','Samples','2026-01-01T00:00:00Z')")
        for n in (1, 2):
            i = rid()
            con.execute("insert into uploads (id,kind,title,collection,folder,filename,thumbnail,mime,bytes,width,height,created_at,sort_order) values (?,?,?,?,?,?,?,?,?,?,?,?,?)",
                        (i, 'photography', f'Sample photo {n}', 'Samples', 'samples', i + '.webp', i + '-thumb.webp', 'image/webp', 1000, 1200, 800, f'2026-01-0{n}T00:00:00Z', n))
    if missing("select count(*) from uploads where kind='music'"):
        i = rid()
        con.execute("insert into uploads (id,kind,title,collection,filename,thumbnail,mime,bytes,created_at) values (?,?,?,?,?,?,?,?,?)",
                    (i, 'music', 'Sample track', 'Samples', i + '.mp3', '', 'audio/mpeg', 1000, '2026-01-01T00:00:00Z'))
    if missing("select count(*) from posts where published_at is not null"):
        con.execute("insert into posts (id,title,body,published_at,created_at,updated_at) values (?,?,?,?,?,?)",
                    (rid(), 'Sample article', 'Some *sample* text.', '2026-01-01T00:00:00Z', '2026-01-01T00:00:00Z', '2026-01-01T00:00:00Z'))
    con.commit()

def start_catalog(dest):
    """Start dest from a new site's catalogue (built once per run by this
    release's own --migrate). The suites test the code, so they never start
    from a site's own catalogue: a site's pages, content and settings are its
    owner's business and mustn't decide whether an update passes. The site's
    own data is checked by tests/upgrade.py alone. Returns the number of
    uploads (none on a new site)."""
    import sqlite3
    with sqlite3.connect(fresh_catalog()) as original, sqlite3.connect(dest) as copy:
        original.backup(copy)
    with sqlite3.connect(dest) as con:
        has = con.execute("select count(*) from sqlite_master where type='table' and name='uploads'").fetchone()[0]
        return con.execute('select count(*) from uploads').fetchone()[0] if has else 0

def fresh_catalog():
    """A new site's catalogue with a basic site's pages (tests/fixtures/basic-site-pages.json),
    built once per run by the code's own --migrate."""
    import os, pathlib, subprocess, tempfile
    global _fresh
    if globals().get('_fresh'):
        return _fresh
    root = pathlib.Path(tempfile.mkdtemp(prefix='blowfish-fresh-'))
    for d in ['var/db/blowfishcms/admin/tmp', 'var/db/blowfishcms-comments', 'var/db/blowfishcms-contact']:
        (root/d).mkdir(parents=True)
    for kind in ['photography', 'music', 'artwork', 'blog', 'video', 'site']:
        (root/'media/blowfishcms'/kind).mkdir(parents=True)
    site = pathlib.Path(__file__).resolve().parent.parent/'cgi-bin/site.pl'
    seed = pathlib.Path(__file__).resolve().parent/'fixtures/basic-site-pages.json'
    subprocess.run(['perl', str(site), '--migrate'], env={**{k: v for k, v in os.environ.items() if not k.startswith('BLOWFISH_')}, 'BLOWFISH_ROOT': str(root), 'BLOWFISH_PAGES_SEED': str(seed)},
                   check=True, stdout=subprocess.DEVNULL)
    _fresh = str(root/'var/db/blowfishcms/catalog.sqlite')
    return _fresh