Files
lego-instructions-manager/app/models/site_setting.py
T
jessikitty 89f65e2d74 Add automatic instruction fetcher for official sets
Downloads official instruction PDFs from LEGO, located via the Brickset API, one set per interval (10 minutes by default) so neither API gets hammered. Official sets only; MOCs are excluded throughout.

- New admin page at /admin/instruction-fetch: queue builder, pacing controls, live countdown and per-set job history

- Database-backed queue (instruction_fetch_jobs) and last-run timestamp, so pacing survives restarts

- Background worker thread, plus 'flask instructions queue/run/status' for Task Scheduler or cron

- SHA-256 dedupe (instructions.sha256), PDF header check, size cap and streaming downloads via temp files

- brickset_api: getInstructions2 by set number and getInstructions by setID (the old code sent a set number to the setID method); getSets params now proper JSON; getKeyUsageStats added

- Schema top-ups applied automatically at boot: new table plus instructions.sha256/source

- Docker: fetcher env vars passed through compose, no worker thread in the entrypoint bootstrap
2026-09-26 13:43:17 +10:00

109 lines
3.3 KiB
Python

"""
Key/value store for admin-configurable site settings.
Values are strings on disk; the typed helpers below do the coercion. Reads
are cached per process and the cache is dropped on write, so the settings
page is authoritative without hitting the database on every request.
Created by: Jess Rogerson (yelling commands at Claude.AI)
"""
from datetime import datetime
import threading
from app import db
# Defaults are the behaviour LIM had before settings existed, except where
# noted — anything not yet stored falls back to these.
DEFAULTS = {
'allow_registration': 'true',
# Visibility given to newly created sets.
'default_visibility': 'private',
# Send logged-in users straight to their dashboard instead of the
# marketing homepage.
'home_redirect_dashboard': 'true',
# Automatic instruction fetcher (see services/instruction_fetcher.py).
# Off by default: an admin has to switch it on deliberately.
'fetch_enabled': 'false',
'fetch_interval_minutes': '10',
'fetch_file_delay_seconds': '5',
'fetch_max_attempts': '3',
'fetch_skip_sets_with_pdfs': 'true',
'fetch_allow_setid_lookup': 'true',
'fetch_last_run': '',
}
VISIBILITIES = ('private', 'shared', 'public')
_cache = {}
_cache_lock = threading.Lock()
class SiteSetting(db.Model):
"""One configurable setting."""
__tablename__ = 'site_settings'
id = db.Column(db.Integer, primary_key=True)
key = db.Column(db.String(64), unique=True, nullable=False, index=True)
value = db.Column(db.Text, nullable=True)
updated_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow,
onupdate=datetime.utcnow)
def __repr__(self):
return f'<SiteSetting {self.key}={self.value!r}>'
# -- reads --------------------------------------------------------------
@classmethod
def get(cls, key, default=None):
with _cache_lock:
if key in _cache:
return _cache[key]
try:
row = cls.query.filter_by(key=key).first()
except Exception:
# Table may not exist yet (first boot, before create_all).
return DEFAULTS.get(key, default)
value = row.value if row else DEFAULTS.get(key, default)
with _cache_lock:
_cache[key] = value
return value
@classmethod
def get_bool(cls, key, default=False):
value = cls.get(key)
if value is None:
return default
return str(value).strip().lower() in ('1', 'true', 'yes', 'on')
@classmethod
def all_settings(cls):
merged = dict(DEFAULTS)
try:
for row in cls.query.all():
merged[row.key] = row.value
except Exception:
pass
return merged
# -- writes -------------------------------------------------------------
@classmethod
def set(cls, key, value):
if isinstance(value, bool):
value = 'true' if value else 'false'
row = cls.query.filter_by(key=key).first()
if row:
row.value = value
else:
db.session.add(cls(key=key, value=value))
cls.clear_cache()
return value
@classmethod
def clear_cache(cls):
with _cache_lock:
_cache.clear()