From 9782ab68d65cf162c360907a31c9345241d62e0e Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 8 Aug 2022 18:09:25 +0200 Subject: [PATCH] join actions and tags --- ereuse_devicehub/inventory/forms.py | 2 +- ereuse_devicehub/inventory/views.py | 56 ++++++++++++++++--- .../templates/inventory/binding.html | 30 ++++++++-- .../templates/inventory/device_detail.html | 18 +++++- .../templates/inventory/device_list.html | 2 +- 5 files changed, 91 insertions(+), 17 deletions(-) diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index 0e8202ca..9bf31d33 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -1669,7 +1669,7 @@ class BindingForm(FlaskForm): if not self.placeholder: self.placeholder = Placeholder.query.filter( - Placeholder.phid == self.phid.data, Placeholder.owner == g.user + Placeholder.phid == self.phid.data.strip(), Placeholder.owner == g.user ).first() if not self.placeholder: diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index b286bf0f..8d0eafb7 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -37,7 +37,7 @@ from ereuse_devicehub.inventory.forms import ( ) from ereuse_devicehub.labels.forms import PrintLabelsForm from ereuse_devicehub.parser.models import PlaceholdersLog, SnapshotsLog -from ereuse_devicehub.resources.action.models import Trade +from ereuse_devicehub.resources.action.models import ActionDevice, Trade from ereuse_devicehub.resources.device.models import ( Computer, DataStorage, @@ -194,12 +194,23 @@ class BindingView(GenericMixin): 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 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) + db.session.delete(old_device_placeholder) + for tag in list(old_device_placeholder.tags): + tag.device = placeholder.device device.binding = placeholder db.session.commit() @@ -209,11 +220,16 @@ class BindingView(GenericMixin): ) return flask.redirect(next_url) + # import pdb; pdb.set_trace() self.context.update( { 'device': device.binding.device, 'placeholder': 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), } ) @@ -242,10 +258,8 @@ class UnBindingView(GenericMixin): self.get_context() if request.method == 'POST': - self.clone_device(device) - next_url = url_for( - 'inventory.device_details', id=placeholder.device.devicehub_id - ) + 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)) return flask.redirect(next_url) @@ -678,7 +692,13 @@ class ExportsView(View): def devices_list(self): """Get device query and put information in csv format.""" data = StringIO() - cw = csv.writer(data, delimiter=';', lineterminator="\n", quotechar='"', quoting=csv.QUOTE_ALL) + cw = csv.writer( + data, + delimiter=';', + lineterminator="\n", + quotechar='"', + quoting=csv.QUOTE_ALL, + ) first = True for device in self.find_devices(): @@ -693,7 +713,13 @@ class ExportsView(View): def metrics(self): """Get device query and put information in csv format.""" data = StringIO() - cw = csv.writer(data, delimiter=';', lineterminator="\n", quotechar='"', quoting=csv.QUOTE_ALL) + cw = csv.writer( + data, + delimiter=';', + lineterminator="\n", + quotechar='"', + quoting=csv.QUOTE_ALL, + ) first = True devs_id = [] # Get the allocate info @@ -757,7 +783,13 @@ class ExportsView(View): def lots_export(self): data = StringIO() - cw = csv.writer(data, delimiter=';', lineterminator="\n", quotechar='"', quoting=csv.QUOTE_ALL) + cw = csv.writer( + data, + delimiter=';', + lineterminator="\n", + quotechar='"', + quoting=csv.QUOTE_ALL, + ) cw.writerow( [ @@ -827,7 +859,13 @@ class ExportsView(View): def devices_lots_export(self): data = StringIO() - cw = csv.writer(data, delimiter=';', lineterminator="\n", quotechar='"', quoting=csv.QUOTE_ALL) + cw = csv.writer( + data, + delimiter=';', + lineterminator="\n", + quotechar='"', + quoting=csv.QUOTE_ALL, + ) head = [ 'DHID', 'Lot Id', diff --git a/ereuse_devicehub/templates/inventory/binding.html b/ereuse_devicehub/templates/inventory/binding.html index e26fb1bb..087b4e85 100644 --- a/ereuse_devicehub/templates/inventory/binding.html +++ b/ereuse_devicehub/templates/inventory/binding.html @@ -139,7 +139,7 @@
- {% if placeholder.device.actions or device.actions %} + {% if actions %}

Actions

@@ -151,15 +151,37 @@ + + +
- {% for a in placeholder.device.actions %} + {% for a in actions %} * {{ a.t }}
{% endfor %}
- {% for a in device.actions %} - * {{ a.t }}
+
+ {% endif %} + +
+ + {% if tags %} +

Tags

+ + + + + + + + + + +
Info to be EnteredInfo to be Decoupled
+ {% for tag in tags %} + * {{ tag.id }}
{% endfor %}
+
diff --git a/ereuse_devicehub/templates/inventory/device_detail.html b/ereuse_devicehub/templates/inventory/device_detail.html index bade9c63..81b02e25 100644 --- a/ereuse_devicehub/templates/inventory/device_detail.html +++ b/ereuse_devicehub/templates/inventory/device_detail.html @@ -68,7 +68,7 @@ {% endif %} - {% if device.placeholder and placeholder.binding %} + {% if device.placeholder and placeholder.binding and device.is_abstract() != 'Abstract'%} @@ -79,7 +79,21 @@
Details
- {% if device.placeholder %}(edit){% endif %} + {% if device.placeholder %} +
+
+ (Edit Device) +
+
{{ device.is_abstract() }}
+
+ {% endif %} + + {% if device.placeholder %} +
+
Phid
+
{{ device.placeholder.phid }}
+
+ {% endif %}
Type
diff --git a/ereuse_devicehub/templates/inventory/device_list.html b/ereuse_devicehub/templates/inventory/device_list.html index 9ed4eb2d..b2ed449d 100644 --- a/ereuse_devicehub/templates/inventory/device_list.html +++ b/ereuse_devicehub/templates/inventory/device_list.html @@ -402,7 +402,7 @@ Title DHID PHID - Is Abstract + Type Unique Identifiers Lifecycle Status Allocated Status