From bdf04061dc398e29998afa5fcef958bb4a1bdc51 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 30 Aug 2022 13:10:35 +0200 Subject: [PATCH] mv unique identifiers buttons to device details --- ereuse_devicehub/inventory/forms.py | 37 ++++++++---------- ereuse_devicehub/inventory/views.py | 26 ++++++++----- .../templates/ereuse_devicehub/base_site.html | 8 ++++ .../templates/inventory/addDevicestag.html | 12 ++---- .../templates/inventory/device_detail.html | 39 +++++++++++++++++++ .../templates/inventory/device_list.html | 33 ---------------- .../inventory/tag_unlink_device.html | 2 +- 7 files changed, 85 insertions(+), 72 deletions(-) diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index 1864bb3b..83d18ad9 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -137,11 +137,11 @@ class FilterForm(FlaskForm): self.lot = self.lots.filter(Lot.id == self.lot_id).one() device_ids = (d.id for d in self.lot.devices) self.devices = Device.query.filter(Device.id.in_(device_ids)).filter( - Device.binding == None + Device.binding == None # noqa: E711 ) else: self.devices = Device.query.filter(Device.owner_id == g.user.id).filter( - Device.binding == None + Device.binding == None # noqa: E711 ) if self.only_unassigned: self.devices = self.devices.filter_by(lots=None) @@ -655,19 +655,30 @@ class NewDeviceForm(FlaskForm): class TagDeviceForm(FlaskForm): - tag = SelectField('Tag', choices=[]) - device = StringField('Device', [validators.Optional()]) + tag = SelectField( + 'Tag', + choices=[], + render_kw={ + 'class': 'form-control selectpicker', + 'data-live-search': 'true', + }, + ) def __init__(self, *args, **kwargs): self.delete = kwargs.pop('delete', None) - self.device_id = kwargs.pop('device', None) + self.dhid = kwargs.pop('dhid', None) + self._device = ( + Device.query.filter(Device.devicehub_id == self.dhid) + .filter(Device.owner_id == g.user.id) + .one() + ) super().__init__(*args, **kwargs) if self.delete: tags = ( Tag.query.filter(Tag.owner_id == g.user.id) - .filter_by(device_id=self.device_id) + .filter_by(device_id=self._device.id) .order_by(Tag.id) ) else: @@ -695,20 +706,6 @@ class TagDeviceForm(FlaskForm): self.tag.errors = [("This tag is actualy in use.")] return False - if self.device.data: - try: - self.device.data = int(self.device.data.split(',')[-1]) - except: # noqa: E722 - self.device.data = None - - if self.device_id or self.device.data: - self.device_id = self.device_id or self.device.data - self._device = ( - Device.query.filter(Device.id == self.device_id) - .filter(Device.owner_id == g.user.id) - .one() - ) - return True def save(self): diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 28f1e252..8b65e37a 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -80,7 +80,6 @@ class DeviceListMixin(GenericMixin): self.context.update( { 'devices': devices, - 'form_tag_device': TagDeviceForm(), 'form_new_action': form_new_action, 'form_new_allocate': AllocateForm(lot=lot_id), 'form_new_datawipe': DataWipeForm(lot=lot_id), @@ -148,11 +147,13 @@ class DeviceDetailView(GenericMixin): .one() ) + form_tags = TagDeviceForm(dhid=id) self.context.update( { 'device': device, 'placeholder': device.binding or device.placeholder, 'page_title': 'Device {}'.format(device.devicehub_id), + 'form_tag_device': form_tags, } ) @@ -542,12 +543,15 @@ class TagLinkDeviceView(View): methods = ['POST'] decorators = [login_required] - def dispatch_request(self): - form = TagDeviceForm() + def dispatch_request(self, dhid): + form = TagDeviceForm(dhid=dhid) if form.validate_on_submit(): + tag = form.tag.data form.save() - return flask.redirect(request.referrer) + next_url = url_for('inventory.device_details', id=dhid) + messages.success('Tag {} was linked successfully!'.format(tag)) + return flask.redirect(next_url) class TagUnlinkDeviceView(GenericMixin): @@ -555,19 +559,20 @@ class TagUnlinkDeviceView(GenericMixin): decorators = [login_required] template_name = 'inventory/tag_unlink_device.html' - def dispatch_request(self, id): + def dispatch_request(self, dhid): self.get_context() - form = TagDeviceForm(delete=True, device=id) + form = TagDeviceForm(delete=True, dhid=dhid) if form.validate_on_submit(): form.remove() - next_url = url_for('inventory.devicelist') + next_url = url_for('inventory.device_details', id=dhid) + messages.success('Tag {} was unlinked successfully!'.format(form.tag.data)) return flask.redirect(next_url) self.context.update( { 'form': form, - 'referrer': request.referrer, + 'dhid': dhid, } ) @@ -1211,10 +1216,11 @@ devices.add_url_rule( '/device/edit//', view_func=DeviceEditView.as_view('device_edit') ) devices.add_url_rule( - '/tag/devices/add/', view_func=TagLinkDeviceView.as_view('tag_devices_add') + '/tag/devices//add/', + view_func=TagLinkDeviceView.as_view('tag_devices_add'), ) devices.add_url_rule( - '/tag/devices//del/', + '/tag/devices//del/', view_func=TagUnlinkDeviceView.as_view('tag_devices_del'), ) devices.add_url_rule( diff --git a/ereuse_devicehub/templates/ereuse_devicehub/base_site.html b/ereuse_devicehub/templates/ereuse_devicehub/base_site.html index 87dfb226..4d793495 100644 --- a/ereuse_devicehub/templates/ereuse_devicehub/base_site.html +++ b/ereuse_devicehub/templates/ereuse_devicehub/base_site.html @@ -256,6 +256,14 @@ + + + + diff --git a/ereuse_devicehub/templates/inventory/addDevicestag.html b/ereuse_devicehub/templates/inventory/addDevicestag.html index bb02c221..55c11fbc 100644 --- a/ereuse_devicehub/templates/inventory/addDevicestag.html +++ b/ereuse_devicehub/templates/inventory/addDevicestag.html @@ -7,16 +7,12 @@ -
- {{ form_tag_device.csrf_token }} + + + +{% include "inventory/addDevicestag.html" %} {% endblock main %} diff --git a/ereuse_devicehub/templates/inventory/device_list.html b/ereuse_devicehub/templates/inventory/device_list.html index b2ed449d..8d305417 100644 --- a/ereuse_devicehub/templates/inventory/device_list.html +++ b/ereuse_devicehub/templates/inventory/device_list.html @@ -268,42 +268,11 @@ - - -{% include "inventory/addDevicestag.html" %} {% include "inventory/lot_delete_modal.html" %} {% include "inventory/actions.html" %} {% include "inventory/allocate.html" %} {% include "inventory/data_wipe.html" %} {% include "inventory/trade.html" %} {% include "inventory/alert_export_error.html" %} -{% include "inventory/alert_unlink_tag_error.html" %} {% include "inventory/alert_lots_changes.html" %} diff --git a/ereuse_devicehub/templates/inventory/tag_unlink_device.html b/ereuse_devicehub/templates/inventory/tag_unlink_device.html index af6ed3a6..42d67014 100644 --- a/ereuse_devicehub/templates/inventory/tag_unlink_device.html +++ b/ereuse_devicehub/templates/inventory/tag_unlink_device.html @@ -49,7 +49,7 @@
- Cancel + Cancel