Skip to content
Snippets Groups Projects
Commit 0c5c388e authored by Benjamin Bertrand's avatar Benjamin Bertrand
Browse files

Update cnames / interface relationship

Add delete and delete-orphan options to the cascade behavior:
- delete: delete from database associated cnames when deleting an
  interface
- delete-orphan: delete cname when removing it from the list of
  interface.cnames
parent a49e002c
No related branches found
No related tags found
No related merge requests found
...@@ -487,7 +487,11 @@ class Interface(db.Model): ...@@ -487,7 +487,11 @@ class Interface(db.Model):
mac_id = db.Column(db.Integer, db.ForeignKey('mac.id')) mac_id = db.Column(db.Integer, db.ForeignKey('mac.id'))
host_id = db.Column(db.Integer, db.ForeignKey('host.id')) host_id = db.Column(db.Integer, db.ForeignKey('host.id'))
cnames = db.relationship('Cname', backref='interface') # Add delete and delete-orphan options to automatically delete cnames when:
# - deleting an interface
# - de-associating a cname (removing it from the interface.cnames list)
cnames = db.relationship('Cname', backref='interface',
cascade='all, delete, delete-orphan')
tags = db.relationship('Tag', secondary=interfacetags_table, lazy='subquery', tags = db.relationship('Tag', secondary=interfacetags_table, lazy='subquery',
backref=db.backref('interfaces', lazy=True)) backref=db.backref('interfaces', lazy=True))
......
...@@ -160,8 +160,10 @@ def edit_interface(name): ...@@ -160,8 +160,10 @@ def edit_interface(name):
for (index, cname) in enumerate(interface.cnames): for (index, cname) in enumerate(interface.cnames):
if cname.name not in new_cnames_string: if cname.name not in new_cnames_string:
current_app.logger.debug(f'Deleting cname: {cname}') current_app.logger.debug(f'Deleting cname: {cname}')
interface.cnames.pop(index) # Removing the cname from interface.cnames list will
db.session.delete(cname) # delete it from the database due to the cascade
# delete-orphan option defined on the model
del interface.cnames[index]
# Add new cnames # Add new cnames
for name in new_cnames_string: for name in new_cnames_string:
if name not in cnames_string: if name not in cnames_string:
...@@ -189,6 +191,9 @@ def edit_interface(name): ...@@ -189,6 +191,9 @@ def edit_interface(name):
def delete_interface(): def delete_interface():
interface = models.Interface.query.get_or_404(request.form['interface_id']) interface = models.Interface.query.get_or_404(request.form['interface_id'])
hostname = interface.host.name hostname = interface.host.name
# Deleting the interface will also delete all
# associated cnames due to the cascade delete option
# defined on the model
db.session.delete(interface) db.session.delete(interface)
db.session.commit() db.session.commit()
flash(f'Interface {interface.name} has been deleted', 'success') flash(f'Interface {interface.name} has been deleted', 'success')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment