From 62b235a45515fbdad568c0d97594fb058988b3ee Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 11 Aug 2022 17:30:00 +0200 Subject: [PATCH] to do binding from real device --- ereuse_devicehub/inventory/forms.py | 6 +- ereuse_devicehub/inventory/views.py | 152 +++++++++++------- .../templates/inventory/binding.html | 2 +- .../templates/inventory/device_detail.html | 2 +- 4 files changed, 99 insertions(+), 63 deletions(-) diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index b1fdb645..1864bb3b 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -1667,8 +1667,8 @@ class BindingForm(FlaskForm): self.phid.errors = [txt] return False - if self.device.is_abstract() != 'Abstract': - txt = "This is not a abstract device." + if self.device.is_abstract() not in ['Abstract', 'Real']: + txt = "This is not a Abstract or Real device." self.phid.errors = [txt] return False @@ -1682,7 +1682,7 @@ class BindingForm(FlaskForm): self.phid.errors = [txt] return False - if self.placeholder.binding: + if self.placeholder.status not in ['Abstract', 'Real']: txt = "This placeholder have a binding with other device. " txt += "Before you need to do an unbinding with this other device." self.phid.errors = [txt] diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 1da71c58..f2d72350 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -163,7 +163,7 @@ class DeviceDetailView(GenericMixin): if form_binding.validate_on_submit(): next_url = url_for( 'inventory.binding', - dhid=form_binding.device.devicehub_id, + dhid=id, phid=form_binding.placeholder.phid, ) return flask.redirect(next_url) @@ -179,72 +179,108 @@ class BindingView(GenericMixin): template_name = 'inventory/binding.html' def dispatch_request(self, dhid, phid): + self.phid = phid + self.dhid = dhid + self.next_url = url_for('inventory.device_details', id=dhid) self.get_context() - old_device = ( - Device.query.filter(Device.owner_id == g.user.id) - .filter(Device.devicehub_id == dhid) - .first() - ) - new_placeholder = ( - Placeholder.query.filter(Placeholder.owner_id == g.user.id) - .filter(Placeholder.phid == phid) - .first() - ) - - if not old_device or old_device.placeholder.status != 'Abstract': - next_url = url_for('inventory.device_details', id=dhid) - messages.error('Device Dhid: "{}" not is a Abstract device!'.format(dhid)) - return flask.redirect(next_url) - - if not new_placeholder or new_placeholder.status != 'Real': - next_url = url_for('inventory.device_details', id=dhid) - messages.error('Device Phid: "{}" not is a Abstract device!'.format(phid)) - return flask.redirect(next_url) - - old_placeholder = old_device.placeholder - new_device = new_placeholder.device - abstract_device = old_placeholder.binding - new_dhid = new_device.devicehub_id + self.get_objects() + if self.check_errors(): + return flask.redirect(self.next_url) if request.method == 'POST': - - for plog in PlaceholdersLog.query.filter_by( - placeholder_id=old_placeholder.id - ): - db.session.delete(plog) - - for ac in old_device.actions: - ac.devices.add(new_device) - ac.devices.remove(old_device) - for act in ac.actions_device: - if act.device == old_device: - db.session.delete(act) - - for tag in list(old_device.tags): - tag.device = new_device - - db.session.delete(old_device) - abstract_device.binding = new_placeholder - db.session.commit() - - next_url = url_for('inventory.device_details', id=new_dhid) - messages.success( - 'Device Dhid: "{}" bind successfully with Phid: {}!'.format(dhid, phid) - ) - return flask.redirect(next_url) + return self.post() self.context.update( { - 'new_placeholder': new_placeholder, - 'old_placeholder': old_placeholder, + 'new_placeholder': self.new_placeholder, + 'old_placeholder': self.old_placeholder, 'page_title': 'Binding confirm', - 'actions': list(old_device.actions) + list(new_device.actions), - 'tags': list(old_device.tags) + list(new_device.tags), + 'actions': list(self.old_device.actions) + + list(self.new_device.actions), + 'tags': list(self.old_device.tags) + list(self.new_device.tags), + 'dhid': self.dhid, } ) return flask.render_template(self.template_name, **self.context) + def check_errors(self): + if not self.new_placeholder: + messages.error('Device Phid: "{}" not exist!'.format(self.phid)) + return True + + if self.old_device.placeholder.status != 'Abstract': + messages.error( + 'Device Dhid: "{}" is not a Abstract device!'.format(self.dhid) + ) + return True + + if self.new_placeholder.status == 'Twin': + messages.error('Device Phid: "{}" is a Twin device!'.format(self.phid)) + return True + + if self.new_placeholder.status == self.old_placeholder.status: + txt = 'Device Phid: "{}" and device Dhid: "{}" have the same status, "{}"!'.format( + self.phid, self.dhid, self.new_placeholder.status + ) + messages.error(txt) + return True + + def get_objects(self): + self.old_device = ( + Device.query.filter(Device.owner_id == g.user.id) + .filter(Device.devicehub_id == self.dhid) + .one() + ) + self.new_placeholder = ( + Placeholder.query.filter(Placeholder.owner_id == g.user.id) + .filter(Placeholder.phid == self.phid) + .first() + ) + + if not self.new_placeholder: + return + + if self.old_device.placeholder.status == 'Abstract': + self.new_device = self.new_placeholder.device + self.old_placeholder = self.old_device.placeholder + elif self.old_device.placeholder.status == 'Real': + self.new_device = self.old_device + self.old_placeholder = self.new_placeholder + self.old_device = self.old_placeholder.device + self.new_placeholder = self.new_device.placeholder + + self.abstract_device = self.old_placeholder.binding + self.new_dhid = self.new_device.devicehub_id + + def post(self): + for plog in PlaceholdersLog.query.filter_by( + placeholder_id=self.old_placeholder.id + ): + db.session.delete(plog) + + for ac in self.old_device.actions: + ac.devices.add(self.new_device) + ac.devices.remove(self.old_device) + for act in ac.actions_device: + if act.device == self.old_device: + db.session.delete(act) + + for tag in list(self.old_device.tags): + tag.device = self.new_device + + db.session.delete(self.old_device) + self.abstract_device.binding = self.new_placeholder + db.session.commit() + + next_url = url_for('inventory.device_details', id=self.new_dhid) + messages.success( + 'Device Dhid: "{}" bind successfully with Phid: {}!'.format( + self.dhid, self.phid + ) + ) + return flask.redirect(next_url) + class UnBindingView(GenericMixin): methods = ['GET', 'POST'] @@ -272,9 +308,9 @@ class UnBindingView(GenericMixin): self.get_context() if request.method == 'POST': - new_device = self.clone_device(placeholder.binding) - new_dhid = new_device.devicehub_id - next_url = url_for('inventory.device_details', id=new_dhid) + dhid = placeholder.device.devicehub_id + self.clone_device(placeholder.binding) + next_url = url_for('inventory.device_details', id=dhid) messages.success('Device Phid: "{}" unbind successfully!'.format(phid)) return flask.redirect(next_url) diff --git a/ereuse_devicehub/templates/inventory/binding.html b/ereuse_devicehub/templates/inventory/binding.html index d453d154..5d4b0ecb 100644 --- a/ereuse_devicehub/templates/inventory/binding.html +++ b/ereuse_devicehub/templates/inventory/binding.html @@ -184,7 +184,7 @@
- Cancel + Cancel
diff --git a/ereuse_devicehub/templates/inventory/device_detail.html b/ereuse_devicehub/templates/inventory/device_detail.html index 9dcc3ead..875d3508 100644 --- a/ereuse_devicehub/templates/inventory/device_detail.html +++ b/ereuse_devicehub/templates/inventory/device_detail.html @@ -46,7 +46,7 @@ - {% if placeholder.status == 'Abstract' %} + {% if placeholder.status in ['Abstract', 'Real'] %}