diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index a797a705..1da71c58 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -180,65 +180,66 @@ class BindingView(GenericMixin): def dispatch_request(self, dhid, phid): self.get_context() - device = ( + old_device = ( Device.query.filter(Device.owner_id == g.user.id) .filter(Device.devicehub_id == dhid) - .one() + .first() ) - placeholder = ( + new_placeholder = ( Placeholder.query.filter(Placeholder.owner_id == g.user.id) .filter(Placeholder.phid == phid) - .one() + .first() ) - if device.is_abstract() != 'Abstract': + if not old_device or old_device.placeholder.status != 'Abstract': next_url = url_for('inventory.device_details', id=dhid) - messages.error('Device "{}" not is a Abstract device!'.format(dhid)) + messages.error('Device Dhid: "{}" not is a Abstract device!'.format(dhid)) return flask.redirect(next_url) - if device.placeholder: - device = device.placeholder.binding - dhid = device.devicehub_id + 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 if request.method == 'POST': - old_placeholder = device.binding - old_device_placeholder = old_placeholder.device - if old_placeholder.is_abstract: - for plog in PlaceholdersLog.query.filter_by( - placeholder_id=old_placeholder.id - ): - db.session.delete(plog) + for plog in PlaceholdersLog.query.filter_by( + placeholder_id=old_placeholder.id + ): + db.session.delete(plog) - for ac in old_device_placeholder.actions: - ac.devices.add(placeholder.device) - ac.devices.remove(old_device_placeholder) - for act in ac.actions_device: - if act.device == old_device_placeholder: - db.session.delete(act) + 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_placeholder.tags): - tag.device = placeholder.device + for tag in list(old_device.tags): + tag.device = new_device - db.session.delete(old_device_placeholder) - - device.binding = placeholder + db.session.delete(old_device) + abstract_device.binding = new_placeholder db.session.commit() - next_url = url_for('inventory.device_details', id=dhid) + + next_url = url_for('inventory.device_details', id=new_dhid) messages.success( - 'Device "{}" bind successfully with {}!'.format(dhid, phid) + 'Device Dhid: "{}" bind successfully with Phid: {}!'.format(dhid, phid) ) return flask.redirect(next_url) self.context.update( { - 'device': device.binding.device, - 'placeholder': placeholder, + 'new_placeholder': new_placeholder, + 'old_placeholder': old_placeholder, 'page_title': 'Binding confirm', - 'actions': list(device.binding.device.actions) - + list(placeholder.device.actions), - 'tags': list(device.binding.device.tags) - + list(placeholder.device.tags), + 'actions': list(old_device.actions) + list(new_device.actions), + 'tags': list(old_device.tags) + list(new_device.tags), } ) @@ -256,31 +257,29 @@ class UnBindingView(GenericMixin): .filter(Placeholder.phid == phid) .one() ) - if not placeholder.binding: + if not placeholder.binding or placeholder.status != 'Twin': next_url = url_for( 'inventory.device_details', id=placeholder.device.devicehub_id ) return flask.redirect(next_url) - device = placeholder.binding - - if device.is_abstract() != 'Twin': - dhid = device.devicehub_id + if placeholder.status != 'Twin': + dhid = placeholder.device.devicehub_id next_url = url_for('inventory.device_details', id=dhid) - messages.error('Device "{}" not is a Twin device!'.format(dhid)) + messages.error('Device Dhid: "{}" not is a Twin device!'.format(dhid)) return flask.redirect(next_url) self.get_context() if request.method == 'POST': - new_device = self.clone_device(device) - next_url = url_for('inventory.device_details', id=new_device.devicehub_id) - messages.success('Device "{}" unbind successfully!'.format(phid)) + new_device = self.clone_device(placeholder.binding) + new_dhid = new_device.devicehub_id + next_url = url_for('inventory.device_details', id=new_dhid) + messages.success('Device Phid: "{}" unbind successfully!'.format(phid)) return flask.redirect(next_url) self.context.update( { - 'device': device, 'placeholder': placeholder, 'page_title': 'Unbinding confirm', } diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index 1a95c3b3..5bc25477 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -935,6 +935,14 @@ class Placeholder(Thing): actions.reverse() return actions + @property + def status(self): + if self.is_abstract: + return 'Abstract' + if self.binding: + return 'Twin' + return 'Real' + class Computer(Device): """A chassis with components inside that can be processed diff --git a/ereuse_devicehub/templates/inventory/binding.html b/ereuse_devicehub/templates/inventory/binding.html index 087b4e85..d453d154 100644 --- a/ereuse_devicehub/templates/inventory/binding.html +++ b/ereuse_devicehub/templates/inventory/binding.html @@ -26,109 +26,104 @@ Basic Data - Info to be Entered - Info to be Decoupled + Info Twin device + Info Abstract device - - PHID: - {{ placeholder.phid or '' }} - {{ device.placeholder.phid or '' }} - Manufacturer: - {{ placeholder.device.manufacturer or '' }} - {{ device.manufacturer or '' }} + {{ new_placeholder.device.manufacturer or '' }} + {{ old_placeholder.device.manufacturer or '' }} Model: - {{ placeholder.device.model or '' }} - {{ device.model or '' }} + {{ new_placeholder.device.model or '' }} + {{ old_placeholder.device.model or '' }} Serial Number: - {{ placeholder.device.serial_number or '' }} - {{ device.serial_number or '' }} + {{ new_placeholder.device.serial_number or '' }} + {{ old_placeholder.device.serial_number or '' }} Brand: - {{ placeholder.device.brand or '' }} - {{ device.brand or '' }} + {{ new_placeholder.device.brand or '' }} + {{ old_placeholder.device.brand or '' }} Sku: - {{ placeholder.device.sku or '' }} - {{ device.sku or '' }} + {{ new_placeholder.device.sku or '' }} + {{ old_placeholder.device.sku or '' }} Generation: - {{ placeholder.device.generation or '' }} - {{ device.generation or '' }} + {{ new_placeholder.device.generation or '' }} + {{ old_placeholder.device.generation or '' }} Version: - {{ placeholder.device.version or '' }} - {{ device.version or '' }} + {{ new_placeholder.device.version or '' }} + {{ old_placeholder.device.version or '' }} Weight: - {{ placeholder.device.weight or '' }} - {{ device.weight or '' }} + {{ new_placeholder.device.weight or '' }} + {{ old_placeholder.device.weight or '' }} Width: - {{ placeholder.device.width or '' }} - {{ device.width or '' }} + {{ new_placeholder.device.width or '' }} + {{ old_placeholder.device.width or '' }} Height: - {{ placeholder.device.height or '' }} - {{ device.height or '' }} + {{ new_placeholder.device.height or '' }} + {{ old_placeholder.device.height or '' }} Depth: - {{ placeholder.device.depth or '' }} - {{ device.depth or '' }} + {{ new_placeholder.device.depth or '' }} + {{ old_placeholder.device.depth or '' }} Color: - {{ placeholder.device.color or '' }} - {{ device.color or '' }} + {{ new_placeholder.device.color or '' }} + {{ old_placeholder.device.color or '' }} Production date: - {{ placeholder.device.production_date or '' }} - {{ device.production_date or '' }} + {{ new_placeholder.device.production_date or '' }} + {{ old_placeholder.device.production_date or '' }} Variant: - {{ placeholder.device.variant or '' }} - {{ device.variant or '' }} + {{ new_placeholder.device.variant or '' }} + {{ old_placeholder.device.variant or '' }}
- {% if placeholder.device.components or device.components %} + {% if new_placeholder.device.components or old_placeholder.device.components %}

Components

- - + + @@ -144,8 +139,8 @@
Info to be EnteredInfo to be DecoupledInfo Twin deviceInfo Abstract device
- {% for c in placeholder.device.components %} + {% for c in new_placeholder.device.components %} * {{ c.verbose_name }}
{% endfor %}
- {% for c in device.components %} + {% for c in old_placeholder.device.components %} * {{ c.verbose_name }}
{% endfor %}
- - + + @@ -169,8 +164,8 @@
Info to be EnteredInfo to be DecoupledInfo Twin deviceInfo Abstract device
- - + + @@ -189,7 +184,7 @@
- Cancel + Cancel
diff --git a/ereuse_devicehub/templates/inventory/device_detail.html b/ereuse_devicehub/templates/inventory/device_detail.html index 3a8ccdb0..9dcc3ead 100644 --- a/ereuse_devicehub/templates/inventory/device_detail.html +++ b/ereuse_devicehub/templates/inventory/device_detail.html @@ -46,13 +46,13 @@ - {% if device.is_abstract() == 'Abstract' %} + {% if placeholder.status == 'Abstract' %} {% endif %} - {% if device.is_abstract() == 'Twin' %} + {% if placeholder.status == 'Twin' %} @@ -67,7 +67,7 @@
(Edit Device)
-
{{ placeholder.device.is_abstract() }}
+
{{ placeholder.status }}
@@ -95,6 +95,7 @@
{{ placeholder.device.serial_number or ''}}
+ {% if placeholder.binding %}
Details Abstract parth
Type
@@ -115,6 +116,7 @@
Serial Number
{{ placeholder.binding.serial_number or ''}}
+ {% endif %}
@@ -202,10 +204,15 @@
-
Components Details
- {% if device.binding %} +
Components Real parth
- {% for component in device.components|sort(attribute='type') %} + {{ placeholder.components or '' }} +
+ + {% if placeholder.binding %} +
Components Abstract parth
+
+ {% for component in placeholder.binding.components|sort(attribute='type') %}
{{ component.type }}
@@ -223,13 +230,9 @@
{% endfor %}
- {% else %} -
- {{ device.placeholder.components or '' }} -
{% endif %}
- {% if device.is_abstract() %} + {% if placeholder.status %}
Binding
diff --git a/ereuse_devicehub/templates/inventory/unbinding.html b/ereuse_devicehub/templates/inventory/unbinding.html index 457b4a74..c041bfea 100644 --- a/ereuse_devicehub/templates/inventory/unbinding.html +++ b/ereuse_devicehub/templates/inventory/unbinding.html @@ -26,84 +26,79 @@
- - + + - - - - - - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -111,26 +106,21 @@
- {% if placeholder.device.components or device.components %} + {% if placeholder.components %}

Components

Info to be EnteredInfo to be DecoupledInfo Twin deviceInfo Abstract device
Basic DataInfo to be EnteredInfo to be DecoupledInfo Abstract deviceInfo Real device
PHID:{{ placeholder.phid or '' }}
Manufacturer:{{ device.manufacturer or '' }}{{ placeholder.binding.manufacturer or '' }} {{ placeholder.device.manufacturer or '' }}
Model:{{ device.model or '' }}{{ placeholder.binding.model or '' }} {{ placeholder.device.model or '' }}
Serial Number:{{ device.serial_number or '' }}{{ placeholder.binding.serial_number or '' }} {{ placeholder.device.serial_number or '' }}
Brand:{{ device.brand or '' }}{{ placeholder.binding.brand or '' }} {{ placeholder.device.brand or '' }}
Sku:{{ device.sku or '' }}{{ placeholder.binding.sku or '' }} {{ placeholder.device.sku or '' }}
Generation:{{ device.generation or '' }}{{ placeholder.binding.generation or '' }} {{ placeholder.device.generation or '' }}
Version:{{ device.version or '' }}{{ placeholder.binding.version or '' }} {{ placeholder.device.version or '' }}
Weight:{{ device.weight or '' }}{{ placeholder.binding.weight or '' }} {{ placeholder.device.weight or '' }}
Width:{{ device.width or '' }}{{ placeholder.binding.width or '' }} {{ placeholder.device.width or '' }}
Height:{{ device.height or '' }}{{ placeholder.binding.height or '' }} {{ placeholder.device.height or '' }}
Depth:{{ device.depth or '' }}{{ placeholder.binding.depth or '' }} {{ placeholder.device.depth or '' }}
Color:{{ device.color or '' }}{{ placeholder.binding.color or '' }} {{ placeholder.device.color or '' }}
Production date:{{ device.production_date or '' }}{{ placeholder.binding.production_date or '' }} {{ placeholder.device.production_date or '' }}
Variant:{{ device.variant or '' }}{{ placeholder.binding.variant or '' }} {{ placeholder.device.variant or '' }}
- - + + @@ -139,19 +129,19 @@
- {% if placeholder.device.manual_actions or device.manual_actions %} + {% if placeholder.device.manual_actions or placeholder.binding.manual_actions %}

Actions

Info to be EnteredInfo to be DecoupledInfo Abstract deviceInfo Real device
- {% for c in device.components %} - * {{ c.verbose_name }}
- {% endfor %}
- {% for c in placeholder.device.components %} - * {{ c.verbose_name }}
- {% endfor %} + {{ placeholder.components or ''}}
- - + + @@ -165,6 +155,29 @@
Info to be EnteredInfo to be DecoupledInfo Abstract deviceInfo Real device
- {% for a in device.manual_actions %} + {% for a in placeholder.binding.manual_actions %} * {{ a.t }}
{% endfor %}
{% endif %} + {% if placeholder.device.tags %} +

Tags

+ + + + + + + + + + + + + +
Info Abstract deviceInfo Real device
+ + {% for a in placeholder.device.tags %} + * {{ a.t }}
+ {% endfor %} +
+ {% endif %} +
Cancel