One printer picker for machines and PCs, and one default per asset
The assignment belongs to the MACHINE, and until now there was no way to set it except the generic relationships card or the API - the form for the thing the feature is about did not exist. MachineForm now carries the picker, and PCForm uses the SAME component rather than its own copy: the PC's set overrides the machine's, and two implementations of that would drift, with the two ends of an override disagreeing being exactly the bug nobody would spot. The shared picker also fixes what PCForm did on save. It wrote row at a time through the generic relationship endpoints, which is a non-atomic reconcile: an HTTP failure part way left a PC half-assigned with nothing recording what was meant. It now calls the reconcile endpoint, which validates the default before writing anything. A relationship type can now say it allows one active row per asset (relationshiptypes.issingular, migration 7d34), and defaultprinter says it. Cardinality belongs to the type rather than the printers plugin: core's create path is where every hand-made link passes, and the next type meaning "exactly one" gets the rule for free. Setting a second default REPLACES the first instead of refusing, because "make this the default" means that - and a card answering 409 would leave the user hunting for the old row. Without it the schema was happy to hold two defaults: the unique constraint is (source, target, type), so two different targets are two valid rows, and the resolver takes the OLDEST - the new default silently lost. Proven by disabling the new rule and watching the tests fail. FOUND WHILE TESTING IN A BROWSER, and it was not mine: MachineForm read .data.data off computersApi.listAll(), which resolves to the ARRAY - fetchAllPages has already unwrapped every page. The whole parallel load threw into the catch, so every dropdown on the machine edit form came up empty and the machine's own values never loaded. A build cannot see this; only opening the page can. GET /api/printers/assignments/for-asset/<id> returns an asset's OWN assignment, without inheritance, because the editor must show what this asset's rows say - otherwise a machine's printers appear ticked on the PC that inherits them and unticking one silently creates an override.
This commit is contained in:
@@ -792,6 +792,27 @@ def create_asset_relationship():
|
||||
http_code=409
|
||||
)
|
||||
|
||||
# A SINGULAR type allows one active row per source, and setting a new one
|
||||
# REPLACES rather than refusing: "make this the default printer" means
|
||||
# exactly that, and a card that answered 409 would leave the user to find
|
||||
# and delete the old row first.
|
||||
#
|
||||
# Without this the schema is happy to hold two defaults - the unique
|
||||
# constraint is (source, target, type), so two different targets are two
|
||||
# valid rows - and the resolver takes the OLDEST, so the new default
|
||||
# silently loses.
|
||||
reltype = db.session.get(RelationshipType, type_id)
|
||||
replaced = 0
|
||||
if reltype is not None and getattr(reltype, 'issingular', False):
|
||||
others = AssetRelationship.query.filter(
|
||||
AssetRelationship.sourceassetid == source_id,
|
||||
AssetRelationship.relationshiptypeid == type_id,
|
||||
AssetRelationship.isactive == True,
|
||||
AssetRelationship.targetassetid != target_id).all()
|
||||
for row in others:
|
||||
row.isactive = False
|
||||
replaced += 1
|
||||
|
||||
# And it cannot relate BOTH WAYS on a directional type. Only one direction
|
||||
# can be true - a PC drives a machine, never the reverse - but the check
|
||||
# above is keyed on (source, target, type), so the inverse used to insert
|
||||
|
||||
@@ -24,6 +24,11 @@ class RelationshipType(BaseModel):
|
||||
# True: edge has a source->target meaning (controls, partof, Backup For).
|
||||
# False: symmetric link (Dualpath, connectedto, USB...) shown on the card
|
||||
# once per peer with no direction, both stored direction rows collapsed.
|
||||
# At most one ACTIVE relationship of this type per source asset. A PC has
|
||||
# one default printer; the (source, target, type) unique constraint cannot
|
||||
# express that, because two different targets are two different rows.
|
||||
issingular = db.Column(db.Boolean, nullable=False, default=False)
|
||||
|
||||
isdirectional = db.Column(
|
||||
db.Boolean,
|
||||
default=True,
|
||||
|
||||
Reference in New Issue
Block a user