Add per-domain global-search toggles

Lets an admin choose which content types appear in global search results,
independent of whether the owning plugin is enabled (the existing
_require_enabled gating was all-or-nothing per plugin).

- settings.py: SEARCH_DOMAINS const (9 result types: application, knowledgebase,
  employee, equipment, computer, printer, network_device, notification, subnet)
  + seed keys search_<type>_enabled (boolean, default true) in
  build_default_settings (covers API seed + CLI).
- search.py: global_search loads disabled types in one query (category 'search')
  and filters the deduped results by type before counts/truncation. Missing key
  = enabled.
- SystemSettings.vue: "Global Search" section, one toggle per domain (mirrors the
  identifier pattern; create-on-404 fallback so an un-reseeded deploy still works).
- Tests: domain included by default, disabled domain excluded, seed creates the
  9 keys.

166 tests pass, naming green, build green. Verified live: toggling
search_knowledgebase_enabled off drops knowledgebase from search counts and back
on restores it. Dev DB seeded (9 keys).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
cproudlock
2026-06-26 20:39:34 -04:00
parent a6f6c3f51f
commit 8ddf771b72
4 changed files with 166 additions and 2 deletions

View File

@@ -0,0 +1,44 @@
"""Per-domain global-search toggles (search_<type>_enabled).
A content type can be hidden from search independently of whether its plugin is
enabled, by setting search_<type>_enabled = false.
"""
from shopdb.core.api.settings import SEARCH_DOMAINS
def _kb_types(client, auth_headers, term):
resp = client.get(f'/api/search?q={term}', headers=auth_headers)
assert resp.status_code == 200, resp.get_json()
return [r for r in resp.get_json()['data']['results']
if r.get('type') == 'knowledgebase']
def test_domain_included_by_default(client, db, auth_headers):
"""With no toggle set, the knowledgebase domain appears in search."""
client.post('/api/knowledgebase',
json={'shortdescription': 'searchtoggle alpha',
'linkurl': 'https://kb.example/a'},
headers=auth_headers)
assert _kb_types(client, auth_headers, 'searchtoggle')
def test_disabled_domain_excluded(client, db, auth_headers):
"""search_knowledgebase_enabled=false hides KB results."""
from shopdb.core.models import Setting
client.post('/api/knowledgebase',
json={'shortdescription': 'searchtoggle beta',
'linkurl': 'https://kb.example/b'},
headers=auth_headers)
Setting.set('search_knowledgebase_enabled', False, valuetype='boolean',
category='search')
assert _kb_types(client, auth_headers, 'searchtoggle') == []
def test_search_seed_creates_domain_keys(client, db, auth_headers):
"""Seeding produces one boolean setting per search domain."""
resp = client.post('/api/settings/seed', headers=auth_headers)
assert resp.status_code == 200
from shopdb.core.models import Setting
keys = {s.key for s in Setting.query.filter_by(category='search').all()}
assert {f'search_{k}_enabled' for k in SEARCH_DOMAINS} <= keys