Three faults around vendor-model photos, found while looking at why an uploaded image did not appear. Saving a model was blocked after uploading a photo. The Image URL field was type="url", and an upload sets it to an application path such as /api/models/image/model-120.png. Native url validation demands an absolute URL with a scheme, so the browser refused to submit the form with "Please enter a URL" for a value the page had just written itself. The field is now type="text", which is what it always needed to be: it holds either a full web address or a path on this server. documentationurl stays type="url". The upload button did not appear when adding a model, only when editing one. That was deliberate - the photo is stored as model-<id>.<ext>, so it cannot be sent before the record has an id - but it reads as a missing feature, and the hint explaining it was easy to miss. A photo chosen while creating is now held and uploaded as soon as the model is saved, and it is dropped if the dialog is cancelled, so it cannot land on the next model created in the same session. Network devices could never show a photo. NetworkDeviceDetail.vue binds its hero image to networkdevice.imageurl, but networkdevices carried only vendorid, with no link to a catalog model, so nothing could populate it - a feature that looked present and could not work. Machines, PCs and printers have carried modelnumberid since July. This adds the same column and relationship, the to_dict branch that exposes modelname and imageurl, the field on the API, and a Model selector on the form so the link can actually be set. The migration is guarded the same way employees0002photo is: on a fresh database the tables come from the SQLAlchemy models, which already declare the column, so an unconditional add fails with "duplicate column name". The foreign key is created only on databases that can add one by ALTER; routing it through batch_alter_table made Alembic's column sort raise "Circular dependency detected" on the fresh-database test. Deploying this needs `flask db upgrade` and `flask plugin upgrade-all` on the server, not just a file copy.
135 lines
3.8 KiB
Python
135 lines
3.8 KiB
Python
"""Network device plugin models."""
|
|
|
|
from shopdb.api import db, BaseModel
|
|
|
|
|
|
class NetworkDeviceType(BaseModel):
|
|
"""
|
|
Network device type classification.
|
|
|
|
Examples: Switch, Router, Access Point, Camera, IDF, Firewall, etc.
|
|
"""
|
|
__tablename__ = 'networkdevicetypes'
|
|
|
|
networkdevicetypeid = db.Column(db.Integer, primary_key=True)
|
|
networkdevicetype = db.Column(db.String(100), unique=True, nullable=False)
|
|
description = db.Column(db.Text)
|
|
icon = db.Column(db.String(50), comment='Icon name for UI')
|
|
color = db.Column(db.String(20), comment='CSS color for UI/map markers')
|
|
|
|
def __repr__(self):
|
|
return f"<NetworkDeviceType {self.networkdevicetype}>"
|
|
|
|
|
|
class NetworkDevice(BaseModel):
|
|
"""
|
|
Network device-specific extension data.
|
|
|
|
Links to core Asset table via assetid.
|
|
Stores network device-specific fields like hostname, firmware, ports, etc.
|
|
"""
|
|
__tablename__ = 'networkdevices'
|
|
|
|
networkdeviceid = db.Column(db.Integer, primary_key=True)
|
|
|
|
# Link to core asset
|
|
assetid = db.Column(
|
|
db.Integer,
|
|
db.ForeignKey('assets.assetid', ondelete='CASCADE'),
|
|
unique=True,
|
|
nullable=False,
|
|
index=True
|
|
)
|
|
|
|
# Network device classification
|
|
networkdevicetypeid = db.Column(
|
|
db.Integer,
|
|
db.ForeignKey('networkdevicetypes.networkdevicetypeid'),
|
|
nullable=True
|
|
)
|
|
|
|
# Vendor
|
|
modelnumberid = db.Column(
|
|
db.Integer,
|
|
db.ForeignKey('models.modelnumberid'),
|
|
nullable=True,
|
|
comment='Catalog model, which is where the device photo comes from'
|
|
)
|
|
|
|
vendorid = db.Column(
|
|
db.Integer,
|
|
db.ForeignKey('vendors.vendorid'),
|
|
nullable=True
|
|
)
|
|
|
|
# Network identity
|
|
hostname = db.Column(
|
|
db.String(100),
|
|
comment='Network hostname'
|
|
)
|
|
|
|
# Firmware/software version
|
|
firmwareversion = db.Column(db.String(100), nullable=True)
|
|
|
|
# Physical characteristics
|
|
portcount = db.Column(
|
|
db.Integer,
|
|
nullable=True,
|
|
comment='Number of ports (for switches)'
|
|
)
|
|
|
|
# Features
|
|
ispoe = db.Column(
|
|
db.Boolean,
|
|
default=False,
|
|
comment='Power over Ethernet capable'
|
|
)
|
|
ismanaged = db.Column(
|
|
db.Boolean,
|
|
default=False,
|
|
comment='Managed device (SNMP, web interface, etc.)'
|
|
)
|
|
|
|
# For IDF/closet locations
|
|
rackunit = db.Column(
|
|
db.String(20),
|
|
nullable=True,
|
|
comment='Rack unit position (e.g., U1, U5)'
|
|
)
|
|
|
|
# Relationships
|
|
asset = db.relationship(
|
|
'Asset',
|
|
backref=db.backref('network_device', uselist=False, lazy='joined')
|
|
)
|
|
networkdevicetype = db.relationship('NetworkDeviceType', backref='networkdevices')
|
|
vendor = db.relationship('Vendor', backref='network_devices')
|
|
model = db.relationship('Model', backref='network_devices')
|
|
|
|
__table_args__ = (
|
|
db.Index('idx_netdev_type', 'networkdevicetypeid'),
|
|
db.Index('idx_netdev_hostname', 'hostname'),
|
|
db.Index('idx_netdev_vendor', 'vendorid'),
|
|
)
|
|
|
|
def __repr__(self):
|
|
return f"<NetworkDevice {self.hostname or self.assetid}>"
|
|
|
|
def to_dict(self):
|
|
"""Convert to dictionary with related names."""
|
|
result = super().to_dict()
|
|
|
|
# Add related object names
|
|
if self.networkdevicetype:
|
|
result['networkdevicetypename'] = self.networkdevicetype.networkdevicetype
|
|
if self.vendor:
|
|
result['vendorname'] = self.vendor.vendor
|
|
# Same shape as machines, PCs and printers: the detail page's hero image
|
|
# binds to imageurl, and it comes from the catalog model, not the device.
|
|
if self.model:
|
|
result['modelname'] = self.model.modelnumber
|
|
if self.model.imageurl:
|
|
result['imageurl'] = self.model.imageurl
|
|
|
|
return result
|