Files
shopdb-flask/plugins/measuringtools/plugin.py
cproudlock 2ad2bd1b90 Sidebar sections are DECLARED, not guessed from a position number
The section headings were derived from position ranges: 10-29 became Assets,
30-49 became Information. A number is for ORDER; it was never a statement about
meaning, and using it as one meant a plugin landed in whichever bucket its
number happened to fall in.

That is how USB Devices - an asset - came to sit under Information beside
GE-Enforce and Tech Tools, and 3D Parts with it. Nobody chose that. It also
meant two plugins picking the same number silently reordered each other:
Warranties and Guides both held 8, the tie broke on plugin load order, and
Guides rendered under Warranties rather than under Slides where it was asked to
go. Warranties moves to 9.

Each nav item now names its section and the renderer groups by what was
declared. An item with no section stays in the unlabelled group at the top,
which is the safe default: it appears, it is simply not filed.

Assets gains USB Devices and 3D Parts. GE-Enforce and Tech Tools move to a new
Fleet section - they are neither assets nor reference material, and calling them
Information was only ever an artefact of their position numbers.

Also: sorting used `position || 99`, and 0 is falsy in JavaScript, so Dashboard -
the only item that uses 0 - sorted to the end of its own group.
2026-08-27 09:25:27 -04:00

181 lines
6.9 KiB
Python

"""Measuring-tools plugin main class.
The first plugin built on the matured framework scaffold (ADR-005). It owns two
tables (measuringtooltypes, measuringtools), a blueprint under /api/measuringtools,
a sidebar entry, and a calibration report card. It seeds its own asset type and
a set of starter tool types on install.
"""
import json
import logging
from pathlib import Path
from typing import List, Dict, Optional, Type
from flask import Flask, Blueprint
from shopdb.plugins.base import BasePlugin, PluginMeta
from shopdb.api import db, AssetType
from .api import measuringtools_bp
from .models import MeasuringTool, MeasuringToolType
logger = logging.getLogger(__name__)
# Starter tool types seeded on install: (name, description, color).
# Colors are drawn from the shared frontend PALETTE (utils/colorStyle.js).
STARTER_TYPES = [
('Caliper', 'Vernier / digital caliper', '#14abef'),
('Micrometer', 'Outside / inside / depth micrometer', '#2dce89'),
('Thread Gage', 'Go / no-go thread gage', '#fb6340'),
('Bore Gage', 'Bore / hole diameter gage', '#7934f3'),
('Height Gage', 'Height / vertical measuring gage', '#ffc107'),
('Indicator', 'Dial / test indicator', '#11cdef'),
('Gage Block Set', 'Reference gage block set', '#e83e8c'),
('Other', 'Other measuring tool', '#6c757d'),
]
class MeasuringToolsPlugin(BasePlugin):
"""Metrology and inspection instruments (calibration lifecycle)."""
def __init__(self):
self._manifest = self._load_manifest()
def _load_manifest(self) -> Dict:
manifest_path = Path(__file__).parent / 'manifest.json'
if manifest_path.exists():
with open(manifest_path, 'r') as f:
return json.load(f)
return {}
@property
def meta(self) -> PluginMeta:
return PluginMeta(
name=self._manifest.get('name', 'measuringtools'),
version=self._manifest.get('version', '1.0.0'),
description=self._manifest.get(
'description', 'Metrology and inspection instruments'),
author=self._manifest.get('author', 'ShopDB Team'),
dependencies=self._manifest.get('dependencies', []),
core_version=self._manifest.get('core_version', '>=0.6.0,<1.0.0'),
api_prefix=self._manifest.get('api_prefix', '/api/measuringtools'),
)
def get_blueprint(self) -> Optional[Blueprint]:
return measuringtools_bp
def get_models(self) -> List[Type]:
return [MeasuringTool, MeasuringToolType]
def get_navigation_items(self) -> List[Dict]:
return [
{
'name': 'Measuring Tools',
'icon': 'ruler',
'route': '/measuringtools',
'position': 22,
'section': 'assets',
},
]
def get_reports(self) -> List[Dict]:
return [
{
'id': 'calibration',
'name': 'Calibration Due',
'description': 'Measuring tools bucketed by calibration status',
'category': 'compliance',
'route': '/reports/calibration',
},
]
def get_config_schema(self) -> List[Dict]:
# No external credentials or endpoints: calibration is tracked by hand,
# so the setup wizard shows nothing to configure. Documented in the
# plugin guide as the intentional empty-schema case.
return []
def get_settings_cards(self) -> List[Dict]:
# ADR-010 pilot. Contributes the Measuring Tools settings card that used
# to be hardcoded in the core settingsNav.js catalog.
return [
{
'group': 'Measuring Tools',
'to': '/settings/measuringtooltypes',
'icon': 'ruler',
'title': 'Measuring Tool Types',
'description': 'Manage measuring-tool subtypes (caliper, '
'micrometer, thread gage...) + map colors',
'position': 22,
},
]
def get_asset_presentation(self) -> List[Dict]:
# ADR-010 pilot. Tells core how to render + link the measuring_tool
# asset type in global-search rows and cross-links.
# The consumer only substitutes {assetid} (search/cross-link rows carry
# the core asset id, not the extension id), so link through the by-asset
# resolver route rather than the id-keyed detail route.
return [
{
'assettype': 'measuring_tool',
'icon': 'ruler',
'label': 'Measuring Tool',
'route': '/measuringtools/by-asset/{assetid}',
},
]
def get_map_overlays(self) -> List[Dict]:
# ADR-010 pilot. Declares a calibration-due badge overlay for the
# shop-floor map; the map fetches the endpoint to decorate markers.
return [
{
'id': 'calibration-due',
'label': 'Calibration due',
'endpoint': '/api/measuringtools/map-overlay',
'style': 'badge',
'legend': True,
},
]
def init_app(self, app: Flask, db_instance) -> None:
logger.info(f"Measuring-tools plugin initialized (v{self.meta.version})")
def on_install(self, app: Flask) -> None:
with app.app_context():
self._ensure_asset_type()
self._ensure_starter_types()
db.session.commit()
logger.info("Measuring-tools plugin installed")
def _ensure_asset_type(self) -> None:
existing = AssetType.query.filter_by(assettype='measuring_tool').first()
if not existing:
db.session.add(AssetType(
assettype='measuring_tool',
pluginname='measuringtools',
tablename='measuringtools',
description='Metrology and inspection instruments (gauges, '
'calipers, thread gages, bore gages, ...)',
icon='ruler',
))
logger.debug("Created asset type: measuring_tool")
db.session.commit()
def _ensure_starter_types(self) -> None:
for name, description, color in STARTER_TYPES:
if not MeasuringToolType.query.filter_by(name=name).first():
db.session.add(MeasuringToolType(
name=name, description=description, color=color))
logger.debug(f"Created measuring-tool type: {name}")
db.session.commit()
def get_permissions(self) -> List:
"""Return the RBAC permissions this plugin owns."""
return [
('measuringtools.view', 'View measuring tools', 'measuringtools'),
('measuringtools.create', 'Create measuring tools', 'measuringtools'),
('measuringtools.edit', 'Edit measuring tools', 'measuringtools'),
('measuringtools.delete', 'Delete measuring tools', 'measuringtools'),
]