Fix model photo upload, and give network devices the model link the page assumed
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.
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
"""Link a network device to a catalog model.
|
||||
|
||||
Machines, PCs and printers all carry modelnumberid, which is what lets an asset
|
||||
detail page show the photo uploaded against its vendor model. Network devices
|
||||
carried only vendorid, so NetworkDeviceDetail.vue rendered a hero image bound to
|
||||
`networkdevice.imageurl` that nothing could ever populate - a feature that
|
||||
looked present and could not work.
|
||||
|
||||
Nullable, with no backfill: a device whose model is not recorded is normal, and
|
||||
guessing one from the vendor would put wrong photos on real hardware.
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = 'network0002model'
|
||||
down_revision = 'network0001anchor'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# Guarded exactly like employees0002photo. On a FRESH database the tables
|
||||
# are built from the SQLAlchemy models, which already declare this column,
|
||||
# so an unconditional add fails with "duplicate column name". On an existing
|
||||
# server the column really is missing and gets added.
|
||||
bind = op.get_bind()
|
||||
insp = sa.inspect(bind)
|
||||
if 'networkdevices' not in insp.get_table_names():
|
||||
return
|
||||
cols = {c['name'] for c in insp.get_columns('networkdevices')}
|
||||
if 'modelnumberid' not in cols:
|
||||
op.add_column('networkdevices', sa.Column('modelnumberid', sa.Integer(), nullable=True))
|
||||
# Only where it is possible. SQLite has no ALTER TABLE ADD CONSTRAINT,
|
||||
# and routing this through batch_alter_table - which rebuilds the table -
|
||||
# made Alembic's column sort raise "Circular dependency detected". The
|
||||
# column is what the relationship needs; the constraint is integrity on
|
||||
# the real database.
|
||||
if bind.dialect.name != 'sqlite':
|
||||
op.create_foreign_key(
|
||||
'fk_networkdevices_modelnumberid',
|
||||
'networkdevices', 'models',
|
||||
['modelnumberid'], ['modelnumberid'],
|
||||
)
|
||||
|
||||
|
||||
def downgrade():
|
||||
bind = op.get_bind()
|
||||
insp = sa.inspect(bind)
|
||||
if 'networkdevices' not in insp.get_table_names():
|
||||
return
|
||||
cols = {c['name'] for c in insp.get_columns('networkdevices')}
|
||||
if 'modelnumberid' in cols:
|
||||
if bind.dialect.name != 'sqlite':
|
||||
op.drop_constraint('fk_networkdevices_modelnumberid', 'networkdevices',
|
||||
type_='foreignkey')
|
||||
op.drop_column('networkdevices', 'modelnumberid')
|
||||
Reference in New Issue
Block a user