Merge pull request #271 from eReuse/feature/all-device-list

Feature: 3196 all device list
This commit is contained in:
Santiago L 2022-05-13 10:26:49 +02:00 committed by GitHub
commit a444579c05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 26 deletions

View File

@ -133,6 +133,7 @@ class FilterForm(FlaskForm):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.lots = lots self.lots = lots
self.lot_id = lot_id self.lot_id = lot_id
self.only_unassigned = kwargs.pop('only_unassigned', True)
self._get_types() self._get_types()
def _get_types(self): def _get_types(self):
@ -148,9 +149,9 @@ class FilterForm(FlaskForm):
device_ids = (d.id for d in self.lot.devices) device_ids = (d.id for d in self.lot.devices)
self.devices = Device.query.filter(Device.id.in_(device_ids)) self.devices = Device.query.filter(Device.id.in_(device_ids))
else: else:
self.devices = Device.query.filter(Device.owner_id == g.user.id).filter_by( self.devices = Device.query.filter(Device.owner_id == g.user.id)
lots=None if self.only_unassigned:
) self.devices = self.devices.filter_by(lots=None)
def search(self): def search(self):
self.filter_from_lots() self.filter_from_lots()

View File

@ -1,5 +1,6 @@
import csv import csv
import logging import logging
from distutils.util import strtobool
from io import StringIO from io import StringIO
import flask import flask
@ -40,60 +41,64 @@ logger = logging.getLogger(__name__)
class DeviceListMix(GenericMixView): class DeviceListMix(GenericMixView):
template_name = 'inventory/device_list.html' template_name = 'inventory/device_list.html'
def get_context(self, lot_id): def get_context(self, lot_id, only_unassigned=True):
super().get_context() super().get_context()
lots = self.context['lots'] lots = self.context['lots']
form_filter = FilterForm(lots, lot_id) form_filter = FilterForm(lots, lot_id, only_unassigned=only_unassigned)
devices = form_filter.search() devices = form_filter.search()
lot = None lot = None
tags = (
Tag.query.filter(Tag.owner_id == current_user.id)
.filter(Tag.device_id.is_(None))
.order_by(Tag.id.asc())
)
if lot_id: if lot_id:
lot = lots.filter(Lot.id == lot_id).one() lot = lots.filter(Lot.id == lot_id).one()
form_new_action = NewActionForm(lot=lot.id)
form_new_allocate = AllocateForm(lot=lot.id)
form_new_datawipe = DataWipeForm(lot=lot.id)
form_new_trade = TradeForm( form_new_trade = TradeForm(
lot=lot.id, lot=lot.id,
user_to=g.user.email, user_to=g.user.email,
user_from=g.user.email, user_from=g.user.email,
) )
else: else:
form_new_action = NewActionForm()
form_new_allocate = AllocateForm()
form_new_datawipe = DataWipeForm()
form_new_trade = '' form_new_trade = ''
action_devices = form_new_action.devices.data
list_devices = []
if action_devices:
list_devices.extend([int(x) for x in action_devices.split(",")])
form_new_action = NewActionForm(lot=lot_id)
self.context.update( self.context.update(
{ {
'devices': devices, 'devices': devices,
'form_tag_device': TagDeviceForm(), 'form_tag_device': TagDeviceForm(),
'form_new_action': form_new_action, 'form_new_action': form_new_action,
'form_new_allocate': form_new_allocate, 'form_new_allocate': AllocateForm(lot=lot_id),
'form_new_datawipe': form_new_datawipe, 'form_new_datawipe': DataWipeForm(lot=lot_id),
'form_new_trade': form_new_trade, 'form_new_trade': form_new_trade,
'form_filter': form_filter, 'form_filter': form_filter,
'form_print_labels': PrintLabelsForm(), 'form_print_labels': PrintLabelsForm(),
'lot': lot, 'lot': lot,
'tags': tags, 'tags': self.get_user_tags(),
'list_devices': list_devices, 'list_devices': self.get_selected_devices(form_new_action),
'unassigned_devices': only_unassigned,
} }
) )
return self.context return self.context
def get_user_tags(self):
return (
Tag.query.filter(Tag.owner_id == current_user.id)
.filter(Tag.device_id.is_(None))
.order_by(Tag.id.asc())
)
def get_selected_devices(self, action_form):
"""Retrieve selected devices (when action form is submited)"""
action_devices = action_form.devices.data
if action_devices:
return [int(x) for x in action_devices.split(",")]
return []
class DeviceListView(DeviceListMix): class DeviceListView(DeviceListMix):
def dispatch_request(self, lot_id=None): def dispatch_request(self, lot_id=None):
self.get_context(lot_id) only_unassigned = request.args.get(
'only_unassigned', default=True, type=strtobool
)
self.get_context(lot_id, only_unassigned)
return flask.render_template(self.template_name, **self.context) return flask.render_template(self.template_name, **self.context)

View File

@ -101,6 +101,15 @@
</a> </a>
</li><!-- End Dashboard Nav --> </li><!-- End Dashboard Nav -->
<li class="nav-heading">Devices</li>
<li class="nav-item">
<a class="nav-link collapsed" href="{{ url_for('inventory.devicelist') }}?only_unassigned=false">
<i class="bi bi-laptop"></i>
<span>All devices</span>
</a>
</li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link collapsed" href="{{ url_for('inventory.devicelist') }}"> <a class="nav-link collapsed" href="{{ url_for('inventory.devicelist') }}">
<i class="bi-menu-button-wide"></i> <i class="bi-menu-button-wide"></i>

View File

@ -7,7 +7,11 @@
<ol class="breadcrumb"> <ol class="breadcrumb">
<li class="breadcrumb-item"><a href="{{ url_for('inventory.devicelist')}}">Inventory</a></li> <li class="breadcrumb-item"><a href="{{ url_for('inventory.devicelist')}}">Inventory</a></li>
{% if not lot %} {% if not lot %}
<li class="breadcrumb-item active">Unassigned</li> {% if unassigned_devices %}
<li class="breadcrumb-item active">Unassigned</li>
{% else %}
<li class="breadcrumb-item active">All devices</li>
{% endif %}
{% elif lot.is_temporary %} {% elif lot.is_temporary %}
<li class="breadcrumb-item active">Temporary Lot</li> <li class="breadcrumb-item active">Temporary Lot</li>
<li class="breadcrumb-item active">{{ lot.name }}</li> <li class="breadcrumb-item active">{{ lot.name }}</li>