From 7e4deea7fefc246a1abadc63ce257bb8eb30140f Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 29 Jul 2022 17:02:27 +0200 Subject: [PATCH] add unbinding --- ereuse_devicehub/inventory/views.py | 70 +++++++ ereuse_devicehub/resources/device/models.py | 18 ++ .../templates/inventory/device_detail.html | 6 + .../templates/inventory/unbinding.html | 185 ++++++++++++++++++ 4 files changed, 279 insertions(+) create mode 100644 ereuse_devicehub/templates/inventory/unbinding.html diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 63cd6dc6..a26df083 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -1,4 +1,5 @@ import csv +import copy import logging import os from distutils.util import strtobool @@ -217,6 +218,72 @@ class BindingView(GenericMixin): return flask.render_template(self.template_name, **self.context) +class UnBindingView(GenericMixin): + methods = ['GET', 'POST'] + decorators = [login_required] + template_name = 'inventory/unbinding.html' + + def dispatch_request(self, phid): + placeholder = ( + Placeholder.query.filter(Placeholder.owner_id == g.user.id) + .filter(Placeholder.phid == phid) + .one() + ) + if not placeholder.binding: + next_url = url_for('inventory.device_details', id=placeholder.device.devicehub_id) + return flask.redirect(next_url) + + device = placeholder.binding + + self.get_context() + + if request.method == 'POST': + self.clone_device(device) + next_url = url_for('inventory.device_details', id=placeholder.device.devicehub_id) + messages.success( + 'Device "{}" unbind successfully!'.format(phid) + ) + return flask.redirect(next_url) + + self.context.update( + { + 'device': device, + 'placeholder': placeholder, + 'page_title': 'Unbinding confirm', + } + ) + + return flask.render_template(self.template_name, **self.context) + + def clone_device(self, device): + if device.binding.is_abstract: + return + # import pdb; pdb.set_trace() + + dict_device = copy.copy(device.__dict__) + dict_device.pop('_sa_instance_state') + dict_device.pop('id', None) + dict_device.pop('devicehub_id', None) + dict_device.pop('actions_multiple', None) + dict_device.pop('actions_one', None) + dict_device.pop('components', None) + dict_device.pop('tags', None) + dict_device.pop('system_uuid', None) + new_device = device.__class__(**dict_device) + db.session.add(new_device) + + if hasattr(device, 'components'): + for c in device.components: + if c.binding: + c.binding.device.parent = new_device + + placeholder = Placeholder(device=new_device, binding=device, is_abstract=True) + db.session.add(placeholder) + db.session.commit() + + return new_device + + class LotCreateView(GenericMixin): methods = ['GET', 'POST'] decorators = [login_required] @@ -1064,3 +1131,6 @@ devices.add_url_rule( devices.add_url_rule( '/binding///', view_func=BindingView.as_view('binding') ) +devices.add_url_rule( + '/unbinding//', view_func=UnBindingView.as_view('unbinding') +) diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index ab310946..ae65b0a3 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -215,6 +215,24 @@ class Device(Thing): def reverse_actions(self) -> list: return reversed(self.actions) + @property + def manual_actions(self) -> list: + mactions = [ + 'ActionDevice', + 'Allocate', + 'DataWipe', + 'Deallocate', + 'Management', + 'Prepare', + 'Ready', + 'Recycling', + 'Refurbish', + 'ToPrepare', + 'ToRepair', + 'Use', + ] + return [a for a in self.actions if a in mactions] + @property def actions(self) -> list: """All the actions where the device participated, including: diff --git a/ereuse_devicehub/templates/inventory/device_detail.html b/ereuse_devicehub/templates/inventory/device_detail.html index 2274e676..bade9c63 100644 --- a/ereuse_devicehub/templates/inventory/device_detail.html +++ b/ereuse_devicehub/templates/inventory/device_detail.html @@ -68,6 +68,12 @@ {% endif %} + {% if device.placeholder and placeholder.binding %} + + {% endif %} +
diff --git a/ereuse_devicehub/templates/inventory/unbinding.html b/ereuse_devicehub/templates/inventory/unbinding.html new file mode 100644 index 00000000..457b4a74 --- /dev/null +++ b/ereuse_devicehub/templates/inventory/unbinding.html @@ -0,0 +1,185 @@ +{% extends "ereuse_devicehub/base_site.html" %} +{% block main %} + +
+

{{ title }}

+ +
+ +
+
+
+ +
+
+ +
+
{{ title }}
+

Please check that the information is correct.

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Basic DataInfo to be EnteredInfo to be Decoupled
PHID:{{ placeholder.phid or '' }}
Manufacturer:{{ device.manufacturer or '' }}{{ placeholder.device.manufacturer or '' }}
Model:{{ device.model or '' }}{{ placeholder.device.model or '' }}
Serial Number:{{ device.serial_number or '' }}{{ placeholder.device.serial_number or '' }}
Brand:{{ device.brand or '' }}{{ placeholder.device.brand or '' }}
Sku:{{ device.sku or '' }}{{ placeholder.device.sku or '' }}
Generation:{{ device.generation or '' }}{{ placeholder.device.generation or '' }}
Version:{{ device.version or '' }}{{ placeholder.device.version or '' }}
Weight:{{ device.weight or '' }}{{ placeholder.device.weight or '' }}
Width:{{ device.width or '' }}{{ placeholder.device.width or '' }}
Height:{{ device.height or '' }}{{ placeholder.device.height or '' }}
Depth:{{ device.depth or '' }}{{ placeholder.device.depth or '' }}
Color:{{ device.color or '' }}{{ placeholder.device.color or '' }}
Production date:{{ device.production_date or '' }}{{ placeholder.device.production_date or '' }}
Variant:{{ device.variant or '' }}{{ placeholder.device.variant or '' }}
+ +
+ + {% if placeholder.device.components or device.components %} +

Components

+ + + + + + + + + + + + + +
Info to be EnteredInfo to be Decoupled
+ {% for c in device.components %} + * {{ c.verbose_name }}
+ {% endfor %} +
+ {% for c in placeholder.device.components %} + * {{ c.verbose_name }}
+ {% endfor %} +
+ {% endif %} + +
+ + {% if placeholder.device.manual_actions or device.manual_actions %} +

Actions

+ + + + + + + + + + + + + +
Info to be EnteredInfo to be Decoupled
+ {% for a in device.manual_actions %} + * {{ a.t }}
+ {% endfor %} +
+ {% for a in placeholder.device.manual_actions %} + * {{ a.t }}
+ {% endfor %} +
+ {% endif %} + +
+
+ Cancel + +
+
+ +
+ +
+ +
+ +
+
+
+
+{% endblock main %}