fix cascade delete in placeholder

This commit is contained in:
Cayo Puigdefabregas 2022-08-02 12:41:31 +02:00
parent 7e4deea7fe
commit 9c2d52a7f7
2 changed files with 14 additions and 8 deletions

View file

@ -1,5 +1,5 @@
import csv
import copy import copy
import csv
import logging import logging
import os import os
from distutils.util import strtobool from distutils.util import strtobool
@ -193,12 +193,14 @@ class BindingView(GenericMixin):
if request.method == 'POST': if request.method == 'POST':
old_placeholder = device.binding old_placeholder = device.binding
old_device_placeholder = old_placeholder.device
if old_placeholder.is_abstract: if old_placeholder.is_abstract:
for plog in PlaceholdersLog.query.filter_by( for plog in PlaceholdersLog.query.filter_by(
placeholder_id=old_placeholder.id placeholder_id=old_placeholder.id
): ):
db.session.delete(plog) db.session.delete(plog)
db.session.delete(old_placeholder) db.session.delete(old_device_placeholder)
device.binding = placeholder device.binding = placeholder
db.session.commit() db.session.commit()
next_url = url_for('inventory.device_details', id=dhid) next_url = url_for('inventory.device_details', id=dhid)
@ -230,7 +232,9 @@ class UnBindingView(GenericMixin):
.one() .one()
) )
if not placeholder.binding: if not placeholder.binding:
next_url = url_for('inventory.device_details', id=placeholder.device.devicehub_id) next_url = url_for(
'inventory.device_details', id=placeholder.device.devicehub_id
)
return flask.redirect(next_url) return flask.redirect(next_url)
device = placeholder.binding device = placeholder.binding
@ -239,10 +243,10 @@ class UnBindingView(GenericMixin):
if request.method == 'POST': if request.method == 'POST':
self.clone_device(device) self.clone_device(device)
next_url = url_for('inventory.device_details', id=placeholder.device.devicehub_id) next_url = url_for(
messages.success( 'inventory.device_details', id=placeholder.device.devicehub_id
'Device "{}" unbind successfully!'.format(phid)
) )
messages.success('Device "{}" unbind successfully!'.format(phid))
return flask.redirect(next_url) return flask.redirect(next_url)
self.context.update( self.context.update(
@ -258,7 +262,6 @@ class UnBindingView(GenericMixin):
def clone_device(self, device): def clone_device(self, device):
if device.binding.is_abstract: if device.binding.is_abstract:
return return
# import pdb; pdb.set_trace()
dict_device = copy.copy(device.__dict__) dict_device = copy.copy(device.__dict__)
dict_device.pop('_sa_instance_state') dict_device.pop('_sa_instance_state')
@ -269,6 +272,8 @@ class UnBindingView(GenericMixin):
dict_device.pop('components', None) dict_device.pop('components', None)
dict_device.pop('tags', None) dict_device.pop('tags', None)
dict_device.pop('system_uuid', None) dict_device.pop('system_uuid', None)
dict_device.pop('binding', None)
dict_device.pop('placeholder', None)
new_device = device.__class__(**dict_device) new_device = device.__class__(**dict_device)
db.session.add(new_device) db.session.add(new_device)

View file

@ -33,6 +33,7 @@ from sqlalchemy_utils import ColorType
from stdnum import imei, meid from stdnum import imei, meid
from teal.db import ( from teal.db import (
CASCADE_DEL, CASCADE_DEL,
CASCADE_OWN,
POLYMORPHIC_ID, POLYMORPHIC_ID,
POLYMORPHIC_ON, POLYMORPHIC_ON,
URL, URL,
@ -883,7 +884,7 @@ class Placeholder(Thing):
) )
device = db.relationship( device = db.relationship(
Device, Device,
backref=backref('placeholder', lazy=True, uselist=False), backref=backref('placeholder', lazy=True, cascade="all, delete-orphan", uselist=False),
primaryjoin=device_id == Device.id, primaryjoin=device_id == Device.id,
) )
device_id.comment = "datas of the placeholder" device_id.comment = "datas of the placeholder"