Files
shopdb-flask/shopdb/core/models/customfield.py
cproudlock 48d3160bc5
All checks were successful
CI / backend (push) Successful in 23s
CI / naming (push) Successful in 2s
CI / frontend (push) Successful in 8s
Rename the equipment domain to machines; retype the models catalog (ADR-011)
The equipment plugin is now the machines plugin, ending the UI-vs-code
vocabulary split while the contract is pre-1.0 and nothing external
depends on the old names.

- plugins/equipment -> plugins/machines: manifest, class, /api/machines,
  machines.* permissions, registry key (with an auto-migrating load shim
  for existing installs).
- Tables: equipment -> machines (equipmentid -> machineid) and
  equipmenttypes -> machinetypes, renamed in the plugin's own migration
  chain (machines0002rename), idempotent for both upgrading and fresh
  installs.
- The legacy core machinetypes lookup actually types the vendor MODELS
  catalog, so it is renamed losslessly to modeltypes
  (models.modeltypeid, /api/modeltypes, Model Types settings page)
  rather than collapsed, freeing the machinetypes name. Core migration
  7d17_machines_rename also flips data in place: assettypes row
  equipment -> machine, auditlog entitytype, identifier_/search_
  settings keys, permission rows, and renames alembic_version_equipment.
- Frontend: machinesApi/modeltypesApi, item.machine response shape,
  assettype value compares 'equipment' -> 'machine' (map, search,
  custom fields, relationships), routes machines.js with plugin gating
  retagged, /print/machine-badge, Machine Types (subtypes) and Model
  Types (catalog) settings pages, machines-by-type report id.
- Docs swept; ADRs left as history per the authoring rule.

Upgrade: flask db upgrade then flask plugin upgrade-all.

Verified: dev DB flipped live (262 machines, 35 modeltypes, 95 models
retyped, zero equipment tables remain); fresh scratch-MySQL install
produces the new names; 341 tests green; naming/style green; frontend
builds; live E2E on machines list/detail, PC relationships, map,
reports, and both settings pages.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-11 15:17:42 -04:00

92 lines
3.1 KiB
Python

"""Custom fields: site-defined extra attributes per asset type.
A CustomField is a definition scoped to one asset type (machine, computer,
printer, network_device). A CustomFieldValue holds one asset's value for one
field. This is the generic form of the built-in identifier columns - sites add
their own attributes without a schema change.
"""
from shopdb.extensions import db
# Allowed datatypes for a custom field. Values are always stored as text and
# cast on render/input by the frontend.
CUSTOM_FIELD_DATATYPES = ('text', 'number', 'date', 'boolean', 'select')
class CustomField(db.Model):
__tablename__ = 'customfields'
fieldid = db.Column(db.Integer, primary_key=True)
assettypeid = db.Column(
db.Integer,
db.ForeignKey('assettypes.assettypeid'),
nullable=False,
comment='Which asset category this field applies to'
)
# Machine-name key, unique per asset type. Used for stable references.
fieldkey = db.Column(db.String(50), nullable=False)
label = db.Column(db.String(150), nullable=False)
datatype = db.Column(db.String(20), nullable=False, server_default='text')
# JSON array of options, only meaningful when datatype='select'
options = db.Column(db.Text)
showondetail = db.Column(db.Boolean, nullable=False, server_default='1')
showonform = db.Column(db.Boolean, nullable=False, server_default='1')
sortorder = db.Column(db.Integer, nullable=False, server_default='0')
isactive = db.Column(db.Boolean, nullable=False, server_default='1')
__table_args__ = (
db.UniqueConstraint('assettypeid', 'fieldkey', name='uq_customfield_type_key'),
)
def to_dict(self):
import json
parsed_options = []
if self.options:
try:
parsed_options = json.loads(self.options)
except (ValueError, TypeError):
parsed_options = []
return {
'fieldid': self.fieldid,
'assettypeid': self.assettypeid,
'fieldkey': self.fieldkey,
'label': self.label,
'datatype': self.datatype,
'options': parsed_options,
'showondetail': bool(self.showondetail),
'showonform': bool(self.showonform),
'sortorder': self.sortorder,
'isactive': bool(self.isactive),
}
class CustomFieldValue(db.Model):
__tablename__ = 'customfieldvalues'
valueid = db.Column(db.Integer, primary_key=True)
fieldid = db.Column(
db.Integer,
db.ForeignKey('customfields.fieldid', ondelete='CASCADE'),
nullable=False
)
assetid = db.Column(
db.Integer,
db.ForeignKey('assets.assetid', ondelete='CASCADE'),
nullable=False
)
value = db.Column(db.Text)
field = db.relationship('CustomField')
__table_args__ = (
db.UniqueConstraint('fieldid', 'assetid', name='uq_customfieldvalue_field_asset'),
)
def to_dict(self):
return {
'valueid': self.valueid,
'fieldid': self.fieldid,
'assetid': self.assetid,
'value': self.value,
}